Skip to content

Commit

Permalink
Refactor code structure and file organization
Browse files Browse the repository at this point in the history
- Renamed directories and files to follow a more organized structure
- Updated import statements in affected files to reflect the new directory structure
  • Loading branch information
jsdbroughton committed Aug 4, 2024
1 parent 6eba125 commit d09e5c7
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ jobs:
- name: Extract functionInputSchema
id: extract_schema
run: |
python main.py generate_schema ${HOME}/${{ env.FUNCTION_SCHEMA_FILE_NAME }}
python src/main.py generate_schema ${HOME}/${{ env.FUNCTION_SCHEMA_FILE_NAME }}
- name: Speckle Automate Function - Build and Publish
uses: specklesystems/[email protected]
with:
speckle_automate_url: ${{ env.SPECKLE_AUTOMATE_URL || 'https://automate.speckle.dev' }}
speckle_token: ${{ secrets.SPECKLE_FUNCTION_TOKEN }}
speckle_function_id: ${{ secrets.SPECKLE_FUNCTION_ID }}
speckle_function_input_schema_file_path: ${{ env.FUNCTION_SCHEMA_FILE_NAME }}
speckle_function_command: 'python -u main.py run'
speckle_function_command: 'python -u src/main.py run'
speckle_function_recommended_cpu_m: 4000
speckle_function_recommended_memory_mi: 4000
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
description = "Examine model health by identifying areas of high mesh density as possible perfomance issues."
authors = ["Jonathon Broughton <[email protected]>"]
readme = "README.md"
packages = [{ include = "src" }]

[tool.poetry.dependencies]
python = "^3.11"
Expand All @@ -25,6 +26,7 @@ requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.ruff]
line-length = 88
select = [
"E", # pycodestyle
"F", # pyflakes
Expand All @@ -34,4 +36,4 @@ select = [
]

[tool.ruff.pydocstyle]
convention = "google"
convention = "google"
File renamed without changes.
3 changes: 1 addition & 2 deletions Objects/objects.py → src/Objects/objects.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import statistics
from dataclasses import dataclass, field
from typing import Any, Dict, Iterable, List, Optional, TypeVar, Union
Expand All @@ -11,7 +10,7 @@
from specklepy.objects.other import RenderMaterial
from specklepy.objects.primitive import Interval

from Utilities.utilities import Utilities
from src.Utilities import Utilities

T = TypeVar("T", bound=Base)

Expand Down
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion Utilities/plotting.py → src/Utilities/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,3 @@ def plot_area_distribution(areas: List[float]) -> None:
plt.ylabel("Count")
plt.title("Area Distribution")
plt.grid(True)
# plt.show()
35 changes: 32 additions & 3 deletions Utilities/reporting.py → src/Utilities/reporting.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import io
import os
import tempfile
from datetime import datetime
from pathlib import Path
from typing import IO, Any, Dict, List, Union

import httpx
import matplotlib.pyplot as plt
from PIL import Image as PILImage
from reportlab.lib.colors import green, red
Expand All @@ -21,8 +21,8 @@
TableStyle,
)

from Objects.objects import HealthObject
from Utilities.plotting import Plotting
from src.Objects.objects import HealthObject
from src.Utilities.plotting import Plotting


class Report:
Expand Down Expand Up @@ -260,3 +260,32 @@ def write_pdf_to_temp(report: IO[bytes]) -> str:
temp_file.write_bytes(report.read())

return str(temp_file)


from speckle_automate import AutomationContext


def safe_store_file_result(automate_context: AutomationContext, file_name: str):
# Store the original URL
original_url = automate_context.automation_run_data.speckle_server_url

try:
# Modify the URL property of the automation_run_data
automate_context.automation_run_data.speckle_server_url = original_url.rstrip(
"/"
)

# Attempt to store the file
automate_context.store_file_result(file_name)
except httpx.HTTPStatusError as e:
if e.response.status_code == 404:
# Handle the 404 error
error_message = f"Unable to store file: {file_name}. Error: {str(e)}"
print(error_message) # For logging purposes
automate_context.mark_run_exception(error_message)

else:
raise
finally:
# Restore the original URL
automate_context.automation_run_data.speckle_server_url = original_url
4 changes: 2 additions & 2 deletions Utilities/utilities.py → src/Utilities/utilities.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import List, TypeVar, Iterable, Optional, Tuple
from typing import List, TypeVar, Iterable, Optional

from specklepy.objects.base import Base
import sys

from Utilities.flatten import extract_base_and_transform
from src.Utilities.flatten import extract_base_and_transform

