Skip to content

Commit

Permalink
Convert to Absolute Imports [AARD-1737] (#1013)
Browse files Browse the repository at this point in the history
  • Loading branch information
HunterBarclay authored Aug 9, 2024
2 parents 2c147a4 + 4514f7e commit 5d4e5e4
Show file tree
Hide file tree
Showing 35 changed files with 181 additions and 242 deletions.
68 changes: 29 additions & 39 deletions exporter/SynthesisFusionAddin/Synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,45 @@

import adsk.core

# Currently required for `resolveDependencies()`, will be required for absolute imports.
# Required for absolute imports.
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "proto", "proto_out")))

from .src.Dependencies import resolveDependencies # isort:skip
from src.Dependencies import resolveDependencies
from src.Logging import logFailure, setupLogger

# Transition: AARD-1741
# Import order should be removed in AARD-1737 and `setupLogger()` moved to `__init__.py`
from .src.Logging import getLogger, logFailure, setupLogger # isort:skip

setupLogger()
logger = setupLogger()

try:
from .src.general_imports import APP_NAME, DESCRIPTION, INTERNAL_ID, gm
from .src.UI import (
HUI,
Camera,
ConfigCommand,
MarkingMenu,
ShowAPSAuthCommand,
ShowWebsiteCommand,
# Attempt to import required pip dependencies to verify their installation.
import requests
from proto.proto_out import (
assembly_pb2,
joint_pb2,
material_pb2,
motor_pb2,
signal_pb2,
types_pb2,
)
from .src.UI.Toolbar import Toolbar
except (ImportError, ModuleNotFoundError) as error:
getLogger().warn(f"Running resolve dependencies with error of:\n{error}")
except (ImportError, ModuleNotFoundError, BaseException) as error: # BaseException required to catch proto.VersionError
logger.warn(f"Running resolve dependencies with error of:\n{error}")
result = resolveDependencies()
if result:
adsk.core.Application.get().userInterface.messageBox("Installed required dependencies.\nPlease restart Fusion.")


from src import APP_NAME, DESCRIPTION, INTERNAL_ID, gm
from src.UI import (
HUI,
Camera,
ConfigCommand,
MarkingMenu,
ShowAPSAuthCommand,
ShowWebsiteCommand,
)
from src.UI.Toolbar import Toolbar


@logFailure
def run(_):
"""## Entry point to application from Fusion.
Expand Down Expand Up @@ -68,28 +78,8 @@ def stop(_):

# nm.deleteMe()

logger = getLogger(INTERNAL_ID)
logger.cleanupHandlers()

for file in gm.files:
try:
os.remove(file)
except OSError:
pass

# removes path so that proto files don't get confused

import sys

path = os.path.abspath(os.path.dirname(__file__))

path_proto_files = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "proto", "proto_out"))

if path in sys.path:
sys.path.remove(path)

if path_proto_files in sys.path:
sys.path.remove(path_proto_files)
gm.clear()


@logFailure
Expand Down
6 changes: 3 additions & 3 deletions exporter/SynthesisFusionAddin/src/APS/APS.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

import requests

from ..general_imports import INTERNAL_ID, gm, my_addin_path
from ..Logging import getLogger
from src import ADDIN_PATH, gm
from src.Logging import getLogger

logger = getLogger()

CLIENT_ID = "GCxaewcLjsYlK8ud7Ka9AKf9dPwMR3e4GlybyfhAK2zvl3tU"
auth_path = os.path.abspath(os.path.join(my_addin_path, "..", ".aps_auth"))
auth_path = os.path.abspath(os.path.join(ADDIN_PATH, "..", ".aps_auth"))

APS_AUTH = None
APS_USER_INFO = None
Expand Down
17 changes: 7 additions & 10 deletions exporter/SynthesisFusionAddin/src/Dependencies.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import importlib.machinery
import importlib.util
import os
import platform
import subprocess
import sys
from pathlib import Path

import adsk.core
import adsk.fusion

from .Logging import getLogger, logFailure
from src import SYSTEM
from src.Logging import getLogger, logFailure

logger = getLogger()
system = platform.system()

# Since the Fusion python runtime is separate from the system python runtime we need to do some funky things
# in order to download and install python packages separate from the standard library.
Expand All @@ -29,13 +28,11 @@ def getInternalFusionPythonInstillationFolder() -> str:
pythonStandardLibraryModulePath = importlib.machinery.PathFinder.find_spec("os", sys.path).origin

# Depending on platform, adjust to folder to where the python executable binaries are stored.
if system == "Windows":
if SYSTEM == "Windows":
folder = f"{Path(pythonStandardLibraryModulePath).parents[1]}"
elif system == "Darwin":
folder = f"{Path(pythonStandardLibraryModulePath).parents[2]}/bin"
else:
# TODO: System string should be moved to __init__ after GH-1013
raise RuntimeError("Unsupported platform.")
assert SYSTEM == "Darwin"
folder = f"{Path(pythonStandardLibraryModulePath).parents[2]}/bin"

return folder

Expand Down Expand Up @@ -100,7 +97,7 @@ def resolveDependencies() -> bool | None:
adsk.doEvents()

pythonFolder = getInternalFusionPythonInstillationFolder()
pythonExecutableFile = "python.exe" if system == "Windows" else "python" # Confirming 110% everything is fine.
pythonExecutableFile = "python.exe" if SYSTEM == "Windows" else "python" # Confirming 110% everything is fine.
pythonExecutablePath = os.path.join(pythonFolder, pythonExecutableFile)

progressBar = ui.createProgressDialog()
Expand All @@ -109,7 +106,7 @@ def resolveDependencies() -> bool | None:
progressBar.show("Synthesis", f"Installing dependencies...", 0, len(PIP_DEPENDENCY_VERSION_MAP) * 2 + 2, 0)

# Install pip manually on macos as it is not included by default? Really?
if system == "Darwin" and not os.path.exists(os.path.join(pythonFolder, "pip")):
if SYSTEM == "Darwin" and not os.path.exists(os.path.join(pythonFolder, "pip")):
pipInstallScriptPath = os.path.join(pythonFolder, "get-pip.py")
if not os.path.exists(pipInstallScriptPath):
executeCommand("curl", "https://bootstrap.pypa.io/get-pip.py", "-o", pipInstallScriptPath)
Expand Down
10 changes: 5 additions & 5 deletions exporter/SynthesisFusionAddin/src/GlobalManager.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
""" Initializes the global variables that are set in the run method to reduce hanging commands. """

import logging

import adsk.core
import adsk.fusion

from .general_imports import *
from .strings import *


class GlobalManager(object):
"""Global Manager instance"""
Expand Down Expand Up @@ -47,6 +42,11 @@ def __init__(self):
def __str__(self):
return "GlobalManager"

def clear(self):
for attr, value in self.__dict__.items():
if isinstance(value, list):
setattr(self, attr, [])

instance = None

def __new__(cls):
Expand Down
7 changes: 4 additions & 3 deletions exporter/SynthesisFusionAddin/src/Logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

import adsk.core

from .strings import INTERNAL_ID
from .UI.OsHelper import getOSPath
from src import INTERNAL_ID
from src.UI.OsHelper import getOSPath

MAX_LOG_FILES_TO_KEEP = 10
TIMING_LEVEL = 25
Expand All @@ -27,7 +27,7 @@ def cleanupHandlers(self) -> None:
handler.close()


def setupLogger() -> None:
def setupLogger() -> SynthesisLogger:
now = datetime.now().strftime("%H-%M-%S")
today = date.today()
logFileFolder = getOSPath(f"{pathlib.Path(__file__).parent.parent}", "logs")
Expand All @@ -46,6 +46,7 @@ def setupLogger() -> None:
logger = getLogger(INTERNAL_ID)
logger.setLevel(10) # Debug
logger.addHandler(logHandler)
return cast(SynthesisLogger, logger)


def getLogger(name: str | None = None) -> SynthesisLogger:
Expand Down
6 changes: 3 additions & 3 deletions exporter/SynthesisFusionAddin/src/Parser/ExporterOptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import adsk.core
from adsk.fusion import CalculationAccuracy, TriangleMeshQualityOptions

from ..Logging import logFailure, timed
from ..strings import INTERNAL_ID
from ..Types import (
from src import INTERNAL_ID
from src.Logging import logFailure, timed
from src.Types import (
KG,
ExportLocation,
ExportMode,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
# Contains all of the logic for mapping the Components / Occurrences
import logging
import traceback
import uuid
from typing import *

import adsk.core
import adsk.fusion
from proto.proto_out import assembly_pb2, joint_pb2, material_pb2, types_pb2

from ...Logging import logFailure, timed
from ...Types import ExportMode
from ..ExporterOptions import ExporterOptions
from . import PhysicalProperties
from .PDMessage import PDMessage
from .Utilities import *
from src.Logging import logFailure
from src.Parser.ExporterOptions import ExporterOptions
from src.Parser.SynthesisParser import PhysicalProperties
from src.Parser.SynthesisParser.PDMessage import PDMessage
from src.Parser.SynthesisParser.Utilities import (
fill_info,
guid_component,
guid_occurrence,
)
from src.Types import ExportMode

# TODO: Impelement Material overrides

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import enum
import logging
import traceback
from typing import *
from typing import Union

import adsk.core
import adsk.fusion
from proto.proto_out import joint_pb2, types_pb2

from ...general_imports import *
from ...Logging import getLogger, logFailure
from ..ExporterOptions import ExporterOptions
from .PDMessage import PDMessage
from .Utilities import guid_component, guid_occurrence
from src import gm
from src.Logging import getLogger, logFailure
from src.Parser.ExporterOptions import ExporterOptions
from src.Parser.SynthesisParser.PDMessage import PDMessage
from src.Parser.SynthesisParser.Utilities import guid_component, guid_occurrence

logger = getLogger()

Expand Down
19 changes: 11 additions & 8 deletions exporter/SynthesisFusionAddin/src/Parser/SynthesisParser/Joints.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@

import adsk.core
import adsk.fusion
from proto.proto_out import assembly_pb2, joint_pb2, motor_pb2, signal_pb2, types_pb2

from ...general_imports import *
from ...Logging import getLogger
from ...Types import JointParentType, SignalType
from ..ExporterOptions import ExporterOptions
from .PDMessage import PDMessage
from .Utilities import construct_info, fill_info, guid_occurrence
from proto.proto_out import assembly_pb2, joint_pb2, signal_pb2, types_pb2

from src.Logging import getLogger
from src.Parser.ExporterOptions import ExporterOptions
from src.Parser.SynthesisParser.PDMessage import PDMessage
from src.Parser.SynthesisParser.Utilities import (
construct_info,
fill_info,
guid_occurrence,
)
from src.Types import JointParentType, SignalType

logger = getLogger()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
# Should contain Physical and Apperance materials ?
import json
import logging
import math
import traceback

import adsk
from proto.proto_out import material_pb2

from ...general_imports import *
from ...Logging import logFailure, timed
from ..ExporterOptions import ExporterOptions
from .PDMessage import PDMessage
from .Utilities import *
from src.Logging import logFailure
from src.Parser.ExporterOptions import ExporterOptions
from src.Parser.SynthesisParser.PDMessage import PDMessage
from src.Parser.SynthesisParser.Utilities import construct_info, fill_info

OPACITY_RAMPING_CONSTANT = 14.0

Expand Down
22 changes: 14 additions & 8 deletions exporter/SynthesisFusionAddin/src/Parser/SynthesisParser/Parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
from google.protobuf.json_format import MessageToJson
from proto.proto_out import assembly_pb2, types_pb2

from ...APS.APS import getAuth, upload_mirabuf
from ...general_imports import *
from ...Logging import getLogger, logFailure, timed
from ...Types import ExportLocation, ExportMode
from ...UI.Camera import captureThumbnail, clearIconCache
from ..ExporterOptions import ExporterOptions
from . import Components, JointHierarchy, Joints, Materials, PDMessage
from .Utilities import *
from src import gm
from src.APS.APS import getAuth, upload_mirabuf
from src.Logging import getLogger, logFailure, timed
from src.Parser.ExporterOptions import ExporterOptions
from src.Parser.SynthesisParser import (
Components,
JointHierarchy,
Joints,
Materials,
PDMessage,
)
from src.Parser.SynthesisParser.Utilities import fill_info
from src.Types import ExportLocation, ExportMode
from src.UI.Camera import captureThumbnail, clearIconCache

logger = getLogger()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@
"""

import logging
import traceback
from typing import Union

import adsk
from proto.proto_out import types_pb2

from ...general_imports import INTERNAL_ID
from ...Logging import logFailure
from src.Logging import logFailure


@logFailure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
- Success
"""

from typing import *
from typing import Union

import adsk.core
import adsk.fusion
from proto.proto_out import assembly_pb2

from ...Logging import logFailure
from src.Logging import logFailure


@logFailure
Expand Down
Loading

0 comments on commit 5d4e5e4

Please sign in to comment.