Compare commits

..

42 Commits

Author SHA1 Message Date
KatKatKateryna 99e9f773b8 Merge pull request #269 from specklesystems/objects/collections
Added collection object
2023-05-05 23:25:56 +08:00
KatKatKateryna 189a5847cf Update other.py 2023-05-05 15:12:00 +01:00
KatKatKateryna eb86b4881a Merge branch 'main' into objects/collections 2023-05-05 14:49:26 +01:00
KatKatKateryna 64fca5f280 Collections moved to Core 2023-05-05 14:48:26 +01:00
Gergő Jedlicska c7f5e0718b Merge pull request #267 from RobClaessensRHDHV/feature/suppress_type_error_during_type_check
Fix: also suppress TypeError during type checking.
2023-04-06 19:00:08 +02:00
908599 d2685c5cf5 Add unit tests for type validation of Union with float and dict. 2023-04-06 17:28:55 +02:00
908599 1b83e5a84b Merge branch 'main' of https://github.com/RobClaessensRHDHV/specklepy into feature/suppress_type_error_during_type_check 2023-04-06 17:10:18 +02:00
Gergő Jedlicska 77e09b9780 Merge pull request #266 from specklesystems/jrm/one-installer-for-all
Added Installer
2023-04-06 17:03:15 +02:00
Gergő Jedlicska 402f750200 Merge pull request #270 from specklesystems/gergo/fix_tests
gergo/fix tests
2023-04-06 17:02:11 +02:00
Gergő Jedlicska a43e7471a4 style(formatting): fix formatting issues 2023-04-06 16:57:44 +02:00
Gergő Jedlicska a4ed7ebb08 fix(integration-tests): fix integration tests 2023-04-06 16:56:21 +02:00
Jedd Morgan e7eb7c67a9 Added collection object 2023-04-06 14:14:29 +01:00
908599 d547cdaac0 Fix to also suppress TypeError during type checking.
This currently gives an error when float conversion is attempted with a non-numeric value.
2023-04-06 10:13:19 +02:00
Jedd Morgan 6f35c1bd20 Added Installer 2023-03-28 17:04:46 +01:00
Jedd Morgan 420c73f484 Merge pull request #265 from specklesystems/gergo/connector_install_path
feat(path-provider): add connector installation path helper
2023-03-28 13:47:56 +01:00
Gergő Jedlicska c2859475cc feat(path-provider): add connector installation path helper 2023-03-28 13:31:57 +02:00
Gergő Jedlicska 56e8d65e2b Merge pull request #264 from specklesystems/jsdbroughton-patch-1
fix: `invite_batch` failing where `emails` or `user_ids` is not provided
2023-03-22 18:32:41 +01:00
Jonathon Broughton 7885a6be8d fix: NoneType is not iterable 2023-03-21 17:46:16 +00:00
Jedd Morgan b19b85c9d1 Merge pull request #263 from specklesystems/jrm/instances
Updated instances to match sharp 2.13 changes
2023-03-21 13:12:50 +00:00
Jedd Morgan db4b2b7f87 Removed new collections class as we are not ready 2023-03-20 15:16:03 +00:00
Jedd Morgan 77916995bc Updated instances to match sharp 2.13 changes 2023-03-19 03:11:10 +00:00
Gergő Jedlicska 3dd56dc38e Merge pull request #260 from specklesystems/gergo/forward_ref_type
gergo/forward ref type
2023-02-15 20:48:55 +01:00
Gergő Jedlicska ae42bec1c3 style(formatting): rerun formatting 2023-02-15 19:21:48 +01:00
Gergő Jedlicska ea7baf8eb5 fix(type_checking): make sure forwardrefs blank pass type checking 2023-02-15 19:20:45 +01:00
Gergő Jedlicska 8352bb5c9a Merge pull request #257 from Knuttatutta/knuttatutta/add_axis_type
Fix: Add AxisType and fix its type in Axis
2023-02-01 15:27:34 +02:00
Gergő Jedlicska fc34b876fd Merge branch 'main' into knuttatutta/add_axis_type 2023-02-01 15:26:10 +02:00
Gergő Jedlicska 183993cfc5 Merge pull request #254 from RobClaessensRHDHV/feature/revit_analytical_to_specklepy_structural_fixes
Fix: Interoperability from Revit analytical to specklepy structural
2023-02-01 15:24:45 +02:00
Knuttatutta 9be3b4b93d Added AxisType in module init 2023-02-01 10:34:01 +01:00
Knuttatutta 0b14660115 Add AxisType, fix type in Axis 2023-02-01 10:16:13 +01:00
908599 68c4c682a0 Revert incorrect type hint change 2023-01-17 10:46:31 +01:00
908599 4f93ddcaf3 Update speckle_type of SectionProfile to match C#, update type hints with C# Enum values to int.
For the SectionProfile, a separate static variable e.g. STRUCTURAL_PROFILE could be created later, I now just made an easy fix.
For the enumeration attributes, I believe they should be integer, as e.g. Revit pushes them as integers, not strings.
2023-01-16 16:01:33 +01:00
Gergő Jedlicska e842f651b9 Merge pull request #253 from RobClaessensRHDHV/feature/structural_property_bugfix
Typo fix in properties.py
2023-01-16 13:53:44 +01:00
908599 7e1bec1aba Typo fix in properties.py 2023-01-16 11:39:41 +01:00
Gergő Jedlicska 1fb9a4f5fe Merge pull request #252 from specklesystems/gergo/set_type_fix
fix(typing-system): add set type into type validation
2023-01-11 16:10:28 +01:00
Gergő Jedlicska 1668c80bed fix(typing-system): add set type into type validation 2023-01-11 16:07:04 +01:00
Gergő Jedlicska ac6ba87c68 Merge pull request #251 from specklesystems/gergo/unionTriples
fix(type-validation): fix union types with more than 2 arguments
2023-01-10 13:02:36 +01:00
Gergő Jedlicska 3db8565f57 fix(type-validation): fix union types with more than 2 arguments 2023-01-10 12:58:19 +01:00
Alan Rynne a32822f4e3 ci: Updated github actions to use new actions repo 2023-01-09 20:42:24 +01:00
Gergő Jedlicska 40956927c8 Merge pull request #250 from specklesystems/gergo/nonGenericTFix
fix(typing): fix non generic typedefed lists and tuples
2023-01-09 15:54:36 +01:00
Gergő Jedlicska 4628f111ba fix(type-checking): fix py >= 3.9 dict type checking 2023-01-09 15:53:15 +01:00
Gergő Jedlicska 9c952b432d fix(typing): fix non specificed generic types for py 3.7 py 3.8 2023-01-09 15:39:45 +01:00
Gergő Jedlicska f075988e4b fix(typing): fix non generic typedefed lists and tuples 2023-01-09 15:10:23 +01:00
16 changed files with 409 additions and 150 deletions
+4 -69
View File
@@ -6,72 +6,7 @@ on:
jobs:
update_issue:
runs-on: ubuntu-latest
steps:
- name: Get project data
env:
GITHUB_TOKEN: ${{secrets.GHPROJECT_TOKEN}}
ORGANIZATION: specklesystems
PROJECT_NUMBER: 9
run: |
gh api graphql --header 'GraphQL-Features: projects_next_graphql' -f query='
query($org: String!, $number: Int!) {
organization(login: $org){
projectNext(number: $number) {
id
fields(first:20) {
nodes {
id
name
settings
}
}
}
}
}' -f org=$ORGANIZATION -F number=$PROJECT_NUMBER > project_data.json
echo 'PROJECT_ID='$(jq '.data.organization.projectNext.id' project_data.json) >> $GITHUB_ENV
echo 'STATUS_FIELD_ID='$(jq '.data.organization.projectNext.fields.nodes[] | select(.name== "Status") | .id' project_data.json) >> $GITHUB_ENV
echo "$PROJECT_ID"
echo "$STATUS_FIELD_ID"
echo 'DONE_ID='$(jq '.data.organization.projectNext.fields.nodes[] | select(.name== "Status") | .settings | fromjson | .options[] | select(.name== "Done") | .id' project_data.json) >> $GITHUB_ENV
echo "$DONE_ID"
- name: Add Issue to project #it's already in the project, but we do this to get its node id!
env:
GITHUB_TOKEN: ${{secrets.GHPROJECT_TOKEN}}
ISSUE_ID: ${{ github.event.issue.node_id }}
run: |
item_id="$( gh api graphql --header 'GraphQL-Features: projects_next_graphql' -f query='
mutation($project:ID!, $id:ID!) {
addProjectNextItem(input: {projectId: $project, contentId: $id}) {
projectNextItem {
id
}
}
}' -f project=$PROJECT_ID -f id=$ISSUE_ID --jq '.data.addProjectNextItem.projectNextItem.id')"
echo 'ITEM_ID='$item_id >> $GITHUB_ENV
- name: Update Status
env:
GITHUB_TOKEN: ${{secrets.GHPROJECT_TOKEN}}
ISSUE_ID: ${{ github.event.issue.node_id }}
run: |
gh api graphql --header 'GraphQL-Features: projects_next_graphql' -f query='
mutation($project:ID!, $status:ID!, $id:ID!, $value:String!) {
set_status: updateProjectNextItemField(
input: {
projectId: $project
itemId: $id
fieldId: $status
value: $value
}
) {
projectNextItem {
id
}
}
}' -f project=$PROJECT_ID -f status=$STATUS_FIELD_ID -f id=$ITEM_ID -f value=${{ env.DONE_ID }}
uses: specklesystems/github-actions/.github/workflows/project-add-issue.yml@main
secrets: inherit
with:
issue-id: ${{ github.event.issue.node_id }}
+4 -42
View File
@@ -6,45 +6,7 @@ on:
jobs:
track_issue:
runs-on: ubuntu-latest
steps:
- name: Get project data
env:
GITHUB_TOKEN: ${{secrets.GHPROJECT_TOKEN}}
ORGANIZATION: specklesystems
PROJECT_NUMBER: 9
run: |
gh api graphql --header 'GraphQL-Features: projects_next_graphql' -f query='
query($org: String!, $number: Int!) {
organization(login: $org){
projectNext(number: $number) {
id
fields(first:20) {
nodes {
id
name
settings
}
}
}
}
}' -f org=$ORGANIZATION -F number=$PROJECT_NUMBER > project_data.json
echo 'PROJECT_ID='$(jq '.data.organization.projectNext.id' project_data.json) >> $GITHUB_ENV
echo 'STATUS_FIELD_ID='$(jq '.data.organization.projectNext.fields.nodes[] | select(.name== "Status") | .id' project_data.json) >> $GITHUB_ENV
- name: Add Issue to project
env:
GITHUB_TOKEN: ${{secrets.GHPROJECT_TOKEN}}
ISSUE_ID: ${{ github.event.issue.node_id }}
run: |
item_id="$( gh api graphql --header 'GraphQL-Features: projects_next_graphql' -f query='
mutation($project:ID!, $id:ID!) {
addProjectNextItem(input: {projectId: $project, contentId: $id}) {
projectNextItem {
id
}
}
}' -f project=$PROJECT_ID -f id=$ISSUE_ID --jq '.data.addProjectNextItem.projectNextItem.id')"
echo 'ITEM_ID='$item_id >> $GITHUB_ENV
uses: specklesystems/github-actions/.github/workflows/project-add-issue.yml@main
secrets: inherit
with:
issue-id: ${{ github.event.issue.node_id }}
+3 -2
View File
@@ -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
}
+17 -6
View File
@@ -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)
+5 -4
View File
@@ -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)
+38 -6
View File
@@ -5,6 +5,7 @@ from typing import (
Any,
ClassVar,
Dict,
ForwardRef,
List,
Optional,
Set,
@@ -217,25 +218,37 @@ 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)
# recursive validation for Unions on both types preferring the fist type
if origin is Union:
t_1, t_2 = t.__args__ # type: ignore
# below is what in nicer for >= py38
# t_1, t_2 = get_args(t)
t_1_success, t_1_value = _validate_type(t_1, value)
if t_1_success:
return True, t_1_value
return _validate_type(t_2, value)
args = t.__args__ # type: ignore
for arg_t in args:
t_success, t_value = _validate_type(arg_t, value)
if t_success:
return True, t_value
return False, value
if origin is dict:
if not isinstance(value, dict):
return False, value
if value == {}:
return True, value
if not getattr(t, "__args__", None):
return True, value
t_key, t_value = t.__args__ # type: ignore
if (
getattr(t_key, "__name__", None),
getattr(t_value, "__name__", None),
) == ("KT", "VT"):
return True, value
# we're only checking the first item, but the for loop and return after
# evaluating the first item is the fastest way
for dict_key, dict_value in value.items():
@@ -251,7 +264,11 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
return False, value
if value == []:
return True, value
if not hasattr(t, "__args__"):
return True, value
t_items = t.__args__[0] # type: ignore
if getattr(t_items, "__name__", None) == "T":
return True, value
first_item_valid, _ = _validate_type(t_items, value[0])
if first_item_valid:
return True, value
@@ -260,7 +277,11 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
if origin is tuple:
if not isinstance(value, tuple):
return False, value
if not hasattr(t, "__args__"):
return True, value
args = t.__args__ # type: ignore
if args == tuple():
return True, value
# we're not checking for empty tuple, cause tuple lengths must match
if len(args) != len(value):
return False, value
@@ -272,10 +293,21 @@ def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
values.append(item_value)
return True, tuple(values)
if origin is set:
if not isinstance(value, set):
return False, value
if not hasattr(t, "__args__"):
return True, value
t_items = t.__args__[0] # type: ignore
first_item_valid, _ = _validate_type(t_items, next(iter(value)))
if first_item_valid:
return True, value
return False, value
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
+48 -7
View File
@@ -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
@@ -255,3 +289,10 @@ class RevitParameter(Base, speckle_type="Objects.BuiltElements.Revit.Parameter")
isShared: bool = False
isReadOnly: bool = False
isTypeParameter: bool = False
class Collection(
Base, speckle_type="Speckle.Core.Models.Collection", detachable={"elements"}
):
name: Optional[str] = None
collectionType: Optional[str] = None
elements: Optional[List[Base]] = None
+5 -1
View File
@@ -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",
+8 -1
View File
@@ -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
@@ -5,7 +5,7 @@ from specklepy.objects.base import Base
from specklepy.objects.structural.axis import Axis
from specklepy.objects.structural.materials import StructuralMaterial
STRUCTURAL_PROPERTY = "Objectives.Structural.Properties"
STRUCTURAL_PROPERTY = "Objects.Structural.Properties"
class MemberType(int, Enum):
@@ -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)
+3 -1
View File
@@ -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",
+9
View File
@@ -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)
+35 -1
View File
@@ -1,5 +1,5 @@
from enum import Enum, IntEnum
from typing import Any, Dict, List, Optional, Tuple
from typing import Any, Dict, List, Optional, Set, Tuple, Union
import pytest
@@ -18,6 +18,16 @@ class FakeIntEnum(IntEnum):
one = 1
class FakeBase(Base):
foo: Optional[str]
def __init__(self, foo: str) -> None:
self.foo = foo
fake_bases = [FakeBase("foo"), FakeBase("bar")]
@pytest.mark.parametrize(
"input_type, value, is_valid, return_value",
[
@@ -64,7 +74,9 @@ class FakeIntEnum(IntEnum):
# same as the dict typing below...
(List[int], [None, 2], True, [None, 2]),
(List[Optional[int]], [None, 2], True, [None, 2]),
(List, ["foo", 2, "bar"], True, ["foo", 2, "bar"]),
(Dict[str, int], {"foo": 1}, True, {"foo": 1}),
(Dict, {"foo": 1}, True, {"foo": 1}),
(Dict[str, Optional[int]], {"foo": None}, True, {"foo": None}),
# this case should be
# (Dict[int, Base], {1: None}, False, {1: None}),
@@ -72,9 +84,31 @@ class FakeIntEnum(IntEnum):
(Dict[int, Base], {1: None}, True, {1: None}),
(Dict[int, Base], {1: test_base}, True, {1: test_base}),
(Tuple[int, str, str], (1, "foo", "bar"), True, (1, "foo", "bar")),
(Tuple, (1, "foo", "bar"), True, (1, "foo", "bar")),
# given our current rules, this is the reality. Its just sad...
(Tuple[str, str, str], (1, "foo", "bar"), True, ("1", "foo", "bar")),
(Tuple[str, Optional[str], str], (1, None, "bar"), True, ("1", None, "bar")),
(Set[bool], set([1, 2]), False, set([1, 2])),
(Set[int], set([1, 2]), True, set([1, 2])),
(Set[int], set([None, 2]), True, set([None, 2])),
# not testing this, since order of input iterables in sets are not preserved
# easily produces false reports since we're only checking the type of the
# first item
# (Set[int], set(["None", 2]), False, set(["None", 2])),
(Set[Optional[int]], set([None, 2]), True, set([None, 2])),
(Optional[Union[List[int], List[FakeBase]]], None, True, None),
(Optional[Union[List[int], List[FakeBase]]], "foo", False, "foo"),
(Union[List[int], List[FakeBase], None], "foo", False, "foo"),
(Optional[Union[List[int], List[FakeBase]]], [1, 2, 3], True, [1, 2, 3]),
(
Optional[Union[List[int], List[FakeBase]]],
fake_bases,
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(
+207
View File
@@ -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}!")