T = TypeVar("T", bound=Base)

Expand Down
Empty file added src/__init__.py
Empty file.
26 changes: 17 additions & 9 deletions main.py → src/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
"""This module contains the business logic for a Speckle Automate function.
(
"""This module contains the business logic for a Speckle Automate function.
The purpose is to demonstrate how one can use the automation_context module
to process and analyze data in a Speckle project.
"""
The purpose is to demonstrate how one can use the automation_context module
to process and analyze data in a Speckle project.
"""
)
from pydantic import Field
from speckle_automate import (
AutomateBase,
AutomationContext,
execute_automate_function,
)

import Objects.objects
from Objects.objects import (
attach_visual_markers,
colorise_densities,
Expand All @@ -25,6 +26,7 @@
## swap those into the original commit object
## send that back to the server


class FunctionInputs(AutomateBase):
"""Definition of user inputs for this function.
Expand Down Expand Up @@ -53,7 +55,7 @@ class FunctionInputs(AutomateBase):


def automate_function(
automate_context: AutomationContext, function_inputs: FunctionInputs
automate_context: AutomationContext, function_inputs: FunctionInputs
) -> None:
"""Analyzes Speckle data and provides visual markers and notifications.
Expand Down Expand Up @@ -84,6 +86,10 @@ def automate_function(
automate_context, health_objects, function_inputs.density_level
)





colorise_densities(automate_context, health_objects)

# Wrap up the analysis by marking the run either successful or failed.
Expand All @@ -95,7 +101,9 @@ def automate_function(

commit_details = {
"stream_id": automate_context.automation_run_data.project_id,
"commit_id": automate_context.automation_run_data.triggers[0].payload.version_id,
"commit_id": automate_context.automation_run_data.triggers[
0
].payload.version_id,
"server_url": automate_context.automation_run_data.speckle_server_url,
}

Expand All @@ -117,12 +125,12 @@ def automate_function(

file_name = Report.write_pdf_to_temp(report)

print(commit_details['server_url'])
print(commit_details["server_url"])

automate_context.store_file_result(file_name)

# colorise the objects that pass/fail and send to a new model version
Objects.objects.transport_recolorized_commit(
src.Objects.objects.transport_recolorized_commit(
automate_context, health_objects, version_root_object
)

Expand Down
3 changes: 3 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os
import sys
from dotenv import load_dotenv
# Add the src directory to the Python path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../src')))


def pytest_configure(config):
Expand Down
3 changes: 1 addition & 2 deletions tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from specklepy.api.client import SpeckleClient
from specklepy.objects.base import Base

from main import FunctionInputs, automate_function
from src.main import FunctionInputs, automate_function


def crypto_random_string(length: int) -> str:
Expand Down Expand Up @@ -94,7 +94,6 @@ def test_object() -> Base:
return root_object



@pytest.fixture()
# fixture to mock the AutomationRunData that would be generated by a full Automation Run
def fake_automation_run_data(request, test_client: SpeckleClient) -> AutomationRunData:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from specklepy.objects.base import Base

from Objects.objects import HealthObject
from Utilities.utilities import Utilities
from src.Objects import HealthObject
from src.Utilities import Utilities


@pytest.fixture
Expand Down Expand Up @@ -39,7 +39,7 @@ def test_filter_displayable_bases(mock_base):
displayable_bases = Utilities.filter_displayable_bases(mock_base)
assert (
len(displayable_bases) == 2
) # Only child_1 and child_2 should be considered displayable
)


def test_convert_from_base_with_nested_elements(mock_base):
Expand All @@ -48,7 +48,7 @@ def test_convert_from_base_with_nested_elements(mock_base):
assert health_obj.id == "12345"
assert (
health_obj.speckle_type == "Base"
) # Assuming no speckle_type was set in the mock_base
)


def test_density_with_nested_elements(mock_base):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from typing import Any

import pytest

# Speckle is a data platform for AEC; here we're importing essential modules from it
from specklepy.api import operations
from specklepy.api.client import SpeckleClient
from specklepy.objects import Base
from specklepy.transports.server import ServerTransport


# Setting up some pytest fixtures for testing
# Fixtures are a way to provide consistent test data or configuration for each test

Expand Down
2 changes: 1 addition & 1 deletion tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from specklepy.objects.base import Base
from specklepy.objects.geometry import Mesh

from Utilities.utilities import Utilities
from src.Utilities import Utilities


@pytest.fixture
Expand Down

0 comments on commit d09e5c7

Please sign in to comment.