Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c7f5e0718b | |||
| d2685c5cf5 | |||
| 1b83e5a84b | |||
| 77e09b9780 | |||
| 402f750200 | |||
| a43e7471a4 | |||
| a4ed7ebb08 | |||
| d547cdaac0 | |||
| 6f35c1bd20 | |||
| 420c73f484 | |||
| c2859475cc | |||
| 56e8d65e2b | |||
| 7885a6be8d | |||
| b19b85c9d1 | |||
| db4b2b7f87 | |||
| 77916995bc | |||
| 3dd56dc38e | |||
| ae42bec1c3 | |||
| ea7baf8eb5 | |||
| 8352bb5c9a | |||
| fc34b876fd | |||
| 183993cfc5 | |||
| 9be3b4b93d | |||
| 0b14660115 | |||
| 68c4c682a0 | |||
| 4f93ddcaf3 |
Vendored
+3
-2
@@ -5,6 +5,7 @@
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
|
||||
|
||||
{
|
||||
"name": "Python: Current File",
|
||||
"type": "python",
|
||||
@@ -17,8 +18,8 @@
|
||||
"name": "Pytest",
|
||||
"type": "python",
|
||||
"request": "launch",
|
||||
"program": "poetry",
|
||||
"args": ["run", "pytest"],
|
||||
"program": "pytest",
|
||||
"args": [],
|
||||
"console": "integratedTerminal",
|
||||
"justMyCode": true
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ from warnings import warn
|
||||
|
||||
from deprecated import deprecated
|
||||
from gql import Client
|
||||
from gql.transport.exceptions import TransportServerError
|
||||
from gql.transport.requests import RequestsHTTPTransport
|
||||
from gql.transport.websockets import WebsocketsTransport
|
||||
|
||||
@@ -164,13 +165,23 @@ class SpeckleClient:
|
||||
|
||||
self._init_resources()
|
||||
|
||||
if self.user.get() is None:
|
||||
warn(
|
||||
SpeckleWarning(
|
||||
"Possibly invalid token - could not authenticate Speckle Client"
|
||||
f" for server {self.url}"
|
||||
try:
|
||||
user_or_error = self.active_user.get()
|
||||
if isinstance(user_or_error, SpeckleException):
|
||||
if isinstance(user_or_error.exception, TransportServerError):
|
||||
raise user_or_error.exception
|
||||
else:
|
||||
raise user_or_error
|
||||
except TransportServerError as ex:
|
||||
if ex.code == 403:
|
||||
warn(
|
||||
SpeckleWarning(
|
||||
"Possibly invalid token - could not authenticate Speckle Client"
|
||||
f" for server {self.url}"
|
||||
)
|
||||
)
|
||||
)
|
||||
else:
|
||||
raise ex
|
||||
|
||||
def execute_query(self, query: str) -> Dict:
|
||||
return self.httpclient.execute(query)
|
||||
|
||||
@@ -567,16 +567,17 @@ class Resource(ResourceBase):
|
||||
|
||||
email_invites = [
|
||||
{"streamId": stream_id, "message": message, "email": email}
|
||||
for email in emails
|
||||
if emails is not None
|
||||
for email in (emails if emails is not None else [])
|
||||
if email is not None
|
||||
]
|
||||
|
||||
user_invites = [
|
||||
{"streamId": stream_id, "message": message, "userId": user_id}
|
||||
for user_id in user_ids
|
||||
if user_ids is not None
|
||||
for user_id in (user_ids if user_ids is not None else [])
|
||||
if user_id is not None
|
||||
]
|
||||
|
||||
|
||||
params = {"input": [*email_invites, *user_invites]}
|
||||
|
||||
return self.make_request(
|
||||
|
||||
@@ -106,6 +106,18 @@ def user_speckle_folder_path() -> Path:
|
||||
return _ensure_folder_exists(user_application_data_path(), _application_name)
|
||||
|
||||
|
||||
def user_speckle_connector_installation_path(host_application: str) -> Path:
|
||||
"""
|
||||
Gets a connector specific installation folder.
|
||||
|
||||
In this folder we can put our connector installation and all python packages.
|
||||
"""
|
||||
return _ensure_folder_exists(
|
||||
_ensure_folder_exists(user_speckle_folder_path(), "connector_installations"),
|
||||
host_application,
|
||||
)
|
||||
|
||||
|
||||
def accounts_folder_path() -> Path:
|
||||
"""Get the folder where the Speckle accounts data should be stored."""
|
||||
return _ensure_folder_exists(user_speckle_folder_path(), _accounts_folder_name)
|
||||
|
||||
@@ -5,6 +5,7 @@ from typing import (
|
||||
Any,
|
||||
ClassVar,
|
||||
Dict,
|
||||
ForwardRef,
|
||||
List,
|
||||
Optional,
|
||||
Set,
|
||||
@@ -217,6 +218,9 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
|
||||
return True, t(value)
|
||||
|
||||
if getattr(t, "__module__", None) == "typing":
|
||||
if isinstance(t, ForwardRef):
|
||||
return True, value
|
||||
|
||||
origin = getattr(t, "__origin__")
|
||||
# below is what in nicer for >= py38
|
||||
# origin = get_origin(t)
|
||||
@@ -303,7 +307,7 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
|
||||
if isinstance(value, t):
|
||||
return True, value
|
||||
|
||||
with contextlib.suppress(ValueError):
|
||||
with contextlib.suppress(ValueError, TypeError):
|
||||
if t is float and value is not None:
|
||||
return True, float(value)
|
||||
# TODO: dafuq, i had to add this not list check
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
from typing import Any, List, Optional
|
||||
from deprecated import deprecated
|
||||
|
||||
from specklepy.objects.geometry import Point, Vector
|
||||
|
||||
from .base import Base
|
||||
|
||||
OTHER = "Objects.Other."
|
||||
OTHER_REVIT = OTHER + "Revit."
|
||||
|
||||
IDENTITY_TRANSFORM = [
|
||||
1.0,
|
||||
@@ -72,7 +74,7 @@ class DisplayStyle(Base, speckle_type=OTHER + "DisplayStyle"):
|
||||
class Transform(
|
||||
Base,
|
||||
speckle_type=OTHER + "Transform",
|
||||
serialize_ignore={"translation", "scaling", "is_identity"},
|
||||
serialize_ignore={"translation", "scaling", "is_identity", "value"},
|
||||
):
|
||||
"""The 4x4 transformation matrix
|
||||
|
||||
@@ -84,12 +86,21 @@ class Transform(
|
||||
_value: Optional[List[float]] = None
|
||||
|
||||
@property
|
||||
@deprecated(version="2.12", reason="Use matrix")
|
||||
def value(self) -> List[float]:
|
||||
"""The transform matrix represented as a flat list of 16 floats"""
|
||||
return self._value
|
||||
|
||||
@value.setter
|
||||
def value(self, value: List[float]) -> None:
|
||||
self.matrix = value
|
||||
|
||||
@property
|
||||
def matrix(self) -> List[float]:
|
||||
"""The transform matrix represented as a flat list of 16 floats"""
|
||||
return self._value
|
||||
|
||||
@matrix.setter
|
||||
def matrix(self, value: List[float]) -> None:
|
||||
try:
|
||||
value = [float(x) for x in value]
|
||||
except (ValueError, TypeError) as error:
|
||||
@@ -118,7 +129,7 @@ class Transform(
|
||||
|
||||
@property
|
||||
def is_identity(self) -> bool:
|
||||
return self.value == IDENTITY_TRANSFORM
|
||||
return self._value == IDENTITY_TRANSFORM
|
||||
|
||||
def apply_to_point(self, point: Point) -> Point:
|
||||
"""Transform a single speckle Point
|
||||
@@ -236,15 +247,38 @@ class BlockDefinition(
|
||||
geometry: Optional[List[Base]] = None
|
||||
|
||||
|
||||
class BlockInstance(
|
||||
Base, speckle_type=OTHER + "BlockInstance", detachable={"blockDefinition"}
|
||||
class Instance(
|
||||
Base, speckle_type=OTHER + "Instance", detachable={"definition"}
|
||||
):
|
||||
blockDefinition: Optional[BlockDefinition] = None
|
||||
transform: Optional[Transform] = None
|
||||
definition: Optional[Base] = None
|
||||
|
||||
|
||||
class BlockInstance(
|
||||
Instance, speckle_type=OTHER + "BlockInstance", serialize_ignore={"blockDefinition"}
|
||||
):
|
||||
@property
|
||||
@deprecated(version="2.13", reason="Use definition")
|
||||
def blockDefinition(self) -> Optional[BlockDefinition]:
|
||||
if isinstance(self.definition, BlockDefinition):
|
||||
return self.definition
|
||||
return None
|
||||
|
||||
@blockDefinition.setter
|
||||
def blockDefinition(self, value: Optional[BlockDefinition]) -> None:
|
||||
self.definition = value
|
||||
|
||||
class RevitInstance(Instance, speckle_type=OTHER_REVIT + "RevitInstance"):
|
||||
level: Optional[Base] = None
|
||||
facingFlipped: bool
|
||||
handFlipped: bool
|
||||
parameters: Optional[Base] = None
|
||||
elementId: Optional[str]
|
||||
|
||||
# TODO: prob move this into a built elements module, but just trialling this for now
|
||||
class RevitParameter(Base, speckle_type="Objects.BuiltElements.Revit.Parameter"):
|
||||
class RevitParameter(
|
||||
Base, speckle_type="Objects.BuiltElements.Revit.Parameter"
|
||||
):
|
||||
name: Optional[str] = None
|
||||
value: Any = None
|
||||
applicationUnitType: Optional[str] = None # eg UnitType UT_Length
|
||||
@@ -254,4 +288,4 @@ class RevitParameter(Base, speckle_type="Objects.BuiltElements.Revit.Parameter")
|
||||
] = None # BuiltInParameterName or GUID for shared parameter
|
||||
isShared: bool = False
|
||||
isReadOnly: bool = False
|
||||
isTypeParameter: bool = False
|
||||
isTypeParameter: bool = False
|
||||
@@ -6,7 +6,10 @@ from specklepy.objects.structural.analysis import (
|
||||
ModelSettings,
|
||||
ModelUnits,
|
||||
)
|
||||
from specklepy.objects.structural.axis import Axis
|
||||
from specklepy.objects.structural.axis import (
|
||||
AxisType,
|
||||
Axis
|
||||
)
|
||||
from specklepy.objects.structural.geometry import (
|
||||
Element1D,
|
||||
Element2D,
|
||||
@@ -82,6 +85,7 @@ __all__ = [
|
||||
"ElementType1D",
|
||||
"ElementType2D",
|
||||
"ElementType3D",
|
||||
"AxisType",
|
||||
"Axis",
|
||||
"Node",
|
||||
"Restraint",
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
from typing import Optional
|
||||
from enum import Enum
|
||||
|
||||
from specklepy.objects.base import Base
|
||||
from specklepy.objects.geometry import Plane
|
||||
|
||||
|
||||
class AxisType(int, Enum):
|
||||
Cartesian = 0
|
||||
Cylindrical = 1
|
||||
Spherical = 2
|
||||
|
||||
|
||||
class Axis(Base, speckle_type="Objects.Structural.Geometry.Axis"):
|
||||
name: Optional[str] = None
|
||||
axisType: Optional[str] = None
|
||||
axisType: Optional[AxisType] = None
|
||||
plane: Optional[Plane] = None
|
||||
|
||||
@@ -90,7 +90,7 @@ class Property(Base, speckle_type=STRUCTURAL_PROPERTY):
|
||||
name: Optional[str] = None
|
||||
|
||||
|
||||
class SectionProfile(Base, speckle_type=STRUCTURAL_PROPERTY + ".SectionProfile"):
|
||||
class SectionProfile(Base, speckle_type=STRUCTURAL_PROPERTY + ".Profiles.SectionProfile"):
|
||||
name: Optional[str] = None
|
||||
shapeType: Optional[ShapeType] = None
|
||||
area: float = 0.0
|
||||
|
||||
@@ -90,17 +90,18 @@ class BatchSender(object):
|
||||
self._exception = self._exception or ex
|
||||
LOG.error("ServerTransport sending thread error: " + str(ex))
|
||||
|
||||
def _bg_send_batch(self, session, batch):
|
||||
def _bg_send_batch(self, session: requests.Session, batch):
|
||||
object_ids = [obj[0] for obj in batch]
|
||||
try:
|
||||
server_has_object = session.post(
|
||||
url=f"{self.server_url}/api/diff/{self.stream_id}",
|
||||
data={"objects": json.dumps(object_ids)},
|
||||
).json()
|
||||
except Exception as ex:
|
||||
response = session.post(
|
||||
url=f"{self.server_url}/api/diff/{self.stream_id}",
|
||||
data={"objects": json.dumps(object_ids)},
|
||||
)
|
||||
if response.status_code == 403:
|
||||
raise SpeckleException(
|
||||
f"Invalid credentials - cannot send objects to server {self.server_url}"
|
||||
) from ex
|
||||
)
|
||||
response.raise_for_status()
|
||||
server_has_object = response.json()
|
||||
|
||||
new_object_ids = [x for x in object_ids if not server_has_object[x]]
|
||||
new_object_ids = set(new_object_ids)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import random
|
||||
import uuid
|
||||
from urllib.parse import parse_qs, urlparse
|
||||
|
||||
import pytest
|
||||
import requests
|
||||
@@ -38,7 +39,8 @@ def seed_user(host):
|
||||
)
|
||||
if not r.ok:
|
||||
raise Exception(f"Cannot seed user: {r.reason}")
|
||||
access_code = r.text.split("access_code=")[1]
|
||||
redirect_url = urlparse(r.headers.get("location"))
|
||||
access_code = parse_qs(redirect_url.query)["access_code"][0] # type: ignore
|
||||
|
||||
r_tokens = requests.post(
|
||||
url=f"http://{host}/auth/token",
|
||||
|
||||
@@ -44,3 +44,12 @@ def test_accounts_folder_name_override():
|
||||
speckle_path_provider.override_accounts_folder_name(new_folder_name)
|
||||
assert speckle_path_provider._accounts_folder_name == new_folder_name
|
||||
speckle_path_provider.override_accounts_folder_name(old_folder_name)
|
||||
|
||||
|
||||
def test_connector_installation_path():
|
||||
host_application = "test application"
|
||||
connector_path = speckle_path_provider.user_speckle_connector_installation_path(
|
||||
host_application
|
||||
)
|
||||
assert "connector_installations" in str(connector_path)
|
||||
assert str(connector_path).endswith(host_application)
|
||||
|
||||
@@ -106,6 +106,9 @@ fake_bases = [FakeBase("foo"), FakeBase("bar")]
|
||||
True,
|
||||
fake_bases,
|
||||
),
|
||||
(List["int"], [2, 3, 4], True, [2, 3, 4]),
|
||||
(Union[float, Dict[str, float]], {"foo": 1, "bar": 2}, True, {"foo": 1.0, "bar": 2.0}),
|
||||
(Union[float, Dict[str, float]], {"foo": "bar"}, False, {"foo": "bar"}),
|
||||
],
|
||||
)
|
||||
def test_validate_type(
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
"""
|
||||
Provides uniform and consistent path helpers for `specklepy`
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from importlib import import_module, invalidate_caches
|
||||
|
||||
_user_data_env_var = "SPECKLE_USERDATA_PATH"
|
||||
|
||||
|
||||
def _path() -> Optional[Path]:
|
||||
"""Read the user data path override setting."""
|
||||
path_override = os.environ.get(_user_data_env_var)
|
||||
if path_override:
|
||||
return Path(path_override)
|
||||
return None
|
||||
|
||||
|
||||
_application_name = "Speckle"
|
||||
|
||||
|
||||
def override_application_name(application_name: str) -> None:
|
||||
"""Override the global Speckle application name."""
|
||||
global _application_name
|
||||
_application_name = application_name
|
||||
|
||||
|
||||
def override_application_data_path(path: Optional[str]) -> None:
|
||||
"""
|
||||
Override the global Speckle application data path.
|
||||
|
||||
If the value of path is `None` the environment variable gets deleted.
|
||||
"""
|
||||
if path:
|
||||
os.environ[_user_data_env_var] = path
|
||||
else:
|
||||
os.environ.pop(_user_data_env_var, None)
|
||||
|
||||
|
||||
def _ensure_folder_exists(base_path: Path, folder_name: str) -> Path:
|
||||
path = base_path.joinpath(folder_name)
|
||||
path.mkdir(exist_ok=True, parents=True)
|
||||
return path
|
||||
|
||||
|
||||
def user_application_data_path() -> Path:
|
||||
"""Get the platform specific user configuration folder path"""
|
||||
path_override = _path()
|
||||
if path_override:
|
||||
return path_override
|
||||
|
||||
try:
|
||||
if sys.platform.startswith("win"):
|
||||
app_data_path = os.getenv("APPDATA")
|
||||
if not app_data_path:
|
||||
raise Exception(
|
||||
"Cannot get appdata path from environment."
|
||||
)
|
||||
return Path(app_data_path)
|
||||
else:
|
||||
# try getting the standard XDG_DATA_HOME value
|
||||
# as that is used as an override
|
||||
app_data_path = os.getenv("XDG_DATA_HOME")
|
||||
if app_data_path:
|
||||
return Path(app_data_path)
|
||||
else:
|
||||
return _ensure_folder_exists(Path.home(), ".config")
|
||||
except Exception as ex:
|
||||
raise Exception(
|
||||
"Failed to initialize user application data path.", ex
|
||||
)
|
||||
|
||||
|
||||
def user_speckle_folder_path() -> Path:
|
||||
"""Get the folder where the user's Speckle data should be stored."""
|
||||
return _ensure_folder_exists(user_application_data_path(), _application_name)
|
||||
|
||||
|
||||
def user_speckle_connector_installation_path(host_application: str) -> Path:
|
||||
"""
|
||||
Gets a connector specific installation folder.
|
||||
|
||||
In this folder we can put our connector installation and all python packages.
|
||||
"""
|
||||
return _ensure_folder_exists(
|
||||
_ensure_folder_exists(user_speckle_folder_path(), "connector_installations"),
|
||||
host_application,
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
print("Starting module dependency installation")
|
||||
print(sys.executable)
|
||||
|
||||
PYTHON_PATH = sys.executable
|
||||
|
||||
|
||||
|
||||
def connector_installation_path(host_application: str) -> Path:
|
||||
connector_installation_path = user_speckle_connector_installation_path(host_application)
|
||||
connector_installation_path.mkdir(exist_ok=True, parents=True)
|
||||
|
||||
# set user modules path at beginning of paths for earlier hit
|
||||
if sys.path[0] != connector_installation_path:
|
||||
sys.path.insert(0, str(connector_installation_path))
|
||||
|
||||
print(f"Using connector installation path {connector_installation_path}")
|
||||
return connector_installation_path
|
||||
|
||||
|
||||
|
||||
def is_pip_available() -> bool:
|
||||
try:
|
||||
import_module("pip") # noqa F401
|
||||
return True
|
||||
except ImportError:
|
||||
return False
|
||||
|
||||
|
||||
def ensure_pip() -> None:
|
||||
print("Installing pip... ")
|
||||
|
||||
from subprocess import run
|
||||
|
||||
completed_process = run([PYTHON_PATH, "-m", "ensurepip"])
|
||||
|
||||
if completed_process.returncode == 0:
|
||||
print("Successfully installed pip")
|
||||
else:
|
||||
raise Exception(f"Failed to install pip, got {completed_process.returncode} return code")
|
||||
|
||||
|
||||
def get_requirements_path() -> Path:
|
||||
# we assume that a requirements.txt exists next to the __init__.py file
|
||||
path = Path(Path(__file__).parent, "requirements.txt")
|
||||
assert path.exists()
|
||||
return path
|
||||
|
||||
|
||||
def install_requirements(host_application: str) -> None:
|
||||
# set up addons/modules under the user
|
||||
# script path. Here we'll install the
|
||||
# dependencies
|
||||
path = connector_installation_path(host_application)
|
||||
print(f"Installing Speckle dependencies to {path}")
|
||||
|
||||
from subprocess import run
|
||||
|
||||
completed_process = run(
|
||||
[
|
||||
PYTHON_PATH,
|
||||
"-m",
|
||||
"pip",
|
||||
"install",
|
||||
"-t",
|
||||
str(path),
|
||||
"-r",
|
||||
str(get_requirements_path()),
|
||||
],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
|
||||
if completed_process.returncode != 0:
|
||||
m = f"Failed to install dependenices through pip, got {completed_process.returncode} return code"
|
||||
print(m)
|
||||
raise Exception(m)
|
||||
|
||||
|
||||
def install_dependencies(host_application: str) -> None:
|
||||
if not is_pip_available():
|
||||
ensure_pip()
|
||||
|
||||
install_requirements(host_application)
|
||||
|
||||
|
||||
def _import_dependencies() -> None:
|
||||
import_module("specklepy")
|
||||
# the code above doesn't work for now, it fails on importing graphql-core
|
||||
# despite that, the connector seams to be working as expected
|
||||
# But it would be nice to make this solution work
|
||||
# it would ensure that all dependencies are fully loaded
|
||||
# requirements = get_requirements_path().read_text()
|
||||
# reqs = [
|
||||
# req.split(" ; ")[0].split("==")[0].split("[")[0].replace("-", "_")
|
||||
# for req in requirements.split("\n")
|
||||
# if req and not req.startswith(" ")
|
||||
# ]
|
||||
# for req in reqs:
|
||||
# print(req)
|
||||
# import_module("specklepy")
|
||||
|
||||
def ensure_dependencies(host_application: str) -> None:
|
||||
try:
|
||||
install_dependencies(host_application)
|
||||
invalidate_caches()
|
||||
_import_dependencies()
|
||||
print("Successfully found dependencies")
|
||||
except ImportError:
|
||||
raise Exception(f"Cannot automatically ensure Speckle dependencies. Please try restarting the host application {host_application}!")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user