Compare commits

..

192 Commits

Author SHA1 Message Date
Gergő Jedlicska fbbd6c0dd7 Merge pull request #303 from specklesystems/gergo/speckle_automate
feat: add speckle automate package with some basic sanity tests
2023-09-20 10:06:10 +02:00
Gergő Jedlicska 8ffe219111 chore: update deps 2023-09-19 20:22:57 +02:00
Gergő Jedlicska e4d087db3a fix: we still support py38 2023-09-19 20:21:46 +02:00
Gergő Jedlicska 2e8943e961 feat: test against latest 2023-09-19 20:18:47 +02:00
Gergő Jedlicska f254defc6b feat: add speckle automate package with some basic sanity tests 2023-09-19 20:11:32 +02:00
KatKatKateryna 948a56a07f Merge pull request #300 from specklesystems/2.16_backwards-compatibility
add deprecated gis classes
2023-09-18 13:07:19 +01:00
KatKatKateryna 3eed9a60fa add deprecated gis classes 2023-09-08 19:28:14 +01:00
Jedd Morgan c169c4eeda Merge pull request #299 from specklesystems/jrm/units/units-brep-fix
fix(objects)): Fixed issue where breps were incorrectly setting their…
2023-09-08 13:45:46 +01:00
Jedd Morgan 32b5ef88a1 fix(tests): fixed unit test for brep serialization 2023-09-08 13:43:50 +01:00
Jedd Morgan 3a979318ad fix(objects)): Fixed issue where breps were incorrectly setting their units 2023-09-07 17:33:01 +01:00
Gergő Jedlicska 1e6321c7f1 Merge pull request #296 from specklesystems/gergo/abstract_transport_no_pydantic
fix(AbstractTransport-and-subclasses): abstract transport and its subclasses should not be pydantic models
2023-09-07 13:26:47 +02:00
Gergő Jedlicska b5fb684864 Merge branch 'main' into gergo/abstract_transport_no_pydantic 2023-09-07 13:22:15 +02:00
Gergő Jedlicska 65048cd01b Merge pull request #290 from specklesystems/gergo/allowUnsupportedUnits
allow string units
2023-09-07 13:19:25 +02:00
Jedd Morgan 9d2fd5bc42 Merge branch 'main' into gergo/allowUnsupportedUnits 2023-09-07 12:17:19 +01:00
Jedd Morgan bd35fb59c3 Merge pull request #295 from specklesystems/jrm/unit-scale-factor
Unit scaling
2023-09-07 12:17:01 +01:00
Gergő Jedlicska 4931c95d7c Merge pull request #291 from specklesystems/2.16
2.16 - add classes for Topography and Tables without geometry
2023-09-07 13:15:16 +02:00
Gergő Jedlicska 52d53db661 fix(AbstractTransport-and-subclasses): abstract transport and its subclasses should not be pydantic models
Pydantic is a validation and parsing library, its supposed to sit at the edge of apps, to make sure the data transferred in and out is valid. Its not meant to be a generic python base class.
2023-09-06 13:43:05 +02:00
Jedd Morgan 23ee28f851 Merge branch 'gergo/allowUnsupportedUnits' into jrm/unit-scale-factor 2023-09-06 12:39:35 +01:00
Jedd Morgan 791190a38c Merge branch 'main' into gergo/allowUnsupportedUnits 2023-09-06 12:36:45 +01:00
Jedd Morgan 3c7feb0bec Merge branch 'main' into jrm/unit-scale-factor 2023-09-06 12:36:03 +01:00
Jedd Morgan 2b583fd822 Added unit scaling functions in units.py 2023-09-06 12:34:16 +01:00
KatKatKateryna 8244e3ecc7 remove redundant units 2023-09-06 11:31:54 +01:00
KatKatKateryna 5ac9d80cbc fix 2023-09-06 11:28:55 +01:00
KatKatKateryna 5e2fbaa7c2 replace init for GIS classes 2023-09-06 11:14:46 +01:00
KatKatKateryna 703ceaf369 naming 2023-09-05 15:02:47 +01:00
Gergő Jedlicska a5096c41ca Merge pull request #293 from specklesystems/metrics_rename
SDK Action metrics rename
2023-09-05 14:47:31 +02:00
Jedd Morgan 972339454d Update base.py 2023-09-05 12:50:11 +01:00
KatKatKateryna 34c11d5931 SDK Action 2023-09-05 09:51:36 +01:00
Jedd Morgan 854ce9f77f Merge branch 'main' into gergo/allowUnsupportedUnits 2023-09-04 21:02:54 +01:00
Jedd Morgan 7f926cf547 Merge pull request #292 from specklesystems/2.16-hotfixes
2.16 hotfixes
2023-09-04 21:02:37 +01:00
KatKatKateryna 5e8b54e3b7 import Core client by default 2023-09-04 20:09:57 +01:00
KatKatKateryna 8bd46e4e64 Revert "add metrics keyword for Connector Action"
This reverts commit 0cb6c7f682.
2023-09-04 19:57:36 +01:00
KatKatKateryna 91edd4f85b add metrics keyword for Connector Action 2023-09-04 19:57:16 +01:00
KatKatKateryna 0cb6c7f682 add metrics keyword for Connector Action 2023-09-04 16:17:48 +01:00
Gergő Jedlicska 125a4bbeed Merge pull request #288 from specklesystems/jrm/graph-traversal
feat: Added graph traversal
2023-08-31 16:05:54 +02:00
Jedd Morgan 76c4074aed Poetry lock 2023-08-31 15:05:19 +01:00
Jedd Morgan 16164a57da Merge branch 'main' into jrm/graph-traversal 2023-08-31 15:04:29 +01:00
Jedd Morgan 3a225fa935 attrs class 2023-08-31 15:01:59 +01:00
Gergő Jedlicska 102850b894 allow string units 2023-08-30 15:56:34 +02:00
KatKatKateryna 5ac85c541b add Topography class 2023-08-25 18:28:06 +01:00
Gergő Jedlicska cca7b18119 Merge pull request #289 from specklesystems/gergo/pydantic2
gergo/pydantic2
2023-08-14 11:42:28 +02:00
Gergő Jedlicska 8a34b95128 remove deprecated user resources 2023-08-14 11:41:37 +02:00
Gergő Jedlicska 46d7abbaee migrate away from deprecated pydantic method 2023-08-14 11:28:08 +02:00
Gergő Jedlicska 67e95caf5f Merge pull request #286 from mortenengen/upgrade-to-pydantic2
chore: upgrade to pydantic 2.0
2023-08-14 11:13:30 +02:00
Morten Engen 04532ed645 Add defaults to optional model fields in core 2023-08-01 12:45:11 +02:00
Morten Engen 7df175d9bb Replace deprecated model val methods in core 2023-08-01 12:44:11 +02:00
Morten Engen 3912fa8860 Merge branch 'main' into upgrade-to-pydantic2 2023-08-01 12:27:42 +02:00
KatKatKateryna 34de2928ae non-geometry GIS class 2023-07-28 13:56:09 +01:00
KatKatKateryna 8a91260887 Merge pull request #287 from specklesystems/2.15-hotfix
fully separate core metrics
2023-07-26 16:19:04 +01:00
KatKatKateryna 88eea00787 duplicate _init_resources to non-core (so the resources will also follow sdk-metrics) 2023-07-26 13:16:07 +01:00
KatKatKateryna c57d57c009 return user resource 2023-07-25 12:56:08 +01:00
KatKatKateryna 708e3329e3 reverted changes, user prop removed 2023-07-24 16:15:43 +01:00
KatKatKateryna f0e68845c0 Revert "might or might not be a good way to warn about no more accessible 'user' methods"
This reverts commit 1ef9b91e82.
2023-07-24 16:12:28 +01:00
KatKatKateryna 434a4376b3 Revert "redirect deprecated methods / methods with old variables set - to 'other_user'"
This reverts commit c99c25e848.
2023-07-24 16:12:23 +01:00
KatKatKateryna d701bedcc7 Revert "move deprecated methods to core"
This reverts commit 3962126b54.
2023-07-24 16:12:20 +01:00
KatKatKateryna 6238150bd5 Revert "typo"
This reverts commit 3e41e8cd8e.
2023-07-24 16:12:18 +01:00
KatKatKateryna 3e41e8cd8e typo 2023-07-24 15:43:57 +01:00
KatKatKateryna 3962126b54 move deprecated methods to core 2023-07-24 15:41:32 +01:00
KatKatKateryna c99c25e848 redirect deprecated methods / methods with old variables set - to 'other_user' 2023-07-24 13:50:28 +01:00
KatKatKateryna 1ef9b91e82 might or might not be a good way to warn about no more accessible 'user' methods 2023-07-24 13:29:00 +01:00
KatKatKateryna c0cfe1471a return import of internal function 2023-07-21 17:18:52 +01:00
KatKatKateryna da838280c3 naming convention, inefficient imports, deprecated methods 2023-07-21 16:32:38 +01:00
KatKatKateryna 681872e5ff typo 2023-07-17 17:58:49 +01:00
KatKatKateryna e11c41e0f8 fixing mixed attribute types 2023-07-17 17:48:16 +01:00
Jedd Morgan ec651a9237 traversal 2023-07-17 14:39:10 +01:00
KatKatKateryna ece957fb0f limit http response length 2023-07-12 13:51:10 +01:00
KatKatKateryna 5338d8ac0f displaying full error message on send 2023-07-12 13:19:45 +01:00
KatKatKateryna e36ea70e8a fully separate core metrics 2023-07-12 00:08:26 +01:00
Morten Engen edf2afaa89 Update poetry.lock 2023-07-07 13:45:45 +02:00
Morten Engen e0b1b272c0 Update pyproject.toml 2023-07-07 13:06:49 +02:00
Morten Engen 682e82057e Replace deprecated model validation methods 2023-07-07 13:05:24 +02:00
Morten Engen 473e5cfddb Store model config in model attribute 2023-07-07 13:04:46 +02:00
Morten Engen 03cd989165 Add defaults to optional model fields 2023-07-07 13:04:14 +02:00
KatKatKateryna 284d841a1e Merge pull request #285 from specklesystems/gergo/lockResourceExecution
fix(resource): make sure no threading contention with the client can happen
2023-06-30 20:45:54 +08:00
Gergő Jedlicska 668fc5131f fix(resource): make sure no threading contention with the client can happen 2023-06-30 14:42:23 +02:00
Jedd Morgan 64926bd41d Merge pull request #279 from specklesystems/kate/separate_metrics
Kate/separate metrics
2023-06-27 15:53:43 +01:00
Jedd Morgan 1c9b186ea5 Merge pull request #275 from ciga2011/main
Fix Pointcloud bugs in geometry.py
2023-06-26 15:54:19 +01:00
KatKatKateryna ed9f1ad818 rotation 2023-06-21 17:21:50 +01:00
KatKatKateryna b83c30a1c9 added offsets to crs class 2023-06-21 12:45:18 +01:00
Gergő Jedlicska c079342a55 Merge pull request #273 from mortenengen/limit-batch-length
Fix: limit number of objects per batch sent to server
2023-06-14 14:03:16 +02:00
Gergő Jedlicska 6aa29d9b30 Merge branch 'main' into limit-batch-length 2023-06-14 13:58:57 +02:00
KatKatKateryna f456e4ddbb rename 2023-06-13 17:53:03 +01:00
KatKatKateryna dcd13224d0 added native_units for Degrees 2023-06-13 17:48:03 +01:00
KatKatKateryna 06952a5991 more specific domain identification 2023-06-13 12:17:27 +01:00
KatKatKateryna 6049049813 fixed server_id 2023-06-12 20:05:47 +01:00
Alan Rynne 75b7d30bd6 Merge branch 'main' into kate/separate_metrics 2023-06-12 15:35:34 +02:00
Alan Rynne 19e26318fc fix(ci): Integration tests now use new docker-compose file (#283)
* fix(ci): Initial test with new docker-compose file

* fix(ci): Don't use python orb

* fix(ci): Use python 3

* fix(ci): Use python3 everywhere

* fix(ci): whitespace PEBKAC

* fix(ci): use included pyenv installation

* fix(ci): Skip pyenv version if its the current one

* fix(ci): Reapply matrix build for integration testing

* fix(ci): Run exec $SHELL

* fix(ci): Removed exec step

* fix(ci): Remove test-old job
2023-06-09 11:46:03 +02:00
Matteo Cominetti 798dc7ff6a Merge pull request #282 from specklesystems/alan/ci/remove-github-actions
chore(ci): Remove old github actions from specklepy repo
2023-06-08 13:59:14 +01:00
Alan Rynne 0e4cff5904 chore(ci): Remove old github actions from specklepy repo 2023-06-08 10:53:02 +02:00
KatKatKateryna d1502c9072 add GIS classes 2023-06-02 20:03:45 +01:00
KatKatKateryna 869629e2a3 return var for unchanged classes 2023-06-02 19:31:35 +01:00
KatKatKateryna 48b98294fb remove constant var from a child class 2023-06-02 19:23:03 +01:00
KatKatKateryna 8205180e5d _untracked_receive referenced from core function 2023-06-02 13:27:25 +01:00
KatKatKateryna a2fd21f541 Metrics renamed to SDK Actions 2023-06-02 13:18:20 +01:00
KatKatKateryna 08ac76cf09 untracked API to Core 2023-06-01 23:48:04 +01:00
Gergő Jedlicska fbf19420fa Merge pull request #278 from specklesystems/gergo/structural_shape_type
fix(structural-properties): add missing ShapeType member
2023-05-30 18:11:42 +02:00
Gergő Jedlicska 44336addaf fix(structural-properties): add missing ShapeType member 2023-05-30 18:02:25 +02:00
ciga2011 43c9a9cace Update geometry.py
Pointcloud properties should be chunkable.
2023-05-11 16:07:58 +08:00
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
Morten Engen 784e9c1326 batch_sender: limit number of objects per batch 2023-04-18 15:13:39 +02: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
Gergő Jedlicska 65c829404a Merge pull request #247 from specklesystems/gergo/pathingUnification
feat(path-handling): rework path handling to match behavior of Core
2023-01-06 15:20:28 +01:00
Jedd Morgan 85e7a72524 fix(tests): Fixed regex escape on path tests 2023-01-06 14:00:19 +00:00
Gergő Jedlicska 0533aa0139 Merge pull request #248 from specklesystems/gergo/updatePypiAuth
;51R;51R
2023-01-05 16:18:22 +01:00
Gergő Jedlicska 04b733a241 ci(circleci): update pypi context usage 2023-01-05 16:15:49 +01:00
Gergő Jedlicska 01036c0f2e test(unit-tests): fix unit tests after merge 2023-01-05 11:06:11 +01:00
Gergő Jedlicska 4d8ca534fe Merge branch 'main' of github.com:specklesystems/specklepy into gergo/pathingUnification 2023-01-05 11:05:03 +01:00
Gergő Jedlicska e941efd95c refactor(tests): split into unit and integration tests folder 2023-01-05 10:59:43 +01:00
Gergő Jedlicska be9defbfa9 Merge pull request #245 from specklesystems/gergo/fully_qualified_names
feat(Base): rework speckle_type handling to better align with Core
2023-01-05 10:55:32 +01:00
Gergő Jedlicska dd54c69554 feat(path-handling): rework path handling to match behavior of Core 2023-01-05 10:41:39 +01:00
Gergő Jedlicska 93c1ec9556 Merge pull request #240 from ItsPatrickHe/bugfix/results_default_values
Update default value of results from 0.0 to None to align with the default value in speckle-sharp
2023-01-04 10:31:18 +01:00
Gergő Jedlicska 659c57e840 Merge branch 'main' of github.com:specklesystems/specklepy into pr/ItsPatrickHe/240 2023-01-04 10:27:38 +01:00
Gergő Jedlicska 1cdd4ffc9c feat(Base): rework speckle_type handling to better align with Core
use a dotnet `FullName` like behavior to deremine the speckle_type

fixes #231
2023-01-04 10:15:36 +01:00
Gergő Jedlicska 5ae022d2ed Merge pull request #244 from specklesystems/gergo/fixNoneInListSerialization
fix(BaseObjectSerializer): fix converting None in list to the string "None"
2022-12-21 16:04:33 +01:00
Gergő Jedlicska 31ca59cea8 fix(BaseObjectSerializer): fix converting None in list to the string "None" 2022-12-21 16:00:49 +01:00
Jinxuan He c91f673dba Add optional for float, regarding type checking 2022-12-21 12:38:21 +01:00
Gergő Jedlicska e2c8ef1b3d Merge pull request #239 from mortenengen/update-pydantic-dep
fix: update minimum required version of pydantic
2022-12-21 12:05:44 +01:00
Gergő Jedlicska b6b0a5a3a0 Merge pull request #242 from specklesystems/gergo/pre-commit
gergo/pre commit
2022-12-21 12:00:45 +01:00
Gergő Jedlicska f36d63a2cf Merge pull request #241 from specklesystems/speckle_type_sync_with_core
speckle type sync with core
2022-12-21 11:59:39 +01:00
Gergő Jedlicska afb9065fb9 fix(metrics): fix missing default arg to track function 2022-12-20 14:53:59 +01:00
Gergő Jedlicska fcc33f8989 style(all-project): fix all linting errors across the project with the current setup 2022-12-20 14:46:36 +01:00
Gergő Jedlicska 2cf9b64221 style(server-transport): fix import export 2022-12-20 10:47:48 +01:00
Gergő Jedlicska 990cf4eb2f style(whole-project): fixing linting and typing errors 2022-12-20 10:45:22 +01:00
Gergő Jedlicska b25f2ab4bc style(whole-project): fix styling from new pre-commit config 2022-12-19 14:03:20 +01:00
Gergő Jedlicska a8786c126d build(pre-commit-config.yaml): add basic pre-commit config 2022-12-19 14:02:42 +01:00
Gergő Jedlicska a35f8936ca bump: version 2.9.1 → 2.9.2 2022-12-19 12:44:11 +01:00
Gergő Jedlicska d965cc0988 add pre-commit dependency 2022-12-19 12:37:20 +01:00
Gergő Jedlicska 59a6950eed fix type checking error message 2022-12-16 22:01:56 +01:00
Gergő Jedlicska 6804282fac import ordering 2022-12-16 21:45:47 +01:00
Gergő Jedlicska a3a22a43d5 formatting fixes 2022-12-16 21:45:24 +01:00
Gergő Jedlicska 08aaa41b6c fix units 2022-12-16 21:45:04 +01:00
Gergő Jedlicska 07a3213ee9 fix objects import 2022-12-16 21:21:13 +01:00
Gergő Jedlicska 7a46176803 additional test cases for type validation 2022-12-16 21:17:39 +01:00
Jinxuan He b75501addd Update default value from 0.0 to None to align them with the default value in speckle-sharp 2022-12-16 15:42:59 +01:00
Gergő Jedlicska 15636dbe62 rework type checking in base 2022-12-16 13:41:07 +01:00
Gergő Jedlicska 9a2061d900 add missing object definitions from core 2022-12-16 13:40:43 +01:00
Morten Engen 8dc51fb936 Update min required version of pydantic 2022-12-16 12:30:56 +01:00
Gergő Jedlicska e805b8ac6e style(all): sort imports with isort 2022-12-09 20:48:02 +01:00
Gergő Jedlicska 5a1d624979 refactor(objects): fix import structure and typing errors for objects 2022-12-09 20:47:16 +01:00
Gergő Jedlicska c8808b07b3 fix(objects): update structural objects to better match core 2022-12-09 19:46:48 +01:00
Gergő Jedlicska 1a9b847c44 test(base): update base tests to properly test the speckle_type generation 2022-12-09 19:46:09 +01:00
Gergő Jedlicska f01fcb8e66 fix(base): update speckle_type creation to better match Core 2022-12-09 19:45:20 +01:00
Gergő Jedlicska 67499ab20c Merge pull request #219 from specklesystems/gergo/typeFixes
typing and formatting fixes
2022-12-09 12:36:07 +01:00
Gergő Jedlicska ebb703e68d Merge branch 'main' of github.com:specklesystems/specklepy into gergo/typeFixes 2022-12-08 14:27:42 +01:00
Gergő Jedlicska ef5d41da5d Merge pull request #233 from ItsPatrickHe/bugfix/property_attr_fix
Align the attribute names for Property1D, Property2D and Property3D w…
2022-12-08 14:26:36 +01:00
Gergő Jedlicska 851dd9c482 Merge pull request #234 from specklesystems/gergo/ciFix
ci(circleci): force integration test server to use localhost ipv4 adresses
2022-12-08 14:01:07 +01:00
Gergő Jedlicska 78074cf691 ci(circleci): force integration test server to use localhost ipv4 adresses 2022-12-08 13:03:39 +01:00
Jinxuan He be0daa419d Align the attribute names for Property1D, Property2D and Property3D with the speckle-sharp repo 2022-12-05 14:32:59 +01:00
Gergő Jedlicska a7d1b9ce30 Merge pull request #230 from specklesystems/gergo/py311
testing cimg/python311
2022-11-02 12:43:49 +01:00
Gergő Jedlicska 05756a8e9e testing cimg/python311 2022-11-02 12:41:32 +01:00
Gergő Jedlicska 1900fece8b version bump 2022-10-07 16:02:16 +02:00
Gergő Jedlicska 344b7de557 typing and formatting fixes 2022-10-07 15:59:48 +02:00
122 changed files with 9284 additions and 4435 deletions
+25 -34
View File
@@ -6,48 +6,38 @@ orbs:
jobs:
test:
docker:
- image: "cimg/python:<<parameters.tag>>"
- image: "cimg/node:16.15"
- image: "cimg/redis:6.2"
- image: "cimg/postgres:14.2"
environment:
POSTGRES_DB: speckle2_test
POSTGRES_PASSWORD: speckle
POSTGRES_USER: speckle
- image: "speckle/speckle-server"
command: ["bash", "-c", "/wait && node bin/www"]
environment:
POSTGRES_URL: "localhost"
POSTGRES_USER: "speckle"
POSTGRES_PASSWORD: "speckle"
POSTGRES_DB: "speckle2_test"
REDIS_URL: "redis://localhost"
SESSION_SECRET: "keyboard cat"
STRATEGY_LOCAL: "true"
CANONICAL_URL: "http://localhost:3000"
WAIT_HOSTS: localhost:5432, localhost:6379
DISABLE_FILE_UPLOADS: "true"
machine:
image: ubuntu-2204:2023.02.1
docker_layer_caching: true
resource_class: medium
parameters:
tag:
default: "3.8"
default: "3.11"
type: string
steps:
- checkout
- run: python --version
- run:
command: python -m pip install --upgrade pip
name: upgrade pip
- python/install-packages:
pkg-manager: poetry
- run: poetry run pytest --cov --cov-report xml:reports/coverage.xml --junitxml=reports/test-results.xml
name: Install python
command: |
pyenv install -s << parameters.tag >>
pyenv global << parameters.tag >>
- run:
name: Startup the Speckle Server
command: docker compose -f docker-compose.yml up -d
- run:
name: Install Poetry
command: |
pip install poetry
- run:
name: Install packages
command: poetry install
- run:
name: Run tests
command: poetry run pytest --cov --cov-report xml:reports/coverage.xml --junitxml=reports/test-results.xml
- store_test_results:
path: reports
- store_artifacts:
path: reports
- codecov/upload
deploy:
@@ -57,7 +47,7 @@ jobs:
- checkout
- run: python patch_version.py $CIRCLE_TAG
- run: poetry build
- run: poetry publish -u specklesystems -p $PYPI_PASSWORD
- run: poetry publish -u __token__ -p $PYPI_TOKEN
workflows:
main:
@@ -65,11 +55,12 @@ workflows:
- test:
matrix:
parameters:
tag: ["3.7", "3.8", "3.9", "3.10"]
tag: ["3.11"]
filters:
tags:
only: /.*/
- deploy:
context: pypi
requires:
- test
filters:
+1 -1
View File
@@ -52,4 +52,4 @@
"postCreateCommand": "poetry config virtualenvs.create false && poetry install",
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}
}
+1 -1
View File
@@ -41,4 +41,4 @@ services:
command: /bin/sh -c "while sleep 1000; do :; done"
network_mode: host
# networks:
# default:
# default:
+1 -1
View File
@@ -1,3 +1,3 @@
* text=auto eol=lf
*.{cmd,[cC][mM][dD]} text eol=crlf
*.{bat,[bB][aA][tT]} text eol=crlf
*.{bat,[bB][aA][tT]} text eol=crlf
-77
View File
@@ -1,77 +0,0 @@
name: Update issue Status
on:
issues:
types: [closed]
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 }}
-50
View File
@@ -1,50 +0,0 @@
name: Move new issues into Project
on:
issues:
types: [opened]
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
+1 -1
View File
@@ -112,4 +112,4 @@ venv.bak/
# other
scratch.py
settings.json
settings.json
+33
View File
@@ -0,0 +1,33 @@
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
hooks:
- id: ruff
rev: v0.0.186
- repo: https://github.com/commitizen-tools/commitizen
hooks:
- id: commitizen
- id: commitizen-branch
stages:
- push
rev: v2.38.0
- repo: https://github.com/pycqa/isort
rev: v5.11.3
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 22.12.0
hooks:
- id: black
# It is recommended to specify the latest version of Python
# supported by your project here, or alternatively use
# pre-commit's default_language_version, see
# https://pre-commit.com/#top_level-default_language_version
# language_version: python3.11
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
+5 -4
View File
@@ -4,7 +4,8 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
@@ -17,10 +18,10 @@
"name": "Pytest",
"type": "python",
"request": "launch",
"program": "poetry",
"args": ["run", "pytest"],
"program": "pytest",
"args": [],
"console": "integratedTerminal",
"justMyCode": true
}
]
}
}
+9 -3
View File
@@ -34,7 +34,7 @@ What is Speckle? Check our ![YouTube Video Views](https://img.shields.io/youtube
Give Speckle a try in no time by:
- [![speckle XYZ](https://img.shields.io/badge/https://-speckle.xyz-0069ff?style=flat-square&logo=hackthebox&logoColor=white)](https://speckle.xyz) ⇒ creating an account at our public server
- [![create a droplet](https://img.shields.io/badge/Create%20a%20Droplet-0069ff?style=flat-square&logo=digitalocean&logoColor=white)](https://marketplace.digitalocean.com/apps/speckle-server?refcode=947a2b5d7dc1) ⇒ deploying an instance in 1 click
- [![create a droplet](https://img.shields.io/badge/Create%20a%20Droplet-0069ff?style=flat-square&logo=digitalocean&logoColor=white)](https://marketplace.digitalocean.com/apps/speckle-server?refcode=947a2b5d7dc1) ⇒ deploying an instance in 1 click
### Resources
@@ -47,7 +47,7 @@ Give Speckle a try in no time by:
## Usage
Send and receive data from a Speckle Server with `operations`, interact with the Speckle API with the `SpeckleClient`, create and extend your own custom Speckle Objects with `Base`, and more!
Send and receive data from a Speckle Server with `operations`, interact with the Speckle API with the `SpeckleClient`, create and extend your own custom Speckle Objects with `Base`, and more!
Head to the [**📚 specklepy docs**](https://speckle.guide/dev/python.html) for more information and usage examples.
@@ -65,6 +65,12 @@ To execute any python script run `$ poetry run python my_script.py`
> Alternatively you may roll your own virtual-env with either venv, virtualenv, pyenv-virtualenv etc. Poetry will play along an recognize if it is invoked from inside a virtual environment.
### Style guide
All our repo wide styling linting and other rules are checked and enforced by `pre-commit`, which is included in the dev dependencies.
It is recommended to set up `pre-commit` after installing the dependencies by running `$ pre-commit install`.
Commiting code that doesn't adhere to the given rules, will fail the checks in our CI system.
### Local Data Paths
It may be helpful to know where the local accounts and object cache dbs are stored. Depending on on your OS, you can find the dbs at:
@@ -82,7 +88,7 @@ The Speckle Community hangs out on [the forum](https://discourse.speckle.works),
## Security
For any security vulnerabilities or concerns, please contact us directly at security[at]speckle.systems.
For any security vulnerabilities or concerns, please contact us directly at security[at]speckle.systems.
## License
+162
View File
@@ -0,0 +1,162 @@
version: "3.9"
name: "speckle-server"
services:
####
# Speckle Server dependencies
#######
postgres:
image: "postgres:14.5-alpine"
restart: always
environment:
POSTGRES_DB: speckle
POSTGRES_USER: speckle
POSTGRES_PASSWORD: speckle
volumes:
- postgres-data:/var/lib/postgresql/data/
healthcheck:
# the -U user has to match the POSTGRES_USER value
test: ["CMD-SHELL", "pg_isready -U speckle"]
interval: 5s
timeout: 5s
retries: 30
redis:
image: "redis:6.0-alpine"
restart: always
volumes:
- redis-data:/data
healthcheck:
test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
interval: 5s
timeout: 5s
retries: 30
minio:
image: "minio/minio"
command: server /data --console-address ":9001"
restart: always
volumes:
- minio-data:/data
healthcheck:
test:
[
"CMD-SHELL",
"curl -s -o /dev/null http://127.0.0.1:9000/minio/index.html",
]
interval: 5s
timeout: 30s
retries: 30
start_period: 10s
####
# Speckle Server
#######
speckle-frontend:
image: speckle/speckle-frontend:latest
restart: always
ports:
- "0.0.0.0:8080:8080"
environment:
FILE_SIZE_LIMIT_MB: 100
speckle-server:
image: speckle/speckle-server:latest
restart: always
healthcheck:
test:
[
"CMD",
"node",
"-e",
"require('node:http').request({headers: {'Content-Type': 'application/json'}, port:3000, hostname:'127.0.0.1', path:'/graphql?query={serverInfo{version}}', method: 'GET' }, (res) => { body = ''; res.on('data', (chunk) => {body += chunk;}); res.on('end', () => {process.exit(res.statusCode != 200 || body.toLowerCase().includes('error'));}); }).end();",
]
interval: 10s
timeout: 3s
retries: 30
ports:
- "0.0.0.0:3000:3000"
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
minio:
condition: service_healthy
environment:
# TODO: Change this to the URL of the speckle server, as accessed from the network
CANONICAL_URL: "http://127.0.0.1:8080"
SPECKLE_AUTOMATE_URL: "http://127.0.0.1:3030"
# TODO: Change thvolumes:
REDIS_URL: "redis://redis"
S3_ENDPOINT: "http://minio:9000"
S3_ACCESS_KEY: "minioadmin"
S3_SECRET_KEY: "minioadmin"
S3_BUCKET: "speckle-server"
S3_CREATE_BUCKET: "true"
FILE_SIZE_LIMIT_MB: 100
# TODO: Change this to a unique secret for this server
SESSION_SECRET: "TODO:ReplaceWithLongString"
STRATEGY_LOCAL: "true"
DEBUG: "speckle:*"
POSTGRES_URL: "postgres"
POSTGRES_USER: "speckle"
POSTGRES_PASSWORD: "speckle"
POSTGRES_DB: "speckle"
ENABLE_MP: "false"
preview-service:
image: speckle/speckle-preview-service:latest
restart: always
depends_on:
speckle-server:
condition: service_healthy
mem_limit: "1000m"
memswap_limit: "1000m"
environment:
DEBUG: "preview-service:*"
PG_CONNECTION_STRING: "postgres://speckle:speckle@postgres/speckle"
webhook-service:
image: speckle/speckle-webhook-service:latest
restart: always
depends_on:
speckle-server:
condition: service_healthy
environment:
DEBUG: "webhook-service:*"
PG_CONNECTION_STRING: "postgres://speckle:speckle@postgres/speckle"
WAIT_HOSTS: postgres:5432
fileimport-service:
image: speckle/speckle-fileimport-service:latest
restart: always
depends_on:
speckle-server:
condition: service_healthy
environment:
DEBUG: "fileimport-service:*"
PG_CONNECTION_STRING: "postgres://speckle:speckle@postgres/speckle"
WAIT_HOSTS: postgres:5432
S3_ENDPOINT: "http://minio:9000"
S3_ACCESS_KEY: "minioadmin"
S3_SECRET_KEY: "minioadmin"
S3_BUCKET: "speckle-server"
SPECKLE_SERVER_URL: "http://speckle-server:3000"
networks:
default:
name: speckle-server
volumes:
postgres-data:
redis-data:
minio-data:
+8 -8
View File
@@ -1,12 +1,13 @@
from typing import List
from specklepy.objects import Base
from specklepy.api import operations
from specklepy.transports.sqlite import SQLiteTransport
import os
import random
import string
import time
from pathlib import Path
import os
import string
import random
from typing import List
from specklepy.api import operations
from specklepy.objects import Base
from specklepy.transports.sqlite import SQLiteTransport
class Sub(Base):
@@ -26,7 +27,6 @@ def clean_db():
def one_pass(clean: bool, randomize: bool, child_count: int):
foo = Base()
for i in range(child_count):
stuff = random_string() if randomize else "stuff"
+15
View File
@@ -0,0 +1,15 @@
from devtools import debug
from specklepy.api import operations
from specklepy.api.wrapper import StreamWrapper
if __name__ == "__main__":
stream_url = "https://latest.speckle.dev/streams/7d051a6449"
wrapper = StreamWrapper(stream_url)
transport = wrapper.get_transport()
rec = operations.receive("98396753f8bf7fe1cb60c5193e9f9d86", transport)
# hash = operations.send(base=foo, transports=[transport], use_default_cache=False)
debug(rec)
+4 -4
View File
@@ -1,9 +1,10 @@
import random
import string
from typing import List
from specklepy.api import operations
from specklepy.api.wrapper import StreamWrapper
from specklepy.objects import Base
from specklepy.api import operations
import string
import random
class Sub(Base):
@@ -24,7 +25,6 @@ def create_object(child_count: int) -> Base:
if __name__ == "__main__":
stream_url = "http://hyperion:3000/streams/2372b54c35"
child_count = 10
+2 -1
View File
@@ -1,3 +1,5 @@
from devtools import debug
from specklepy.api import operations
from specklepy.objects.geometry import Base
from specklepy.objects.units import Units
@@ -13,7 +15,6 @@ base = Base()
for prop, value in dct.items():
base.__setattr__(prop, value)
from devtools import debug
debug(base)
debug(base.units)
+3 -2
View File
@@ -1,10 +1,11 @@
"""This is an example showcasing the usage of speckle `Base` class."""
# the speckle.objects module exposes all speckle provided classes
from specklepy.objects import Base
from specklepy.api import operations
from devtools import debug
from specklepy.api import operations
from specklepy.objects import Base
class ExampleSub(Base):
"""
+1 -1
View File
@@ -9,7 +9,7 @@ def patch(tag):
lines = f.readlines()
if "version" not in lines[2]:
raise Exception(f"Invalid pyproject.toml. Could not patch version.")
raise Exception("Invalid pyproject.toml. Could not patch version.")
lines[2] = f'version = "{tag}"\n'
with open("pyproject.toml", "w") as file:
Generated
+1449 -945
View File
File diff suppressed because it is too large Load Diff
+20 -3
View File
@@ -10,16 +10,20 @@ documentation = "https://speckle.guide/dev/py-examples.html"
homepage = "https://speckle.systems/"
packages = [
{ include = "specklepy", from = "src" },
{ include = "speckle_automate", from = "src" },
]
[tool.poetry.dependencies]
python = ">=3.7.2, <4.0"
pydantic = "^1.8.2"
python = ">=3.8.0, <4.0"
pydantic = "^2.0"
appdirs = "^1.4.4"
gql = {extras = ["requests", "websockets"], version = "^3.3.0"}
ujson = "^5.3.0"
Deprecated = "^1.2.13"
stringcase = "^1.2.0"
attrs = "^23.1.0"
httpx = "^0.25.0"
[tool.poetry.group.dev.dependencies]
black = "^22.8.0"
@@ -30,6 +34,12 @@ pytest-cov = "^3.0.0"
devtools = "^0.8.0"
pylint = "^2.14.4"
mypy = "^0.982"
pre-commit = "^2.20.0"
commitizen = "^2.38.0"
ruff = "^0.0.187"
types-deprecated = "^1.2.9"
types-ujson = "^5.6.0.0"
types-requests = "^2.28.11.5"
[tool.black]
exclude = '''
@@ -48,9 +58,16 @@ exclude = '''
'''
include = '\.pyi?$'
line-length = 88
target-version = ["py37", "py38", "py39", "py310"]
target-version = ["py37", "py38", "py39", "py310", "py311"]
[tool.commitizen]
name = "cz_conventional_commits"
version = "2.9.2"
tag_format = "$version"
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
[tool.isort]
profile = "black"
+23
View File
@@ -0,0 +1,23 @@
"""This module contains an SDK for working with Speckle Automate."""
from speckle_automate.automation_context import AutomationContext
from speckle_automate.runner import execute_automate_function, run_function
from speckle_automate.schema import (
AutomateBase,
AutomationResult,
AutomationRunData,
AutomationStatus,
ObjectResult,
ObjectResultLevel,
)
__all__ = [
"AutomationContext",
"AutomateBase",
"AutomationStatus",
"AutomationResult",
"AutomationRunData",
"ObjectResult",
"ObjectResultLevel",
"run_function",
"execute_automate_function",
]
+278
View File
@@ -0,0 +1,278 @@
"""This module provides an abstraction layer above the Speckle Automate runtime."""
from dataclasses import dataclass, field
from pathlib import Path
import time
from typing import Optional, Union
import httpx
from gql import gql
from specklepy.api import operations
from specklepy.api.client import SpeckleClient
from specklepy.objects import Base
from specklepy.transports.memory import MemoryTransport
from specklepy.transports.server import ServerTransport
from speckle_automate.schema import (
AutomateBase,
AutomationResult,
AutomationRunData,
AutomationStatus,
ObjectResult,
ObjectResultLevel,
)
@dataclass
class AutomationContext:
"""A context helper class.
This class exposes methods to work with the Speckle Automate context inside
Speckle Automate functions.
An instance of AutomationContext is injected into every run of a function.
"""
automation_run_data: AutomationRunData
speckle_client: SpeckleClient
_server_transport: ServerTransport
_speckle_token: str
#: keep a memory transponrt at hand, to speed up things if needed
_memory_transport: MemoryTransport = field(default_factory=MemoryTransport)
#: added for performance measuring
_init_time: float = field(default_factory=time.perf_counter)
_automation_result: AutomationResult = field(default_factory=AutomationResult)
@classmethod
def initialize(
cls, automation_run_data: Union[str, AutomationRunData], speckle_token: str
) -> "AutomationContext":
"""Bootstrap the AutomateSDK from raw data.
Todo:
----
* bootstrap a structlog logger instance
* expose a logger, that ppl can use instead of print
"""
# parse the json value if its not an initialized project data instance
automation_run_data = (
automation_run_data
if isinstance(automation_run_data, AutomationRunData)
else AutomationRunData.model_validate_json(automation_run_data)
)
speckle_client = SpeckleClient(
automation_run_data.speckle_server_url,
automation_run_data.speckle_server_url.startswith("https"),
)
speckle_client.authenticate_with_token(speckle_token)
if not speckle_client.account:
msg = (
f"Could not autenticate to {automation_run_data.speckle_server_url}",
"with the provided token",
)
raise ValueError(msg)
server_transport = ServerTransport(
automation_run_data.project_id, speckle_client
)
return cls(automation_run_data, speckle_client, server_transport, speckle_token)
@property
def run_status(self) -> AutomationStatus:
"""Get the status of the automation run."""
return self._automation_result.run_status
def elapsed(self) -> float:
"""Return the elapsed time in seconds since the initialization time."""
return time.perf_counter() - self._init_time
def receive_version(self) -> Base:
"""Receive the Speckle project version that triggered this automation run."""
commit = self.speckle_client.commit.get(
self.automation_run_data.project_id, self.automation_run_data.version_id
)
if not commit.referencedObject:
raise ValueError("The commit has no referencedObject, cannot receive it.")
base = operations.receive(
commit.referencedObject, self._server_transport, self._memory_transport
)
print(
f"It took {self.elapsed():2f} seconds to receive",
f" the speckle version {self.automation_run_data.version_id}",
)
return base
def create_new_version_in_project(
self, root_object: Base, model_id: str, version_message: str = ""
) -> None:
"""Save a base model to a new version on the project.
Args:
root_object (Base): The Speckle base object for the new version.
model_id (str): For now please use a `branchName`!
version_message (str): The message for the new version.
"""
if model_id == self.automation_run_data.model_id:
raise ValueError(
f"The target model id: {model_id} cannot match the model id"
f" that triggered this automation: {self.automation_run_data.model_id}"
)
root_object_id = operations.send(
root_object,
[self._server_transport, self._memory_transport],
use_default_cache=False,
)
version_id = self.speckle_client.commit.create(
stream_id=self.automation_run_data.project_id,
object_id=root_object_id,
branch_name=model_id,
message=version_message,
source_application="SpeckleAutomate",
)
self._automation_result.result_versions.append(version_id)
def report_run_status(self) -> None:
"""Report the current run status to the project of this automation."""
query = gql(
"""
mutation ReportFunctionRunStatus(
$automationId: String!,
$automationRevisionId: String!,
$automationRunId: String!,
$versionId: String!,
$functionId: String!,
$runStatus: AutomationRunStatus!
$elapsed: Float!
$resultVersionIds: [String!]!
$statusMessage: String
$objectResults: JSONObject
){
automationMutations {
functionRunStatusReport(input: {
automationId: $automationId
automationRevisionId: $automationRevisionId
automationRunId: $automationRunId
versionId: $versionId
functionRuns: [
{
functionId: $functionId
status: $runStatus,
elapsed: $elapsed,
resultVersionIds: $resultVersionIds,
statusMessage: $statusMessage
results: $objectResults
}]
})
}
}
"""
)
if self.run_status in [AutomationStatus.SUCCEEDED, AutomationStatus.FAILED]:
object_results = {
"version": "1.0.0",
"values": {
"speckleObjects": self._automation_result.model_dump(by_alias=True)[
"objectResults"
],
"blobs": self._automation_result.blobs,
},
}
else:
object_results = None
params = {
"automationId": self.automation_run_data.automation_id,
"automationRevisionId": self.automation_run_data.automation_revision_id,
"automationRunId": self.automation_run_data.automation_run_id,
"versionId": self.automation_run_data.version_id,
"functionId": self.automation_run_data.function_id,
"runStatus": self.run_status.value,
"statusMessage": self._automation_result.status_message,
"elapsed": self.elapsed(),
"resultVersionIds": self._automation_result.result_versions,
"objectResults": object_results,
}
self.speckle_client.httpclient.execute(query, params)
def store_file_result(self, file_path: Union[Path, str]) -> None:
"""Save a file attached to the project of this automation."""
path_obj = (
Path(file_path).resolve() if isinstance(file_path, str) else file_path
)
class UploadResult(AutomateBase):
blob_id: str
file_name: str
upload_status: int
class BlobUploadResponse(AutomateBase):
upload_results: list[UploadResult]
if not path_obj.exists():
raise ValueError("The given file path doesn't exist")
files = {path_obj.name: open(str(path_obj), "rb")}
url = (
f"{self.automation_run_data.speckle_server_url}/api/stream/"
f"{self.automation_run_data.project_id}/blob"
)
data = (
httpx.post(
url,
files=files,
headers={"authorization": f"Bearer {self._speckle_token}"},
)
.raise_for_status()
.json()
)
upload_response = BlobUploadResponse.model_validate(data)
if len(upload_response.upload_results) != 1:
raise ValueError("Expecting one upload result.")
for upload_result in upload_response.upload_results:
self._automation_result.blobs.append(upload_result.blob_id)
def mark_run_failed(self, status_message: str) -> None:
"""Mark the current run a failure."""
self._mark_run(AutomationStatus.FAILED, status_message)
def mark_run_success(self, status_message: Optional[str]) -> None:
"""Mark the current run a success with an optional message."""
self._mark_run(AutomationStatus.SUCCEEDED, status_message)
def _mark_run(
self, status: AutomationStatus, status_message: Optional[str]
) -> None:
duration = self.elapsed()
self._automation_result.status_message = status_message
self._automation_result.run_status = status
self._automation_result.elapsed = duration
msg = f"Automation run {status.value} after {duration:2f} seconds."
print("\n".join([msg, status_message]) if status_message else msg)
def add_object_error(self, object_id: str, error_cause: str) -> None:
"""Add an error to a given Speckle object."""
self._add_object_result(object_id, ObjectResultLevel.ERROR, error_cause)
def add_object_warning(self, object_id: str, warning: str) -> None:
"""Add a warning to a given Speckle object."""
self._add_object_result(object_id, ObjectResultLevel.WARNING, warning)
def add_object_info(self, object_id: str, info: str) -> None:
"""Add an info message to a given Speckle object."""
self._add_object_result(object_id, ObjectResultLevel.INFO, info)
def _add_object_result(
self, object_id: str, level: ObjectResultLevel, status_message: str
) -> None:
print(
f"Object {object_id} was marked with {level.value.upper()}",
f" cause: {status_message}",
)
self._automation_result.object_results[object_id].append(
ObjectResult(level=level, status_message=status_message)
)
+155
View File
@@ -0,0 +1,155 @@
"""Function execution module.
Provides mechanisms to execute any function,
that conforms to the AutomateFunction "interface"
"""
import json
import os
import sys
import traceback
from pathlib import Path
from typing import Callable, Optional, TypeVar, Union, overload
from speckle_automate.automation_context import AutomationContext
from speckle_automate.schema import AutomateBase, AutomationRunData, AutomationStatus
T = TypeVar("T", bound=AutomateBase)
AutomateFunction = Callable[[AutomationContext, T], None]
AutomateFunctionWithoutInputs = Callable[[AutomationContext], None]
@overload
def execute_automate_function(
automate_function: AutomateFunction[T],
input_schema: type[T],
) -> None:
...
@overload
def execute_automate_function(automate_function: AutomateFunctionWithoutInputs) -> None:
...
def execute_automate_function(
automate_function: Union[AutomateFunction[T], AutomateFunctionWithoutInputs],
input_schema: Optional[type[T]] = None,
):
"""Runs the provided automate function with the input schema."""
# first arg is the python file name, we do not need that
args = sys.argv[1:]
if len(args) < 2:
raise ValueError("too few arguments specified need minimum 2")
if len(args) > 4:
raise ValueError("too many arguments specified, max supported is 4")
# we rely on a command name convention to decide what to do.
# this is here, so that the function authors do not see any of this
command = args[0]
if command == "generate_schema":
path = Path(args[1])
schema = json.dumps(
input_schema.model_json_schema(by_alias=True) if input_schema else {}
)
path.write_text(schema)
elif command == "run":
automation_run_data = args[1]
function_inputs = args[2]
speckle_token = os.environ.get("SPECKLE_TOKEN", None)
if not speckle_token and len(args) != 4:
raise ValueError("Cannot get speckle token from arguments or environment")
speckle_token = speckle_token if speckle_token else args[3]
inputs = (
input_schema.model_validate_json(function_inputs)
if input_schema
else input_schema
)
if inputs:
automation_context = run_function(
automate_function, # type: ignore
automation_run_data,
speckle_token,
inputs,
)
else:
automation_context = run_function(
automate_function, # type: ignore
automation_run_data,
speckle_token,
)
exit_code = (
0 if automation_context.run_status == AutomationStatus.SUCCEEDED else 1
)
exit(exit_code)
else:
raise NotImplementedError(f"Command: '{command}' is not supported.")
@overload
def run_function(
automate_function: AutomateFunction[T],
automation_run_data: Union[AutomationRunData, str],
speckle_token: str,
inputs: T,
) -> AutomationContext:
...
@overload
def run_function(
automate_function: AutomateFunctionWithoutInputs,
automation_run_data: Union[AutomationRunData, str],
speckle_token: str,
) -> AutomationContext:
...
def run_function(
automate_function: Union[AutomateFunction[T], AutomateFunctionWithoutInputs],
automation_run_data: Union[AutomationRunData, str],
speckle_token: str,
inputs: Optional[T] = None,
) -> AutomationContext:
"""Run the provided function with the automate sdk context."""
automation_context = AutomationContext.initialize(
automation_run_data, speckle_token
)
automation_context.report_run_status()
try:
# avoiding complex type gymnastics here on the internals.
# the external type overloads make this correct
if inputs:
automate_function(automation_context, inputs) # type: ignore
else:
automate_function(automation_context) # type: ignore
# the function author forgot to mark the function success
if automation_context.run_status not in [
AutomationStatus.FAILED,
AutomationStatus.SUCCEEDED,
]:
automation_context.mark_run_success(
"WARNING: Automate assumed a success status,"
" but it was not marked as so by the function."
)
except Exception:
trace = traceback.format_exc()
print(trace)
automation_context.mark_run_failed(
"Function error. Check the automation run logs for details."
)
finally:
automation_context.report_run_status()
return automation_context
+73
View File
@@ -0,0 +1,73 @@
""""""
from collections import defaultdict
from enum import Enum
from typing import Optional
from pydantic import BaseModel, ConfigDict, Field
from stringcase import camelcase
class AutomateBase(BaseModel):
"""Use this class as a base model for automate related DTO."""
model_config = ConfigDict(alias_generator=camelcase, populate_by_name=True)
class AutomationRunData(BaseModel):
"""Values of the project / model that triggered the run of this function."""
project_id: str
model_id: str
branch_name: str
version_id: str
speckle_server_url: str
automation_id: str
automation_revision_id: str
automation_run_id: str
function_id: str
function_revision: str
model_config = ConfigDict(
alias_generator=camelcase, populate_by_name=True, protected_namespaces=()
)
class AutomationStatus(str, Enum):
"""Set the status of the automation."""
INITIALIZING = "INITIALIZING"
RUNNING = "RUNNING"
FAILED = "FAILED"
SUCCEEDED = "SUCCEEDED"
class ObjectResultLevel(str, Enum):
"""Possible status message levels for object reports."""
INFO = "INFO"
WARNING = "WARNING"
ERROR = "ERROR"
class ObjectResult(AutomateBase):
"""An object level result."""
level: ObjectResultLevel
status_message: str
class AutomationResult(AutomateBase):
"""Schema accepted by the Speckle server as a result for an automation run."""
elapsed: float = 0
result_view: Optional[str] = None
result_versions: list[str] = Field(default_factory=list)
blobs: list[str] = Field(default_factory=list)
run_status: AutomationStatus = AutomationStatus.RUNNING
status_message: Optional[str] = None
object_results: dict[str, list[ObjectResult]] = Field(
default_factory=lambda: defaultdict(list) # typing: ignore
)
+74 -137
View File
@@ -1,37 +1,41 @@
import re
from warnings import warn
from deprecated import deprecated
from specklepy.api.credentials import Account, get_account_from_token
from specklepy.logging import metrics
from specklepy.logging.exceptions import (
SpeckleException,
SpeckleWarning,
)
from typing import Dict
from warnings import warn
from specklepy.api import resources
from specklepy.api.resources import (
branch,
commit,
stream,
object,
server,
user,
subscriptions,
other_user,
active_user
)
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
from specklepy.api import resources
from specklepy.api.credentials import Account, get_account_from_token
from specklepy.api.resources import (
user,
active_user,
branch,
commit,
object,
other_user,
server,
stream,
subscriptions,
)
from specklepy.logging import metrics
from specklepy.logging.exceptions import SpeckleException, SpeckleWarning
class SpeckleClient:
from specklepy.core.api.client import SpeckleClient as CoreSpeckleClient
class SpeckleClient(CoreSpeckleClient):
"""
The `SpeckleClient` is your entry point for interacting with your Speckle Server's GraphQL API.
You'll need to have access to a server to use it, or you can use our public server `speckle.xyz`.
The `SpeckleClient` is your entry point for interacting with
your Speckle Server's GraphQL API.
You'll need to have access to a server to use it,
or you can use our public server `speckle.xyz`.
To authenticate the client, you'll need to have downloaded the [Speckle Manager](https://speckle.guide/#speckle-manager)
To authenticate the client, you'll need to have downloaded
the [Speckle Manager](https://speckle.guide/#speckle-manager)
and added your account.
```py
@@ -58,110 +62,12 @@ class SpeckleClient:
USE_SSL = True
def __init__(self, host: str = DEFAULT_HOST, use_ssl: bool = USE_SSL) -> None:
metrics.track(metrics.CLIENT, custom_props={"name": "create"})
ws_protocol = "ws"
http_protocol = "http"
if use_ssl:
ws_protocol = "wss"
http_protocol = "https"
# sanitise host input by removing protocol and trailing slash
host = re.sub(r"((^\w+:|^)\/\/)|(\/$)", "", host)
self.url = f"{http_protocol}://{host}"
self.graphql = f"{self.url}/graphql"
self.ws_url = f"{ws_protocol}://{host}/graphql"
super().__init__(
host=host,
use_ssl=use_ssl,
)
self.account = Account()
self.httpclient = Client(
transport=RequestsHTTPTransport(url=self.graphql, verify=True, retries=3)
)
self.wsclient = None
self._init_resources()
# ? Check compatibility with the server - i think we can skip this at this point? save a request
# try:
# server_info = self.server.get()
# if isinstance(server_info, Exception):
# raise server_info
# if not isinstance(server_info, ServerInfo):
# raise Exception("Couldn't get ServerInfo")
# except Exception as ex:
# raise SpeckleException(
# f"{self.url} is not a compatible Speckle Server", ex
# ) from ex
def __repr__(self):
return f"SpeckleClient( server: {self.url}, authenticated: {self.account.token is not None} )"
@deprecated(
version="2.6.0",
reason="Renamed: please use `authenticate_with_account` or `authenticate_with_token` instead.",
)
def authenticate(self, token: str) -> None:
"""Authenticate the client using a personal access token
The token is saved in the client object and a synchronous GraphQL entrypoint is created
Arguments:
token {str} -- an api token
"""
self.authenticate_with_token(token)
self._set_up_client()
def authenticate_with_token(self, token: str) -> None:
"""Authenticate the client using a personal access token
The token is saved in the client object and a synchronous GraphQL entrypoint is created
Arguments:
token {str} -- an api token
"""
self.account = get_account_from_token(token, self.url)
metrics.track(metrics.CLIENT, self.account, {"name": "authenticate with token"})
self._set_up_client()
def authenticate_with_account(self, account: Account) -> None:
"""Authenticate the client using an Account object
The account is saved in the client object and a synchronous GraphQL entrypoint is created
Arguments:
account {Account} -- the account object which can be found with `get_default_account` or `get_local_accounts`
"""
metrics.track(metrics.CLIENT, account, {"name": "authenticate with account"})
self.account = account
self._set_up_client()
def _set_up_client(self) -> None:
metrics.track(metrics.CLIENT, self.account, {"name": "set up client"})
headers = {
"Authorization": f"Bearer {self.account.token}",
"Content-Type": "application/json",
"apollographql-client-name": metrics.HOST_APP,
"apollographql-client-version": metrics.HOST_APP_VERSION,
}
httptransport = RequestsHTTPTransport(
url=self.graphql, headers=headers, verify=True, retries=3
)
wstransport = WebsocketsTransport(
url=self.ws_url,
init_payload={"Authorization": f"Bearer {self.account.token}"},
)
self.httpclient = Client(transport=httptransport)
self.wsclient = Client(transport=wstransport)
self._init_resources()
if self.user.get() is None:
warn(
SpeckleWarning(
f"Possibly invalid token - could not authenticate Speckle Client for server {self.url}"
)
)
def execute_query(self, query: str) -> Dict:
return self.httpclient.execute(query)
def _init_resources(self) -> None:
self.server = server.Resource(
account=self.account, basepath=self.url, client=self.httpclient
@@ -169,7 +75,7 @@ class SpeckleClient:
server_version = None
try:
server_version = self.server.version()
except:
except Exception:
pass
self.user = user.Resource(
account=self.account,
@@ -210,13 +116,44 @@ class SpeckleClient:
client=self.wsclient,
)
def __getattr__(self, name):
try:
attr = getattr(resources, name)
return attr.Resource(
account=self.account, basepath=self.url, client=self.httpclient
)
except:
raise SpeckleException(
f"Method {name} is not supported by the SpeckleClient class"
)
@deprecated(
version="2.6.0",
reason=(
"Renamed: please use `authenticate_with_account` or"
" `authenticate_with_token` instead."
),
)
def authenticate(self, token: str) -> None:
"""Authenticate the client using a personal access token
The token is saved in the client object and a synchronous GraphQL
entrypoint is created
Arguments:
token {str} -- an api token
"""
metrics.track(metrics.SDK, self.account, {"name": "Client Authenticate_deprecated"})
return super().authenticate(token)
def authenticate_with_token(self, token: str) -> None:
"""
Authenticate the client using a personal access token.
The token is saved in the client object and a synchronous GraphQL
entrypoint is created
Arguments:
token {str} -- an api token
"""
metrics.track(metrics.SDK, self.account, {"name": "Client Authenticate With Token"})
return super().authenticate_with_token(token)
def authenticate_with_account(self, account: Account) -> None:
"""Authenticate the client using an Account object
The account is saved in the client object and a synchronous GraphQL
entrypoint is created
Arguments:
account {Account} -- the account object which can be found with
`get_default_account` or `get_local_accounts`
"""
metrics.track(metrics.SDK, self.account, {"name": "Client Authenticate With Account"})
return super().authenticate_with_account(account)
+27 -98
View File
@@ -1,39 +1,20 @@
import os
from pydantic import BaseModel, Field # pylint: disable=no-name-in-module
from typing import List, Optional
from specklepy.logging import metrics
from pydantic import BaseModel, Field # pylint: disable=no-name-in-module
from specklepy.api.models import ServerInfo
from specklepy.transports.sqlite import SQLiteTransport
from specklepy.core.helpers import speckle_path_provider
from specklepy.logging import metrics
from specklepy.logging.exceptions import SpeckleException
from specklepy import paths
from specklepy.transports.sqlite import SQLiteTransport
class UserInfo(BaseModel):
name: Optional[str] = None
email: Optional[str] = None
company: Optional[str] = None
id: Optional[str] = None
class Account(BaseModel):
isDefault: bool = False
token: Optional[str] = None
refreshToken: Optional[str] = None
serverInfo: ServerInfo = Field(default_factory=ServerInfo)
userInfo: UserInfo = Field(default_factory=UserInfo)
id: Optional[str] = None
def __repr__(self) -> str:
return f"Account(email: {self.userInfo.email}, server: {self.serverInfo.url}, isDefault: {self.isDefault})"
def __str__(self) -> str:
return self.__repr__()
@classmethod
def from_token(cls, token: str, server_url: str = None):
acct = cls(token=token)
acct.serverInfo.url = server_url
return acct
# following imports seem to be unnecessary, but they need to stay
# to not break the scripts using these functions as non-core
from specklepy.core.api.credentials import (Account, UserInfo,
StreamWrapper, # deprecated
get_local_accounts as core_get_local_accounts,
get_account_from_token as core_get_account_from_token)
def get_local_accounts(base_path: Optional[str] = None) -> List[Account]:
@@ -43,64 +24,33 @@ def get_local_accounts(base_path: Optional[str] = None) -> List[Account]:
base_path {str} -- custom base path if you are not using the system default
Returns:
List[Account] -- list of all local accounts or an empty list if no accounts were found
List[Account] -- list of all local accounts or an empty list if
no accounts were found
"""
accounts: List[Account] = []
try:
account_storage = SQLiteTransport(scope="Accounts", base_path=base_path)
res = account_storage.get_all_objects()
account_storage.close()
if res:
accounts.extend(Account.parse_raw(r[1]) for r in res)
except SpeckleException:
# cannot open SQLiteTransport, probably because of the lack
# of disk write permissions
pass
json_acct_files = []
json_path = paths.accounts_path()
try:
os.makedirs(json_path, exist_ok=True)
json_acct_files.extend(
file for file in os.listdir(json_path) if file.endswith(".json")
)
except Exception:
# cannot find or get the json account paths
pass
if json_acct_files:
try:
accounts.extend(
Account.parse_file(os.path.join(json_path, json_file))
for json_file in json_acct_files
)
except Exception as ex:
raise SpeckleException(
"Invalid json accounts could not be read. Please fix or remove them.",
ex,
) from ex
accounts = core_get_local_accounts(base_path)
metrics.track(
metrics.ACCOUNTS,
metrics.SDK,
next(
(acc for acc in accounts if acc.isDefault),
accounts[0] if accounts else None,
),
{"name": "Get Local Accounts"}
)
return accounts
def get_default_account(base_path: Optional[str] = None) -> Optional[Account]:
"""Gets this environment's default account if any. If there is no default, the first found will be returned and set as default.
"""
Gets this environment's default account if any. If there is no default,
the first found will be returned and set as default.
Arguments:
base_path {str} -- custom base path if you are not using the system default
Returns:
Account -- the default account or None if no local accounts were found
"""
accounts = get_local_accounts(base_path=base_path)
accounts = core_get_local_accounts(base_path=base_path)
if not accounts:
return None
@@ -111,38 +61,17 @@ def get_default_account(base_path: Optional[str] = None) -> Optional[Account]:
metrics.initialise_tracker(default)
return default
def get_account_from_token(token: str, server_url: str = None) -> Account:
"""Gets the local account for the token if it exists
Arguments:
token {str} -- the api token
Returns:
Account -- the local account with this token or a shell account containing just the token and url if no local account is found
Account -- the local account with this token or a shell account containing
just the token and url if no local account is found
"""
accounts = get_local_accounts()
if not accounts:
return Account.from_token(token, server_url)
account = core_get_account_from_token(token, server_url)
acct = next((acc for acc in accounts if acc.token == token), None)
if acct:
return acct
if server_url:
url = server_url.lower()
acct = next(
(acc for acc in accounts if url in acc.serverInfo.url.lower()), None
)
if acct:
return acct
return Account.from_token(token, server_url)
class StreamWrapper:
def __init__(self, url: str = None) -> None:
raise SpeckleException(
message="The StreamWrapper has moved as of v2.6.0! Please import from specklepy.api.wrapper",
exception=DeprecationWarning,
)
metrics.track( metrics.SDK, account, {"name": "Get Account From Token"} )
return account
+12 -110
View File
@@ -1,116 +1,18 @@
from enum import Enum
from dataclasses import dataclass
from enum import Enum
from unicodedata import name
class HostAppVersion(Enum):
v = "v"
v6 = "v6"
v7 = "v7"
v2019 = "v2019"
v2020 = "v2020"
v2021 = "v2021"
v2022 = "v2022"
v2023 = "v2023"
v2024 = "v2024"
v2025 = "v2025"
vSandbox = "vSandbox"
vRevit = "vRevit"
vRevit2021 = "vRevit2021"
vRevit2022 = "vRevit2022"
vRevit2023 = "vRevit2023"
vRevit2024 = "vRevit2024"
vRevit2025 = "vRevit2025"
v25 = "v25"
v26 = "v26"
def __repr__(self) -> str:
return self.value
def __str__(self) -> str:
return self.value
@dataclass
class HostApplication:
name: str
slug: str
def get_version(self, version: HostAppVersion) -> str:
return f"{name.replace(' ', '')}{str(version).strip('v')}"
RHINO = HostApplication("Rhino", "rhino")
GRASSHOPPER = HostApplication("Grasshopper", "grasshopper")
REVIT = HostApplication("Revit", "revit")
DYNAMO = HostApplication("Dynamo", "dynamo")
UNITY = HostApplication("Unity", "unity")
GSA = HostApplication("GSA", "gsa")
CIVIL = HostApplication("Civil 3D", "civil3d")
AUTOCAD = HostApplication("AutoCAD", "autocad")
MICROSTATION = HostApplication("MicroStation", "microstation")
OPENROADS = HostApplication("OpenRoads", "openroads")
OPENRAIL = HostApplication("OpenRail", "openrail")
OPENBUILDINGS = HostApplication("OpenBuildings", "openbuildings")
ETABS = HostApplication("ETABS", "etabs")
SAP2000 = HostApplication("SAP2000", "sap2000")
CSIBRIDGE = HostApplication("CSIBridge", "csibridge")
SAFE = HostApplication("SAFE", "safe")
TEKLASTRUCTURES = HostApplication("Tekla Structures", "teklastructures")
DXF = HostApplication("DXF Converter", "dxf")
EXCEL = HostApplication("Excel", "excel")
UNREAL = HostApplication("Unreal", "unreal")
POWERBI = HostApplication("Power BI", "powerbi")
BLENDER = HostApplication("Blender", "blender")
QGIS = HostApplication("QGIS", "qgis")
ARCGIS = HostApplication("ArcGIS", "arcgis")
SKETCHUP = HostApplication("SketchUp", "sketchup")
ARCHICAD = HostApplication("Archicad", "archicad")
TOPSOLID = HostApplication("TopSolid", "topsolid")
PYTHON = HostApplication("Python", "python")
NET = HostApplication(".NET", "net")
OTHER = HostApplication("Other", "other")
_app_name_host_app_mapping = {
"dynamo": DYNAMO,
"revit": REVIT,
"autocad": AUTOCAD,
"civil": CIVIL,
"rhino": RHINO,
"grasshopper": GRASSHOPPER,
"unity": UNITY,
"gsa": GSA,
"microstation": MICROSTATION,
"openroads": OPENROADS,
"openrail": OPENRAIL,
"openbuildings": OPENBUILDINGS,
"etabs": ETABS,
"sap": SAP2000,
"csibridge": CSIBRIDGE,
"safe": SAFE,
"teklastructures": TEKLASTRUCTURES,
"dxf": DXF,
"excel": EXCEL,
"unreal": UNREAL,
"powerbi": POWERBI,
"blender": BLENDER,
"qgis": QGIS,
"arcgis": ARCGIS,
"sketchup": SKETCHUP,
"archicad": ARCHICAD,
"topsolid": TOPSOLID,
"python": PYTHON,
"net": NET,
}
def get_host_app_from_string(app_name: str) -> HostApplication:
app_name = app_name.lower().replace(" ", "")
for partial_app_name, host_app in _app_name_host_app_mapping.items():
if partial_app_name in app_name:
return host_app
return HostApplication(app_name, app_name)
# following imports seem to be unnecessary, but they need to stay
# to not break the scripts using these functions as non-core
from specklepy.core.api.host_applications import (HostApplication, HostAppVersion,
get_host_app_from_string,
_app_name_host_app_mapping,
RHINO,GRASSHOPPER,REVIT,DYNAMO,UNITY,GSA,
CIVIL,AUTOCAD,MICROSTATION,OPENROADS,
OPENRAIL,OPENBUILDINGS,ETABS,SAP2000,CSIBRIDGE,
SAFE,TEKLASTRUCTURES,DXF,EXCEL,UNREAL,POWERBI,
BLENDER,QGIS,ARCGIS,SKETCHUP,ARCHICAD,TOPSOLID,
PYTHON,NET,OTHER)
if __name__ == "__main__":
print(HostAppVersion.v)
+7 -173
View File
@@ -1,178 +1,12 @@
from datetime import datetime
from typing import List, Optional
from pydantic import BaseModel, Field
class Collaborator(BaseModel):
id: Optional[str]
name: Optional[str]
role: Optional[str]
avatar: Optional[str]
class Commit(BaseModel):
id: Optional[str]
message: Optional[str]
authorName: Optional[str]
authorId: Optional[str]
authorAvatar: Optional[str]
branchName: Optional[str]
createdAt: Optional[datetime]
sourceApplication: Optional[str]
referencedObject: Optional[str]
totalChildrenCount: Optional[int]
parents: Optional[List[str]]
def __repr__(self) -> str:
return f"Commit( id: {self.id}, message: {self.message}, referencedObject: {self.referencedObject}, authorName: {self.authorName}, branchName: {self.branchName}, createdAt: {self.createdAt} )"
def __str__(self) -> str:
return self.__repr__()
class Commits(BaseModel):
totalCount: Optional[int]
cursor: Optional[datetime]
items: List[Commit] = []
class Object(BaseModel):
id: Optional[str]
speckleType: Optional[str]
applicationId: Optional[str]
totalChildrenCount: Optional[int]
createdAt: Optional[datetime]
class Branch(BaseModel):
id: Optional[str]
name: Optional[str]
description: Optional[str]
commits: Optional[Commits]
class Branches(BaseModel):
totalCount: Optional[int]
cursor: Optional[datetime]
items: List[Branch] = []
class Stream(BaseModel):
id: Optional[str] = None
name: Optional[str]
role: Optional[str] = None
isPublic: Optional[bool] = None
description: Optional[str] = None
createdAt: Optional[datetime] = None
updatedAt: Optional[datetime] = None
collaborators: List[Collaborator] = Field(default_factory=list)
branches: Optional[Branches] = None
commit: Optional[Commit] = None
object: Optional[Object] = None
commentCount: Optional[int] = None
favoritedDate: Optional[datetime] = None
favoritesCount: Optional[int] = None
def __repr__(self):
return f"Stream( id: {self.id}, name: {self.name}, description: {self.description}, isPublic: {self.isPublic})"
def __str__(self) -> str:
return self.__repr__()
class Streams(BaseModel):
totalCount: Optional[int]
cursor: Optional[datetime]
items: List[Stream] = []
class User(BaseModel):
id: Optional[str]
email: Optional[str]
name: Optional[str]
bio: Optional[str]
company: Optional[str]
avatar: Optional[str]
verified: Optional[bool]
role: Optional[str]
streams: Optional[Streams]
def __repr__(self):
return f"User( id: {self.id}, name: {self.name}, email: {self.email}, company: {self.company} )"
def __str__(self) -> str:
return self.__repr__()
class LimitedUser(BaseModel):
"""Limited user type, for showing public info about a user to another user."""
id: str
name: Optional[str]
bio: Optional[str]
company: Optional[str]
avatar: Optional[str]
verified: Optional[bool]
role: Optional[str]
class PendingStreamCollaborator(BaseModel):
id: Optional[str]
inviteId: Optional[str]
streamId: Optional[str]
streamName: Optional[str]
title: Optional[str]
role: Optional[str]
invitedBy: Optional[User]
user: Optional[User]
token: Optional[str]
def __repr__(self):
return f"PendingStreamCollaborator( inviteId: {self.inviteId}, streamId: {self.streamId}, role: {self.role}, title: {self.title}, invitedBy: {self.user.name if self.user else None})"
def __str__(self) -> str:
return self.__repr__()
class Activity(BaseModel):
actionType: Optional[str]
info: Optional[dict]
userId: Optional[str]
streamId: Optional[str]
resourceId: Optional[str]
resourceType: Optional[str]
message: Optional[str]
time: Optional[datetime]
def __repr__(self) -> str:
return f"Activity( streamId: {self.streamId}, actionType: {self.actionType}, message: {self.message}, userId: {self.userId} )"
def __str__(self) -> str:
return self.__repr__()
class ActivityCollection(BaseModel):
totalCount: Optional[int]
items: Optional[List[Activity]]
cursor: Optional[datetime]
def __repr__(self) -> str:
return f"ActivityCollection( totalCount: {self.totalCount}, items: {len(self.items) if self.items else 0}, cursor: {self.cursor.isoformat() if self.cursor else None} )"
def __str__(self) -> str:
return self.__repr__()
class ServerInfo(BaseModel):
name: Optional[str] = None
company: Optional[str] = None
url: Optional[str] = None
description: Optional[str] = None
adminContact: Optional[str] = None
canonicalUrl: Optional[str] = None
roles: Optional[List[dict]] = None
scopes: Optional[List[dict]] = None
authStrategies: Optional[List[dict]] = None
version: Optional[str] = None
# following imports seem to be unnecessary, but they need to stay
# to not break the scripts using these functions as non-core
from specklepy.core.api.models import (Collaborator, Commit,
Commits, Object, Branch, Branches,
Stream, Streams, User, LimitedUser,
PendingStreamCollaborator, Activity,
ActivityCollection, ServerInfo)
+38 -75
View File
@@ -1,15 +1,21 @@
from typing import List
from typing import List, Optional
from specklepy.logging import metrics
from specklepy.objects.base import Base
from specklepy.transports.sqlite import SQLiteTransport
from specklepy.logging.exceptions import SpeckleException
from specklepy.transports.abstract_transport import AbstractTransport
from specklepy.objects.base import Base
from specklepy.serialization.base_object_serializer import BaseObjectSerializer
from specklepy.transports.abstract_transport import AbstractTransport
from specklepy.transports.sqlite import SQLiteTransport
from specklepy.core.api.operations import (send as core_send,
receive as _untracked_receive,
serialize as core_serialize,
deserialize as core_deserialize)
def send(
base: Base,
transports: List[AbstractTransport] = None,
transports: Optional[List[AbstractTransport]] = None,
use_default_cache: bool = True,
):
"""Sends an object via the provided transports. Defaults to the local cache.
@@ -17,51 +23,25 @@ def send(
Arguments:
obj {Base} -- the object you want to send
transports {list} -- where you want to send them
use_default_cache {bool} -- toggle for the default cache. If set to false, it will only send to the provided transports
use_default_cache {bool} -- toggle for the default cache.
If set to false, it will only send to the provided transports
Returns:
str -- the object id of the sent object
"""
if not transports and not use_default_cache:
raise SpeckleException(
message="You need to provide at least one transport: cannot send with an empty transport list and no default cache"
)
if isinstance(transports, AbstractTransport):
transports = [transports]
if transports is None:
metrics.track(metrics.SEND)
transports = []
else:
metrics.track(metrics.SEND, getattr(transports[0], "account", None))
if use_default_cache:
transports.insert(0, SQLiteTransport())
serializer = BaseObjectSerializer(write_transports=transports)
obj_hash, _ = serializer.write_json(base=base)
return obj_hash
return core_send(base, transports, use_default_cache)
def receive(
obj_id: str,
remote_transport: AbstractTransport = None,
local_transport: AbstractTransport = None,
remote_transport: Optional[AbstractTransport] = None,
local_transport: Optional[AbstractTransport] = None,
) -> Base:
metrics.track(metrics.RECEIVE, getattr(remote_transport, "account", None))
return _untracked_receive(obj_id, remote_transport, local_transport)
def _untracked_receive(
obj_id: str,
remote_transport: AbstractTransport = None,
local_transport: AbstractTransport = None,
) -> Base:
"""Receives an object from a transport.
Arguments:
@@ -73,65 +53,48 @@ def _untracked_receive(
Returns:
Base -- the base object
"""
if not local_transport:
local_transport = SQLiteTransport()
serializer = BaseObjectSerializer(read_transport=local_transport)
# try local transport first. if the parent is there, we assume all the children are there and continue with deserialization using the local transport
obj_string = local_transport.get_object(obj_id)
if obj_string:
return serializer.read_json(obj_string=obj_string)
if not remote_transport:
raise SpeckleException(
message="Could not find the specified object using the local transport, and you didn't provide a fallback remote from which to pull it."
)
obj_string = remote_transport.copy_object_and_children(
id=obj_id, target_transport=local_transport
)
return serializer.read_json(obj_string=obj_string)
metrics.track(metrics.RECEIVE, getattr(remote_transport, "account", None))
return _untracked_receive(obj_id, remote_transport, local_transport)
def serialize(base: Base, write_transports: List[AbstractTransport] = []) -> str:
"""
Serialize a base object. If no write transports are provided, the object will be serialized
Serialize a base object. If no write transports are provided,
the object will be serialized
without detaching or chunking any of the attributes.
Arguments:
base {Base} -- the object to serialize
write_transports {List[AbstractTransport]} -- optional: the transports to write to
write_transports {List[AbstractTransport]}
-- optional: the transports to write to
Returns:
str -- the serialized object
"""
metrics.track(metrics.SERIALIZE)
serializer = BaseObjectSerializer(write_transports=write_transports)
metrics.track(metrics.SDK, custom_props={"name": "Serialize"})
return core_serialize(base, write_transports)
return serializer.write_json(base)[1]
def deserialize(obj_string: str, read_transport: AbstractTransport = None) -> Base:
def deserialize(
obj_string: str, read_transport: Optional[AbstractTransport] = None
) -> Base:
"""
Deserialize a string object into a Base object. If the object contains referenced child objects that are not stored in the local db, a read transport needs to be provided in order to recompose the base with the children objects.
Deserialize a string object into a Base object.
If the object contains referenced child objects that are not stored in the local db,
a read transport needs to be provided in order to recompose
the base with the children objects.
Arguments:
obj_string {str} -- the string object to deserialize
read_transport {AbstractTransport} -- the transport to fetch children objects from
(defaults to SQLiteTransport)
read_transport {AbstractTransport}
-- the transport to fetch children objects from
(defaults to SQLiteTransport)
Returns:
Base -- the deserialized object
"""
metrics.track(metrics.DESERIALIZE)
if not read_transport:
read_transport = SQLiteTransport()
serializer = BaseObjectSerializer(read_transport=read_transport)
return serializer.read_json(obj_string=obj_string)
metrics.track(metrics.SDK, custom_props={"name": "Deserialize"})
return core_deserialize(obj_string, read_transport)
__all__ = [receive.__name__, send.__name__, serialize.__name__, deserialize.__name__]
__all__ = ["receive", "send", "serialize", "deserialize"]
+18 -97
View File
@@ -1,18 +1,25 @@
from graphql import DocumentNode
from specklepy.api.credentials import Account
from specklepy.transports.sqlite import SQLiteTransport
from threading import Lock
from typing import Any, Dict, List, Optional, Tuple, Type, Union
from gql.client import Client
from gql.transport.exceptions import TransportQueryError
from graphql import DocumentNode
from specklepy.api.credentials import Account
from specklepy.logging.exceptions import (
GraphQLException,
SpeckleException,
UnsupportedException,
)
from specklepy.serialization.base_object_serializer import BaseObjectSerializer
from specklepy.transports.sqlite import SQLiteTransport
# following imports seem to be unnecessary, but they need to stay
# to not break the scripts using these functions as non-core
from specklepy.core.api.resource import ResourceBase as CoreResourceBase
class ResourceBase(object):
class ResourceBase(CoreResourceBase):
def __init__(
self,
account: Account,
@@ -21,97 +28,11 @@ class ResourceBase(object):
name: str,
server_version: Optional[Tuple[Any, ...]] = None,
) -> None:
self.account = account
self.basepath = basepath
self.client = client
self.name = name
self.server_version = server_version
self.schema: Optional[Type] = None
def _step_into_response(self, response: dict, return_type: Union[str, List, None]):
"""Step into the dict to get the relevant data"""
if return_type is None:
return response
if isinstance(return_type, str):
return response[return_type]
if isinstance(return_type, List):
for key in return_type:
response = response[key]
return response
def _parse_response(self, response: Union[dict, list, None], schema=None):
"""Try to create a class instance from the response"""
if response is None:
return None
if isinstance(response, list):
return [self._parse_response(response=r, schema=schema) for r in response]
if schema:
return schema.parse_obj(response)
elif self.schema:
try:
return self.schema.parse_obj(response)
except:
s = BaseObjectSerializer(read_transport=SQLiteTransport())
return s.recompose_base(response)
else:
return response
def make_request(
self,
query: DocumentNode,
params: Dict = None,
return_type: Union[str, List, None] = None,
schema=None,
parse_response: bool = True,
) -> Any:
"""Executes the GraphQL query"""
try:
response = self.client.execute(query, variable_values=params)
except Exception as ex:
if isinstance(ex, TransportQueryError):
return GraphQLException(
message=f"Failed to execute the GraphQL {self.name} request. Errors: {ex.errors}",
errors=ex.errors,
data=ex.data,
)
else:
return SpeckleException(
message=f"Failed to execute the GraphQL {self.name} request. Inner exception: {ex}",
exception=ex,
)
response = self._step_into_response(response=response, return_type=return_type)
if parse_response:
return self._parse_response(response=response, schema=schema)
else:
return response
def _check_server_version_at_least(
self, target_version: Tuple[Any, ...], unsupported_message: str = None
):
"""Use this check to guard against making unsupported requests on older servers.
Arguments:
target_version {tuple} -- the minimum server version in the format (major, minor, patch, (tag, build))
eg (2, 6, 3) for a stable build and (2, 6, 4, 'alpha', 4711) for alpha
"""
if not unsupported_message:
unsupported_message = f"The client method used is not supported on Speckle Server versions prior to v{'.'.join(target_version)}"
# if version is dev, it should be supported... (or not)
if self.server_version == ("dev",):
return
if self.server_version and self.server_version < target_version:
raise UnsupportedException(unsupported_message)
def _check_invites_supported(self):
"""Invites are only supported for Speckle Server >= 2.6.4.
Use this check to guard against making unsupported requests on older servers.
"""
self._check_server_version_at_least(
(2, 6, 4),
(
"Stream invites are only supported as of Speckle Server v2.6.4. "
"Please update your Speckle Server to use this method or use the `grant_permission` flow instead."
),
super().__init__(
account = account,
basepath = basepath,
client = client,
name = name,
server_version = server_version
)
+2 -5
View File
@@ -1,11 +1,8 @@
from pathlib import Path
import sys
import pkgutil
import sys
from importlib import import_module
for (_, name, _) in pkgutil.iter_modules(__path__):
for _, name, _ in pkgutil.iter_modules(__path__):
imported_module = import_module("." + name, package=__name__)
if hasattr(imported_module, "Resource"):
+37 -161
View File
@@ -1,16 +1,17 @@
from typing import List, Optional
from datetime import datetime, timezone
from typing import List, Optional
from gql import gql
from specklepy.api.models import ActivityCollection, PendingStreamCollaborator, User
from specklepy.api.resource import ResourceBase
from specklepy.logging import metrics
from specklepy.logging.exceptions import SpeckleException
from specklepy.api.resource import ResourceBase
from specklepy.api.models import ActivityCollection, PendingStreamCollaborator, User
from specklepy.core.api.resources.active_user import Resource as CoreResource
NAME = "active_user"
class Resource(ResourceBase):
class Resource(CoreResource):
"""API Access class for users"""
def __init__(self, account, basepath, client, server_version) -> None:
@@ -18,13 +19,14 @@ class Resource(ResourceBase):
account=account,
basepath=basepath,
client=client,
name=NAME,
server_version=server_version,
)
self.schema = User
def get(self) -> User:
"""Gets the profile of a user. If no id argument is provided, will return the current authenticated user's profile (as extracted from the authorization header).
"""Gets the profile of a user. If no id argument is provided,
will return the current authenticated user's profile
(as extracted from the authorization header).
Arguments:
id {str} -- the user id
@@ -32,28 +34,8 @@ class Resource(ResourceBase):
Returns:
User -- the retrieved user
"""
metrics.track(metrics.USER, self.account, {"name": "get"})
query = gql(
"""
query User {
activeUser {
id
email
name
bio
company
avatar
verified
profiles
role
}
}
"""
)
params = {}
return self.make_request(query=query, params=params, return_type="activeUser")
metrics.track(metrics.SDK, custom_props={"name": "User Active Get"})
return super().get()
def update(
self,
@@ -73,26 +55,8 @@ class Resource(ResourceBase):
Returns @deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT):
bool -- True if your profile was updated successfully
"""
metrics.track(metrics.USER, self.account, {"name": "update"})
query = gql(
"""
mutation UserUpdate($user: UserUpdateInput!) {
userUpdate(user: $user)
}
"""
)
params = {"name": name, "company": company, "bio": bio, "avatar": avatar}
params = {"user": {k: v for k, v in params.items() if v is not None}}
if not params["user"]:
return SpeckleException(
message="You must provide at least one field to update your user profile"
)
return self.make_request(
query=query, params=params, return_type="userUpdate", parse_response=False
)
metrics.track(metrics.SDK, self.account, {"name": "User Active Update"})
return super().update(name, company, bio, avatar)
def activity(
self,
@@ -103,56 +67,26 @@ class Resource(ResourceBase):
cursor: Optional[datetime] = None,
):
"""
Get the activity from a given stream in an Activity collection. Step into the activity `items` for the list of activity.
If no id argument is provided, will return the current authenticated user's activity (as extracted from the authorization header).
Get the activity from a given stream in an Activity collection.
Step into the activity `items` for the list of activity.
If no id argument is provided, will return the current authenticated user's
activity (as extracted from the authorization header).
Note: all timestamps arguments should be `datetime` of any tz as they will be converted to UTC ISO format strings
Note: all timestamps arguments should be `datetime` of any tz as they will be
converted to UTC ISO format strings
user_id {str} -- the id of the user to get the activity from
action_type {str} -- filter results to a single action type (eg: `commit_create` or `commit_receive`)
action_type {str} -- filter results to a single action type
(eg: `commit_create` or `commit_receive`)
limit {int} -- max number of Activity items to return
before {datetime} -- latest cutoff for activity (ie: return all activity _before_ this time)
after {datetime} -- oldest cutoff for activity (ie: return all activity _after_ this time)
before {datetime} -- latest cutoff for activity
(ie: return all activity _before_ this time)
after {datetime} -- oldest cutoff for activity
(ie: return all activity _after_ this time)
cursor {datetime} -- timestamp cursor for pagination
"""
query = gql(
"""
query UserActivity($action_type: String, $before:DateTime, $after: DateTime, $cursor: DateTime, $limit: Int){
activeUser {
activity(actionType: $action_type, before: $before, after: $after, cursor: $cursor, limit: $limit) {
totalCount
cursor
items {
actionType
info
userId
streamId
resourceId
resourceType
message
time
}
}
}
}
"""
)
params = {
"limit": limit,
"action_type": action_type,
"before": before.astimezone(timezone.utc).isoformat() if before else before,
"after": after.astimezone(timezone.utc).isoformat() if after else after,
"cursor": cursor.astimezone(timezone.utc).isoformat() if cursor else cursor,
}
return self.make_request(
query=query,
params=params,
return_type=["activeUser", "activity"],
schema=ActivityCollection,
)
metrics.track(metrics.SDK, self.account, {"name": "User Active Activity"})
return super().activity(limit, action_type, before, after, cursor)
def get_all_pending_invites(self) -> List[PendingStreamCollaborator]:
"""Get all of the active user's pending stream invites
@@ -160,38 +94,11 @@ class Resource(ResourceBase):
Requires Speckle Server version >= 2.6.4
Returns:
List[PendingStreamCollaborator] -- a list of pending invites for the current user
List[PendingStreamCollaborator]
-- a list of pending invites for the current user
"""
metrics.track(metrics.INVITE, self.account, {"name": "get"})
self._check_invites_supported()
query = gql(
"""
query StreamInvites {
streamInvites{
id
token
inviteId
streamId
streamName
title
role
invitedBy {
id
name
company
avatar
}
}
}
"""
)
return self.make_request(
query=query,
return_type="streamInvites",
schema=PendingStreamCollaborator,
)
metrics.track(metrics.SDK, self.account, {"name": "User Active Invites All Get"})
return super().get_all_pending_invites()
def get_pending_invite(
self, stream_id: str, token: Optional[str] = None
@@ -206,39 +113,8 @@ class Resource(ResourceBase):
token {str} -- the token of the invite to look for (optional)
Returns:
PendingStreamCollaborator -- the invite for the given stream (or None if it isn't found)
PendingStreamCollaborator
-- the invite for the given stream (or None if it isn't found)
"""
metrics.track(metrics.INVITE, self.account, {"name": "get"})
self._check_invites_supported()
query = gql(
"""
query StreamInvite($streamId: String!, $token: String) {
streamInvite(streamId: $streamId, token: $token) {
id
token
streamId
streamName
title
role
invitedBy {
id
name
company
avatar
}
}
}
"""
)
params = {"streamId": stream_id}
if token:
params["token"] = token
return self.make_request(
query=query,
params=params,
return_type="streamInvite",
schema=PendingStreamCollaborator,
)
metrics.track(metrics.SDK, self.account, {"name": "User Active Invite Get"})
return super().get_pending_invite(stream_id, token)
+21 -137
View File
@@ -1,12 +1,15 @@
from typing import Optional
from gql import gql
from specklepy.api.resource import ResourceBase
from specklepy.api.models import Branch
from specklepy.api.resource import ResourceBase
from specklepy.logging import metrics
NAME = "branch"
from specklepy.core.api.resources.branch import Resource as CoreResource
class Resource(ResourceBase):
class Resource(CoreResource):
"""API Access class for branches"""
def __init__(self, account, basepath, client) -> None:
@@ -14,7 +17,6 @@ class Resource(ResourceBase):
account=account,
basepath=basepath,
client=client,
name=NAME,
)
self.schema = Branch
@@ -30,25 +32,8 @@ class Resource(ResourceBase):
Returns:
id {str} -- the newly created branch's id
"""
metrics.track(metrics.BRANCH, self.account, {"name": "create"})
query = gql(
"""
mutation BranchCreate($branch: BranchCreateInput!) {
branchCreate(branch: $branch)
}
"""
)
params = {
"branch": {
"streamId": stream_id,
"name": name,
"description": description,
}
}
return self.make_request(
query=query, params=params, return_type="branchCreate", parse_response=False
)
metrics.track(metrics.SDK, self.account, {"name": "Branch Create"})
return super().create(stream_id, name, description)
def get(self, stream_id: str, name: str, commits_limit: int = 10):
"""Get a branch by name from a stream
@@ -61,42 +46,8 @@ class Resource(ResourceBase):
Returns:
Branch -- the fetched branch with its latest commits
"""
metrics.track(metrics.BRANCH, self.account, {"name": "get"})
query = gql(
"""
query BranchGet($stream_id: String!, $name: String!, $commits_limit: Int!) {
stream(id: $stream_id) {
branch(name: $name) {
id,
name,
description,
commits (limit: $commits_limit) {
totalCount,
cursor,
items {
id,
referencedObject,
sourceApplication,
totalChildrenCount,
message,
authorName,
authorId,
branchName,
parents,
createdAt
}
}
}
}
}
"""
)
params = {"stream_id": stream_id, "name": name, "commits_limit": commits_limit}
return self.make_request(
query=query, params=params, return_type=["stream", "branch"]
)
metrics.track(metrics.SDK, self.account, {"name": "Branch Get"})
return super().get(stream_id, name, commits_limit)
def list(self, stream_id: str, branches_limit: int = 10, commits_limit: int = 10):
"""Get a list of branches from a given stream
@@ -109,49 +60,15 @@ class Resource(ResourceBase):
Returns:
List[Branch] -- the branches on the stream
"""
metrics.track(metrics.BRANCH, self.account, {"name": "get"})
query = gql(
"""
query BranchesGet($stream_id: String!, $branches_limit: Int!, $commits_limit: Int!) {
stream(id: $stream_id) {
branches(limit: $branches_limit) {
items {
id
name
description
commits(limit: $commits_limit) {
totalCount
items{
id
message
referencedObject
sourceApplication
parents
authorId
authorName
branchName
createdAt
}
}
}
}
}
}
"""
)
params = {
"stream_id": stream_id,
"branches_limit": branches_limit,
"commits_limit": commits_limit,
}
return self.make_request(
query=query, params=params, return_type=["stream", "branches", "items"]
)
metrics.track(metrics.SDK, self.account, {"name": "Branch List"})
return super().list(stream_id, branches_limit, commits_limit)
def update(
self, stream_id: str, branch_id: str, name: str = None, description: str = None
self,
stream_id: str,
branch_id: str,
name: Optional[str] = None,
description: Optional[str] = None,
):
"""Update a branch
@@ -164,29 +81,8 @@ class Resource(ResourceBase):
Returns:
bool -- True if update is successful
"""
metrics.track(metrics.BRANCH, self.account, {"name": "update"})
query = gql(
"""
mutation BranchUpdate($branch: BranchUpdateInput!) {
branchUpdate(branch: $branch)
}
"""
)
params = {
"branch": {
"streamId": stream_id,
"id": branch_id,
}
}
if name:
params["branch"]["name"] = name
if description:
params["branch"]["description"] = description
return self.make_request(
query=query, params=params, return_type="branchUpdate", parse_response=False
)
metrics.track(metrics.SDK, self.account, {"name": "Branch Update"})
return super().update(stream_id, branch_id, name, description)
def delete(self, stream_id: str, branch_id: str):
"""Delete a branch
@@ -198,17 +94,5 @@ class Resource(ResourceBase):
Returns:
bool -- True if deletion is successful
"""
metrics.track(metrics.BRANCH, self.account, {"name": "delete"})
query = gql(
"""
mutation BranchDelete($branch: BranchDeleteInput!) {
branchDelete(branch: $branch)
}
"""
)
params = {"branch": {"streamId": stream_id, "id": branch_id}}
return self.make_request(
query=query, params=params, return_type="branchDelete", parse_response=False
)
metrics.track(metrics.SDK, self.account, {"name": "Branch Delete"})
return super().delete(stream_id, branch_id)
+29 -139
View File
@@ -1,14 +1,15 @@
from typing import Optional, List
from typing import List, Optional
from gql import gql
from specklepy.api.resource import ResourceBase
from specklepy.api.models import Commit
from specklepy.api.resource import ResourceBase
from specklepy.logging import metrics
NAME = "commit"
from specklepy.core.api.resources.commit import Resource as CoreResource
class Resource(ResourceBase):
class Resource(CoreResource):
"""API Access class for commits"""
def __init__(self, account, basepath, client) -> None:
@@ -16,7 +17,6 @@ class Resource(ResourceBase):
account=account,
basepath=basepath,
client=client,
name=NAME,
)
self.schema = Commit
@@ -31,32 +31,8 @@ class Resource(ResourceBase):
Returns:
Commit -- the retrieved commit object
"""
query = gql(
"""
query Commit($stream_id: String!, $commit_id: String!) {
stream(id: $stream_id) {
commit(id: $commit_id) {
id
message
referencedObject
authorId
authorName
authorAvatar
branchName
createdAt
sourceApplication
totalChildrenCount
parents
}
}
}
"""
)
params = {"stream_id": stream_id, "commit_id": commit_id}
return self.make_request(
query=query, params=params, return_type=["stream", "commit"]
)
metrics.track(metrics.SDK, self.account, {"name": "Commit Get"})
return super().get(stream_id, commit_id)
def list(self, stream_id: str, limit: int = 10) -> List[Commit]:
"""
@@ -69,36 +45,8 @@ class Resource(ResourceBase):
Returns:
List[Commit] -- a list of the most recent commit objects
"""
metrics.track(metrics.COMMIT, self.account, {"name": "get"})
query = gql(
"""
query Commits($stream_id: String!, $limit: Int!) {
stream(id: $stream_id) {
commits(limit: $limit) {
items {
id
message
referencedObject
authorName
authorId
authorName
authorAvatar
branchName
createdAt
sourceApplication
totalChildrenCount
parents
}
}
}
}
"""
)
params = {"stream_id": stream_id, "limit": limit}
return self.make_request(
query=query, params=params, return_type=["stream", "commits", "items"]
)
metrics.track(metrics.SDK, self.account, {"name": "Commit List"})
return super().list(stream_id, limit)
def create(
self,
@@ -115,84 +63,51 @@ class Resource(ResourceBase):
Arguments:
stream_id {str} -- the stream you want to commit to
object_id {str} -- the hash of your commit object
branch_name {str} -- the name of the branch to commit to (defaults to "main")
message {str} -- optional: a message to give more information about the commit
source_application{str} -- optional: the application from which the commit was created (defaults to "python")
branch_name {str}
-- the name of the branch to commit to (defaults to "main")
message {str}
-- optional: a message to give more information about the commit
source_application{str}
-- optional: the application from which the commit was created
(defaults to "python")
parents {List[str]} -- optional: the id of the parent commits
Returns:
str -- the id of the created commit
"""
metrics.track(metrics.COMMIT, self.account, {"name": "create"})
query = gql(
"""
mutation CommitCreate ($commit: CommitCreateInput!){ commitCreate(commit: $commit)}
"""
)
params = {
"commit": {
"streamId": stream_id,
"branchName": branch_name,
"objectId": object_id,
"message": message,
"sourceApplication": source_application,
}
}
if parents:
params["commit"]["parents"] = parents
return self.make_request(
query=query, params=params, return_type="commitCreate", parse_response=False
)
metrics.track(metrics.SDK, self.account, {"name": "Commit Create"})
return super().create(stream_id, object_id, branch_name, message, source_application, parents)
def update(self, stream_id: str, commit_id: str, message: str) -> bool:
"""
Update a commit
Arguments:
stream_id {str} -- the id of the stream that contains the commit you'd like to update
stream_id {str}
-- the id of the stream that contains the commit you'd like to update
commit_id {str} -- the id of the commit you'd like to update
message {str} -- the updated commit message
Returns:
bool -- True if the operation succeeded
"""
metrics.track(metrics.COMMIT, self.account, {"name": "update"})
query = gql(
"""
mutation CommitUpdate($commit: CommitUpdateInput!){ commitUpdate(commit: $commit)}
"""
)
params = {
"commit": {"streamId": stream_id, "id": commit_id, "message": message}
}
return self.make_request(
query=query, params=params, return_type="commitUpdate", parse_response=False
)
metrics.track(metrics.SDK, self.account, {"name": "Commit Update"})
return super().update(stream_id, commit_id, message)
def delete(self, stream_id: str, commit_id: str) -> bool:
"""
Delete a commit
Arguments:
stream_id {str} -- the id of the stream that contains the commit you'd like to delete
stream_id {str}
-- the id of the stream that contains the commit you'd like to delete
commit_id {str} -- the id of the commit you'd like to delete
Returns:
bool -- True if the operation succeeded
"""
metrics.track(metrics.COMMIT, self.account, {"name": "delete"})
query = gql(
"""
mutation CommitDelete($commit: CommitDeleteInput!){ commitDelete(commit: $commit)}
"""
)
params = {"commit": {"streamId": stream_id, "id": commit_id}}
return self.make_request(
query=query, params=params, return_type="commitDelete", parse_response=False
)
metrics.track(metrics.SDK, self.account, {"name": "Commit Delete"})
return super().delete(stream_id, commit_id)
def received(
self,
@@ -204,30 +119,5 @@ class Resource(ResourceBase):
"""
Mark a commit object a received by the source application.
"""
metrics.track(metrics.COMMIT, self.account, {"name": "received"})
query = gql(
"""
mutation CommitReceive($receivedInput:CommitReceivedInput!){
commitReceive(input:$receivedInput)
}
"""
)
params = {
"receivedInput": {
"sourceApplication": source_application,
"streamId": stream_id,
"commitId": commit_id,
"message": "message",
}
}
try:
return self.make_request(
query=query,
params=params,
return_type="commitReceive",
parse_response=False,
)
except Exception as ex:
print(ex.with_traceback)
return False
metrics.track(metrics.SDK, self.account, {"name": "Commit Received"})
return super().received(stream_id, commit_id, source_application, message)
+20 -42
View File
@@ -1,12 +1,16 @@
from typing import Dict, List
from gql import gql
from specklepy.api.resource import ResourceBase
from specklepy.objects.base import Base
NAME = "object"
from specklepy.logging import metrics
from specklepy.core.api.resources.object import Resource as CoreResource
class Resource(ResourceBase):
class Resource(CoreResource):
"""API Access class for objects"""
def __init__(self, account, basepath, client) -> None:
@@ -14,7 +18,6 @@ class Resource(ResourceBase):
account=account,
basepath=basepath,
client=client,
name=NAME,
)
self.schema = Base
@@ -29,55 +32,30 @@ class Resource(ResourceBase):
Returns:
Base -- the returned Base object
"""
query = gql(
"""
query Object($stream_id: String!, $object_id: String!) {
stream(id: $stream_id) {
id
name
object(id: $object_id) {
id
speckleType
applicationId
createdAt
totalChildrenCount
data
}
}
}
"""
)
params = {"stream_id": stream_id, "object_id": object_id}
return self.make_request(
query=query,
params=params,
return_type=["stream", "object", "data"],
)
metrics.track(metrics.SDK, self.account, {"name": "Object Get"})
return super().get(stream_id, object_id)
def create(self, stream_id: str, objects: List[Dict]) -> str:
"""
Not advised - generally, you want to use `operations.send()`.
Create a new object on a stream. To send a base object, you can prepare it by running it through the
`BaseObjectSerializer.traverse_base()` function to get a valid (serialisable) object to send.
Create a new object on a stream.
To send a base object, you can prepare it by running it through the
`BaseObjectSerializer.traverse_base()` function to get a valid (serialisable)
object to send.
NOTE: this does not create a commit - you can create one with `SpeckleClient.commit.create`. Dynamic fields will be located in the 'data' dict of the received `Base` object
NOTE: this does not create a commit - you can create one with
`SpeckleClient.commit.create`.
Dynamic fields will be located in the 'data' dict of the received `Base` object
Arguments:
stream_id {str} -- the id of the stream you want to send the object to
objects {List[Dict]} -- a list of base dictionary objects (NOTE: must be json serialisable)
objects {List[Dict]}
-- a list of base dictionary objects (NOTE: must be json serialisable)
Returns:
str -- the id of the object
"""
query = gql(
"""
mutation ObjectCreate($object_input: ObjectCreateInput!) { objectCreate(objectInput: $object_input) }
"""
)
params = {"object_input": {"streamId": stream_id, "objects": objects}}
return self.make_request(
query=query, params=params, return_type="objectCreate", parse_response=False
)
metrics.track(metrics.SDK, self.account, {"name": "Object Create"})
return super().create(stream_id, objects)
+26 -96
View File
@@ -1,18 +1,17 @@
from typing import List, Optional, Union
from datetime import datetime, timezone
from typing import List, Optional, Union
from gql import gql
from specklepy.api.models import ActivityCollection, LimitedUser
from specklepy.api.resource import ResourceBase
from specklepy.logging import metrics
from specklepy.logging.exceptions import SpeckleException
from specklepy.api.resource import ResourceBase
from specklepy.api.models import (
ActivityCollection,
LimitedUser,
)
NAME = "other_user"
from specklepy.core.api.resources.other_user import Resource as CoreResource
class Resource(ResourceBase):
class Resource(CoreResource):
"""API Access class for other users, that are not the currently active user."""
def __init__(self, account, basepath, client, server_version) -> None:
@@ -20,7 +19,6 @@ class Resource(ResourceBase):
account=account,
basepath=basepath,
client=client,
name=NAME,
server_version=server_version,
)
self.schema = LimitedUser
@@ -35,31 +33,14 @@ class Resource(ResourceBase):
Returns:
LimitedUser -- the retrieved profile of another user
"""
metrics.track(metrics.OTHER_USER, self.account, {"name": "get"})
query = gql(
"""
query OtherUser($id: String!) {
otherUser(id: $id) {
id
name
bio
company
avatar
verified
role
}
}
"""
)
params = {"id": id}
return self.make_request(query=query, params=params, return_type="otherUser")
metrics.track(metrics.SDK, self.account, {"name": "Other User Get"})
return super().get(id)
def search(
self, search_query: str, limit: int = 25
) -> Union[List[LimitedUser], SpeckleException]:
"""Searches for user by name or email. The search query must be at least 3 characters long
"""Searches for user by name or email. The search query must be at least
3 characters long
Arguments:
search_query {str} -- a string to search for
@@ -72,28 +53,8 @@ class Resource(ResourceBase):
message="User search query must be at least 3 characters"
)
metrics.track(metrics.OTHER_USER, self.account, {"name": "search"})
query = gql(
"""
query UserSearch($search_query: String!, $limit: Int!) {
userSearch(query: $search_query, limit: $limit) {
items {
id
name
bio
company
avatar
verified
}
}
}
"""
)
params = {"search_query": search_query, "limit": limit}
return self.make_request(
query=query, params=params, return_type=["userSearch", "items"]
)
metrics.track(metrics.SDK, self.account, {"name": "Other User Search"})
return super().search(search_query, limit)
def activity(
self,
@@ -105,53 +66,22 @@ class Resource(ResourceBase):
cursor: Optional[datetime] = None,
) -> ActivityCollection:
"""
Get the activity from a given stream in an Activity collection. Step into the activity `items` for the list of activity.
Get the activity from a given stream in an Activity collection.
Step into the activity `items` for the list of activity.
Note: all timestamps arguments should be `datetime` of any tz as they will be converted to UTC ISO format strings
Note: all timestamps arguments should be `datetime` of
any tz as they will be converted to UTC ISO format strings
user_id {str} -- the id of the user to get the activity from
action_type {str} -- filter results to a single action type (eg: `commit_create` or `commit_receive`)
action_type {str} -- filter results to a single action type
(eg: `commit_create` or `commit_receive`)
limit {int} -- max number of Activity items to return
before {datetime} -- latest cutoff for activity (ie: return all activity _before_ this time)
after {datetime} -- oldest cutoff for activity (ie: return all activity _after_ this time)
before {datetime} -- latest cutoff for activity
(ie: return all activity _before_ this time)
after {datetime} -- oldest cutoff for activity
(ie: return all activity _after_ this time)
cursor {datetime} -- timestamp cursor for pagination
"""
query = gql(
"""
query UserActivity($user_id: String!, $action_type: String, $before:DateTime, $after: DateTime, $cursor: DateTime, $limit: Int){
otherUser(id: $user_id) {
activity(actionType: $action_type, before: $before, after: $after, cursor: $cursor, limit: $limit) {
totalCount
cursor
items {
actionType
info
userId
streamId
resourceId
resourceType
message
time
}
}
}
}
"""
)
params = {
"user_id": user_id,
"limit": limit,
"action_type": action_type,
"before": before.astimezone(timezone.utc).isoformat() if before else before,
"after": after.astimezone(timezone.utc).isoformat() if after else after,
"cursor": cursor.astimezone(timezone.utc).isoformat() if cursor else cursor,
}
return self.make_request(
query=query,
params=params,
return_type=["otherUser", "activity"],
schema=ActivityCollection,
)
metrics.track(metrics.SDK, self.account, {"name": "Other User Activity"})
return super().activity(user_id, limit, action_type, before, after, cursor)
+15 -117
View File
@@ -1,16 +1,17 @@
import re
from typing import Any, Dict, List, Tuple
from gql import gql
from specklepy.api.models import ServerInfo
from specklepy.api.resource import ResourceBase
from specklepy.logging import metrics
from specklepy.logging.exceptions import GraphQLException
NAME = "server"
from specklepy.core.api.resources.server import Resource as CoreResource
class Resource(ResourceBase):
class Resource(CoreResource):
"""API Access class for the server"""
def __init__(self, account, basepath, client) -> None:
@@ -18,7 +19,6 @@ class Resource(ResourceBase):
account=account,
basepath=basepath,
client=client,
name=NAME,
)
def get(self) -> ServerInfo:
@@ -27,72 +27,18 @@ class Resource(ResourceBase):
Returns:
dict -- the server info in dictionary form
"""
metrics.track(metrics.SERVER, self.account, {"name": "get"})
query = gql(
"""
query Server {
serverInfo {
name
company
description
adminContact
canonicalUrl
version
roles {
name
description
resourceTarget
}
scopes {
name
description
}
authStrategies{
id
name
icon
}
}
}
"""
)
return self.make_request(
query=query, return_type="serverInfo", schema=ServerInfo
)
metrics.track(metrics.SDK, self.account, {"name": "Server Get"})
return super().get()
def version(self) -> Tuple[Any, ...]:
"""Get the server version
Returns:
tuple -- the server version in the format (major, minor, patch, (tag, build))
eg (2, 6, 3) for a stable build and (2, 6, 4, 'alpha', 4711) for alpha
the server version in the format (major, minor, patch, (tag, build))
eg (2, 6, 3) for a stable build and (2, 6, 4, 'alpha', 4711) for alpha
"""
# not tracking as it will be called along with other mutations / queries as a check
query = gql(
"""
query Server {
serverInfo {
version
}
}
"""
)
ver = self.make_request(
query=query, return_type=["serverInfo", "version"], parse_response=False
)
if isinstance(ver, Exception):
raise GraphQLException(
f"Could not get server version for {self.basepath}", [ver]
)
# pylint: disable=consider-using-generator; (list comp is faster)
return tuple(
[
int(segment) if segment.isdigit() else segment
for segment in re.split(r"\.|-", ver)
]
)
return super().version()
def apps(self) -> Dict:
"""Get the apps registered on the server
@@ -100,28 +46,8 @@ class Resource(ResourceBase):
Returns:
dict -- a dictionary of apps registered on the server
"""
metrics.track(metrics.SERVER, self.account, {"name": "apps"})
query = gql(
"""
query Apps {
apps{
id
name
description
termsAndConditionsLink
trustByDefault
logo
author {
id
name
avatar
}
}
}
"""
)
return self.make_request(query=query, return_type="apps", parse_response=False)
metrics.track(metrics.SDK, self.account, {"name": "Server Apps"})
return super().apps()
def create_token(self, name: str, scopes: List[str], lifespan: int) -> str:
"""Create a personal API token
@@ -134,22 +60,8 @@ class Resource(ResourceBase):
Returns:
str -- the new API token. note: this is the only time you'll see the token!
"""
metrics.track(metrics.SERVER, self.account, {"name": "create_token"})
query = gql(
"""
mutation TokenCreate($token: ApiTokenCreateInput!) {
apiTokenCreate(token: $token)
}
"""
)
params = {"token": {"scopes": scopes, "name": name, "lifespan": lifespan}}
return self.make_request(
query=query,
params=params,
return_type="apiTokenCreate",
parse_response=False,
)
metrics.track(metrics.SDK, self.account, {"name": "Server Create Token"})
return super().create_token(name, scopes, lifespan)
def revoke_token(self, token: str) -> bool:
"""Revokes (deletes) a personal API token
@@ -160,19 +72,5 @@ class Resource(ResourceBase):
Returns:
bool -- True if the token was successfully deleted
"""
metrics.track(metrics.SERVER, self.account, {"name": "revoke_token"})
query = gql(
"""
mutation TokenRevoke($token: String!) {
apiTokenRevoke(token: $token)
}
"""
)
params = {"token": token}
return self.make_request(
query=query,
params=params,
return_type="apiTokenRevoke",
parse_response=False,
)
metrics.track(metrics.SDK, self.account, {"name": "Server Revoke Token"})
return super().revoke_token(token)
+84 -555
View File
@@ -1,17 +1,18 @@
from datetime import datetime, timezone
from typing import List, Optional
from deprecated import deprecated
from gql import gql
from specklepy.logging import metrics
from specklepy.api.models import ActivityCollection, PendingStreamCollaborator, Stream
from specklepy.api.resource import ResourceBase
from specklepy.logging.exceptions import UnsupportedException, SpeckleException
from specklepy.logging import metrics
from specklepy.logging.exceptions import SpeckleException, UnsupportedException
from specklepy.core.api.resources.stream import Resource as CoreResource
NAME = "stream"
class Resource(ResourceBase):
class Resource(CoreResource):
"""API Access class for streams"""
def __init__(self, account, basepath, client, server_version) -> None:
@@ -19,7 +20,6 @@ class Resource(ResourceBase):
account=account,
basepath=basepath,
client=client,
name=NAME,
server_version=server_version,
)
@@ -36,56 +36,8 @@ class Resource(ResourceBase):
Returns:
Stream -- the retrieved stream
"""
metrics.track(metrics.STREAM, self.account, {"name": "get"})
query = gql(
"""
query Stream($id: String!, $branch_limit: Int!, $commit_limit: Int!) {
stream(id: $id) {
id
name
role
description
isPublic
createdAt
updatedAt
commentCount
favoritesCount
collaborators {
id
name
role
avatar
}
branches(limit: $branch_limit) {
totalCount
cursor
items {
id
name
description
commits(limit: $commit_limit) {
totalCount
cursor
items {
id
message
authorId
createdAt
authorName
referencedObject
sourceApplication
}
}
}
}
}
}
"""
)
params = {"id": id, "branch_limit": branch_limit, "commit_limit": commit_limit}
return self.make_request(query=query, params=params, return_type="stream")
metrics.track(metrics.SDK, self.account, {"name": "Stream Get"})
return super().get(id, branch_limit, commit_limit)
def list(self, stream_limit: int = 10) -> List[Stream]:
"""Get a list of the user's streams
@@ -96,50 +48,8 @@ class Resource(ResourceBase):
Returns:
List[Stream] -- A list of Stream objects
"""
metrics.track(metrics.STREAM, self.account, {"name": "get"})
query = gql(
"""
query User($stream_limit: Int!) {
user {
id
bio
name
email
avatar
company
verified
profiles
role
streams(limit: $stream_limit) {
totalCount
cursor
items {
id
name
role
isPublic
createdAt
updatedAt
description
commentCount
favoritesCount
collaborators {
id
name
role
}
}
}
}
}
"""
)
params = {"stream_limit": stream_limit}
return self.make_request(
query=query, params=params, return_type=["user", "streams", "items"]
)
metrics.track(metrics.SDK, self.account, {"name": "Stream List"})
return super().list(stream_limit)
def create(
self,
@@ -152,30 +62,21 @@ class Resource(ResourceBase):
Arguments:
name {str} -- the name of the string
description {str} -- a short description of the stream
is_public {bool} -- whether or not the stream can be viewed by anyone with the id
is_public {bool}
-- whether or not the stream can be viewed by anyone with the id
Returns:
id {str} -- the id of the newly created stream
"""
metrics.track(metrics.STREAM, self.account, {"name": "create"})
query = gql(
"""
mutation StreamCreate($stream: StreamCreateInput!) {
streamCreate(stream: $stream)
}
"""
)
params = {
"stream": {"name": name, "description": description, "isPublic": is_public}
}
return self.make_request(
query=query, params=params, return_type="streamCreate", parse_response=False
)
metrics.track(metrics.SDK, self.account, {"name": "Stream Create"})
return super().create(name, description, is_public)
def update(
self, id: str, name: str = None, description: str = None, is_public: bool = None
self,
id: str,
name: Optional[str] = None,
description: Optional[str] = None,
is_public: Optional[bool] = None,
) -> bool:
"""Update an existing stream
@@ -183,32 +84,14 @@ class Resource(ResourceBase):
id {str} -- the id of the stream to be updated
name {str} -- the name of the string
description {str} -- a short description of the stream
is_public {bool} -- whether or not the stream can be viewed by anyone with the id
is_public {bool}
-- whether or not the stream can be viewed by anyone with the id
Returns:
bool -- whether the stream update was successful
"""
metrics.track(metrics.STREAM, self.account, {"name": "update"})
query = gql(
"""
mutation StreamUpdate($stream: StreamUpdateInput!) {
streamUpdate(stream: $stream)
}
"""
)
params = {
"id": id,
"name": name,
"description": description,
"isPublic": is_public,
}
# remove None values so graphql doesn't cry
params = {"stream": {k: v for k, v in params.items() if v is not None}}
return self.make_request(
query=query, params=params, return_type="streamUpdate", parse_response=False
)
metrics.track(metrics.SDK, self.account, {"name": "Stream Update"})
return super().update(id, name, description, is_public)
def delete(self, id: str) -> bool:
"""Delete a stream given its id
@@ -219,20 +102,8 @@ class Resource(ResourceBase):
Returns:
bool -- whether the deletion was successful
"""
metrics.track(metrics.STREAM, self.account, {"name": "delete"})
query = gql(
"""
mutation StreamDelete($id: String!) {
streamDelete(id: $id)
}
"""
)
params = {"id": id}
return self.make_request(
query=query, params=params, return_type="streamDelete", parse_response=False
)
metrics.track(metrics.SDK, self.account, {"name": "Stream Delete"})
return super().delete(id)
def search(
self,
@@ -252,152 +123,22 @@ class Resource(ResourceBase):
Returns:
List[Stream] -- a list of Streams that match the search query
"""
metrics.track(metrics.STREAM, self.account, {"name": "search"})
query = gql(
"""
query StreamSearch($search_query: String!,$limit: Int!, $branch_limit:Int!, $commit_limit:Int!) {
streams(query: $search_query, limit: $limit) {
items {
id
name
role
description
isPublic
createdAt
updatedAt
collaborators {
id
name
role
avatar
}
branches(limit: $branch_limit) {
totalCount
cursor
items {
id
name
description
commits(limit: $commit_limit) {
totalCount
cursor
items {
id
referencedObject
message
authorName
authorId
createdAt
}
}
}
}
}
}
}
"""
)
params = {
"search_query": search_query,
"limit": limit,
"branch_limit": branch_limit,
"commit_limit": commit_limit,
}
return self.make_request(
query=query, params=params, return_type=["streams", "items"]
)
metrics.track(metrics.SDK, self.account, {"name": "Stream Search"})
return super().search(search_query, limit, branch_limit, commit_limit)
def favorite(self, stream_id: str, favorited: bool = True):
"""Favorite or unfavorite the given stream.
Arguments:
stream_id {str} -- the id of the stream to favorite / unfavorite
favorited {bool} -- whether to favorite (True) or unfavorite (False) the stream
favorited {bool}
-- whether to favorite (True) or unfavorite (False) the stream
Returns:
Stream -- the stream with its `id`, `name`, and `favoritedDate`
"""
metrics.track(metrics.STREAM, self.account, {"name": "favorite"})
query = gql(
"""
mutation StreamFavorite($stream_id: String!, $favorited: Boolean!) {
streamFavorite(streamId: $stream_id, favorited: $favorited) {
id
name
favoritedDate
favoritesCount
}
}
"""
)
params = {
"stream_id": stream_id,
"favorited": favorited,
}
return self.make_request(
query=query, params=params, return_type=["streamFavorite"]
)
@deprecated(
version="2.6.4",
reason=(
"As of Speckle Server v2.6.4, this method is deprecated. "
"Users need to be invited and accept the invite before being added to a stream"
),
)
def grant_permission(self, stream_id: str, user_id: str, role: str):
"""Grant permissions to a user on a given stream
Valid for Speckle Server version < 2.6.4
Arguments:
stream_id {str} -- the id of the stream to grant permissions to
user_id {str} -- the id of the user to grant permissions for
role {str} -- the role to grant the user
Returns:
bool -- True if the operation was successful
"""
metrics.track(metrics.PERMISSION, self.account, {"name": "add", "role": role})
# we're checking for the actual version info, and if the version is 'dev' we treat it
# as an up to date instance
if self.server_version and (
self.server_version == ("dev",) or self.server_version >= (2, 6, 4)
):
raise UnsupportedException(
(
"Server mutation `grant_permission` is no longer supported as of Speckle Server v2.6.4. "
"Please use the new `update_permission` method to change an existing user's permission "
"or use the `invite` method to invite a user to a stream."
)
)
query = gql(
"""
mutation StreamGrantPermission($permission_params: StreamGrantPermissionInput !) {
streamGrantPermission(permissionParams: $permission_params)
}
"""
)
params = {
"permission_params": {
"streamId": stream_id,
"userId": user_id,
"role": role,
}
}
return self.make_request(
query=query,
params=params,
return_type="streamGrantPermission",
parse_response=False,
)
metrics.track(metrics.SDK, self.account, {"name": "Stream Favorite"})
return super().favorite(stream_id, favorited)
def get_all_pending_invites(
self, stream_id: str
@@ -411,56 +152,19 @@ class Resource(ResourceBase):
stream_id {str} -- the stream id from which to get the pending invites
Returns:
List[PendingStreamCollaborator] -- a list of pending invites for the specified stream
List[PendingStreamCollaborator]
-- a list of pending invites for the specified stream
"""
metrics.track(metrics.INVITE, self.account, {"name": "get"})
self._check_invites_supported()
query = gql(
"""
query StreamInvites($streamId: String!) {
stream(id: $streamId){
pendingCollaborators {
id
token
inviteId
streamId
streamName
title
role
invitedBy{
id
name
company
avatar
}
user {
id
name
company
avatar
}
}
}
}
"""
)
params = {"streamId": stream_id}
return self.make_request(
query=query,
params=params,
return_type=["stream", "pendingCollaborators"],
schema=PendingStreamCollaborator,
)
metrics.track(metrics.SDK, self.account, {"name": "Stream Invite Get"})
return super().get_all_pending_invites(stream_id)
def invite(
self,
stream_id: str,
email: str = None,
user_id: str = None,
email: Optional[str] = None,
user_id: Optional[str] = None,
role: str = "stream:contributor", # should default be reviewer?
message: str = None,
message: Optional[str] = None,
):
"""Invite someone to a stream using either their email or user id
@@ -470,50 +174,23 @@ class Resource(ResourceBase):
stream_id {str} -- the id of the stream to invite the user to
email {str} -- the email of the user to invite (use this OR `user_id`)
user_id {str} -- the id of the user to invite (use this OR `email`)
role {str} -- the role to assign to the user (defaults to `stream:contributor`)
message {str} -- a message to send along with this invite to the specified user
role {str}
-- the role to assign to the user (defaults to `stream:contributor`)
message {str}
-- a message to send along with this invite to the specified user
Returns:
bool -- True if the operation was successful
"""
metrics.track(metrics.INVITE, self.account, {"name": "create"})
self._check_invites_supported()
if email is None and user_id is None:
raise SpeckleException(
"You must provide either an email or a user id to use the `stream.invite` method"
)
query = gql(
"""
mutation StreamInviteCreate($input: StreamInviteCreateInput!) {
streamInviteCreate(input: $input)
}
"""
)
params = {
"email": email,
"userId": user_id,
"streamId": stream_id,
"message": message,
"role": role,
}
params = {"input": {k: v for k, v in params.items() if v is not None}}
return self.make_request(
query=query,
params=params,
return_type="streamInviteCreate",
parse_response=False,
)
metrics.track(metrics.SDK, self.account, {"name": "Stream Invite Create"})
return super().invite(stream_id, email, user_id, role, message)
def invite_batch(
self,
stream_id: str,
emails: List[str] = None,
user_ids: List[None] = None,
message: str = None,
emails: Optional[List[str]] = None,
user_ids: Optional[List[None]] = None,
message: Optional[str] = None,
) -> bool:
"""Invite a batch of users to a specified stream.
@@ -521,48 +198,18 @@ class Resource(ResourceBase):
Arguments:
stream_id {str} -- the id of the stream to invite the user to
emails {List[str]} -- the email of the user to invite (use this and/or `user_ids`)
user_id {List[str]} -- the id of the user to invite (use this and/or `emails`)
message {str} -- a message to send along with this invite to the specified user
emails {List[str]}
-- the email of the user to invite (use this and/or `user_ids`)
user_id {List[str]}
-- the id of the user to invite (use this and/or `emails`)
message {str}
-- a message to send along with this invite to the specified user
Returns:
bool -- True if the operation was successful
"""
metrics.track(metrics.INVITE, self.account, {"name": "batch create"})
self._check_invites_supported()
if emails is None and user_ids is None:
raise SpeckleException(
"You must provide either an email or a user id to use the `stream.invite` method"
)
query = gql(
"""
mutation StreamInviteBatchCreate($input: [StreamInviteCreateInput!]!) {
streamInviteBatchCreate(input: $input)
}
"""
)
email_invites = [
{"streamId": stream_id, "message": message, "email": email}
for email in emails
if emails 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
]
params = {"input": [*email_invites, *user_invites]}
return self.make_request(
query=query,
params=params,
return_type="streamInviteBatchCreate",
parse_response=False,
)
metrics.track(metrics.SDK, self.account, {"name": "Stream Invite Batch Create"})
return super().invite_batch(stream_id, emails, user_ids, message)
def invite_cancel(self, stream_id: str, invite_id: str) -> bool:
"""Cancel an existing stream invite
@@ -576,25 +223,8 @@ class Resource(ResourceBase):
Returns:
bool -- true if the operation was successful
"""
metrics.track(metrics.INVITE, self.account, {"name": "cancel"})
self._check_invites_supported()
query = gql(
"""
mutation StreamInviteCancel($streamId: String!, $inviteId: String!) {
streamInviteCancel(streamId: $streamId, inviteId: $inviteId)
}
"""
)
params = {"streamId": stream_id, "inviteId": invite_id}
return self.make_request(
query=query,
params=params,
return_type="streamInviteCancel",
parse_response=False,
)
metrics.track(metrics.SDK, self.account, {"name": "Stream Invite Cancel"})
return super().invite_cancel(stream_id, invite_id)
def invite_use(self, stream_id: str, token: str, accept: bool = True) -> bool:
"""Accept or decline a stream invite
@@ -602,32 +232,16 @@ class Resource(ResourceBase):
Requires Speckle Server version >= 2.6.4
Arguments:
stream_id {str} -- the id of the stream for which the user has a pending invite
stream_id {str}
-- the id of the stream for which the user has a pending invite
token {str} -- the token of the invite to use
accept {bool} -- whether or not to accept the invite (defaults to True)
Returns:
bool -- true if the operation was successful
"""
metrics.track(metrics.INVITE, self.account, {"name": "use"})
self._check_invites_supported()
query = gql(
"""
mutation StreamInviteUse($accept: Boolean!, $streamId: String!, $token: String!) {
streamInviteUse(accept: $accept, streamId: $streamId, token: $token)
}
"""
)
params = {"streamId": stream_id, "token": token, "accept": accept}
return self.make_request(
query=query,
params=params,
return_type="streamInviteUse",
parse_response=False,
)
metrics.track(metrics.SDK, self.account, {"name": "Invite Use"})
return super().invite_use(stream_id, token, accept)
def update_permission(self, stream_id: str, user_id: str, role: str):
"""Updates permissions for a user on a given stream
@@ -642,40 +256,8 @@ class Resource(ResourceBase):
Returns:
bool -- True if the operation was successful
"""
metrics.track(
metrics.PERMISSION, self.account, {"name": "update", "role": role}
)
if self.server_version and (
self.server_version != ("dev",) and self.server_version < (2, 6, 4)
):
raise UnsupportedException(
(
"Server mutation `update_permission` is only supported as of Speckle Server v2.6.4. "
"Please update your Speckle Server to use this method or use the `grant_permission` method instead."
)
)
query = gql(
"""
mutation StreamUpdatePermission($permission_params: StreamUpdatePermissionInput !) {
streamUpdatePermission(permissionParams: $permission_params)
}
"""
)
params = {
"permission_params": {
"streamId": stream_id,
"userId": user_id,
"role": role,
}
}
return self.make_request(
query=query,
params=params,
return_type="streamUpdatePermission",
parse_response=False,
)
metrics.track(metrics.SDK, self.account, {"name": "Stream Permission Update", "role": role})
return super().update_permission(stream_id, user_id, role)
def revoke_permission(self, stream_id: str, user_id: str):
"""Revoke permissions from a user on a given stream
@@ -687,89 +269,36 @@ class Resource(ResourceBase):
Returns:
bool -- True if the operation was successful
"""
metrics.track(metrics.PERMISSION, self.account, {"name": "revoke"})
query = gql(
"""
mutation StreamRevokePermission($permission_params: StreamRevokePermissionInput !) {
streamRevokePermission(permissionParams: $permission_params)
}
"""
)
params = {"permission_params": {"streamId": stream_id, "userId": user_id}}
return self.make_request(
query=query,
params=params,
return_type="streamRevokePermission",
parse_response=False,
)
metrics.track(metrics.SDK, self.account, {"name": "Stream Permission Revoke"})
return super().revoke_permission(stream_id, user_id)
def activity(
self,
stream_id: str,
action_type: str = None,
action_type: Optional[str] = None,
limit: int = 20,
before: datetime = None,
after: datetime = None,
cursor: datetime = None,
before: Optional[datetime] = None,
after: Optional[datetime] = None,
cursor: Optional[datetime] = None,
):
"""
Get the activity from a given stream in an Activity collection. Step into the activity `items` for the list of activity.
Get the activity from a given stream in an Activity collection.
Step into the activity `items` for the list of activity.
Note: all timestamps arguments should be `datetime` of any tz as they will be converted to UTC ISO format strings
Note: all timestamps arguments should be `datetime` of any tz
as they will be converted to UTC ISO format strings
stream_id {str} -- the id of the stream to get activity from
action_type {str} -- filter results to a single action type (eg: `commit_create` or `commit_receive`)
action_type {str}
-- filter results to a single action type
(eg: `commit_create` or `commit_receive`)
limit {int} -- max number of Activity items to return
before {datetime} -- latest cutoff for activity (ie: return all activity _before_ this time)
after {datetime} -- oldest cutoff for activity (ie: return all activity _after_ this time)
before {datetime}
-- latest cutoff for activity (ie: return all activity _before_ this time)
after {datetime}
-- oldest cutoff for activity (ie: return all activity _after_ this time)
cursor {datetime} -- timestamp cursor for pagination
"""
query = gql(
"""
query StreamActivity($stream_id: String!, $action_type: String, $before:DateTime, $after: DateTime, $cursor: DateTime, $limit: Int){
stream(id: $stream_id) {
activity(actionType: $action_type, before: $before, after: $after, cursor: $cursor, limit: $limit) {
totalCount
cursor
items {
actionType
info
userId
streamId
resourceId
resourceType
message
time
}
}
}
}
"""
)
try:
params = {
"stream_id": stream_id,
"limit": limit,
"action_type": action_type,
"before": before.astimezone(timezone.utc).isoformat()
if before
else before,
"after": after.astimezone(timezone.utc).isoformat() if after else after,
"cursor": cursor.astimezone(timezone.utc).isoformat()
if cursor
else cursor,
}
except AttributeError as e:
raise SpeckleException(
"Could not get stream activity - `before`, `after`, and `cursor` must be in `datetime` format if provided",
ValueError,
) from e
return self.make_request(
query=query,
params=params,
return_type=["stream", "activity"],
schema=ActivityCollection,
)
metrics.track(metrics.SDK, self.account, {"name": "Stream Activity"})
return super().activity(stream_id, action_type, limit, before, after, cursor)
+36 -51
View File
@@ -1,13 +1,15 @@
from typing import Callable, Dict, List, Union
from functools import wraps
from typing import Callable, Dict, List, Optional, Union
from gql import gql
from graphql import DocumentNode
from specklepy.api.resource import ResourceBase
from specklepy.api.resources.stream import Stream
from specklepy.logging.exceptions import SpeckleException
NAME = "subscribe"
from specklepy.logging import metrics
from specklepy.core.api.resources.subscriptions import Resource as CoreResource
def check_wsclient(function):
@wraps(function)
@@ -22,7 +24,7 @@ def check_wsclient(function):
return check_wsclient_wrapper
class Resource(ResourceBase):
class Resource(CoreResource):
"""API Access class for subscriptions"""
def __init__(self, account, basepath, client) -> None:
@@ -30,84 +32,67 @@ class Resource(ResourceBase):
account=account,
basepath=basepath,
client=client,
name=NAME,
)
@check_wsclient
async def stream_added(self, callback: Callable = None):
"""Subscribes to new stream added event for your profile. Use this to display an up-to-date list of streams.
async def stream_added(self, callback: Optional[Callable] = None):
"""Subscribes to new stream added event for your profile.
Use this to display an up-to-date list of streams.
Arguments:
callback {Callable[Stream]} -- a function that takes the updated stream as an argument and executes each time a stream is added
callback {Callable[Stream]} -- a function that takes the updated stream
as an argument and executes each time a stream is added
Returns:
Stream -- the update stream
"""
query = gql(
"""
subscription { userStreamAdded }
"""
)
return await self.subscribe(
query=query, callback=callback, return_type="userStreamAdded", schema=Stream
)
metrics.track(metrics.SDK, self.account, {"name": "Subscription Stream Added"})
return super().stream_added(callback)
@check_wsclient
async def stream_updated(self, id: str, callback: Callable = None):
"""Subscribes to stream updated event. Use this in clients/components that pertain only to this stream.
async def stream_updated(self, id: str, callback: Optional[Callable] = None):
"""
Subscribes to stream updated event.
Use this in clients/components that pertain only to this stream.
Arguments:
id {str} -- the stream id of the stream to subscribe to
callback {Callable[Stream]} -- a function that takes the updated stream as an argument and executes each time the stream is updated
callback {Callable[Stream]}
-- a function that takes the updated stream
as an argument and executes each time the stream is updated
Returns:
Stream -- the update stream
"""
query = gql(
"""
subscription Update($id: String!) { streamUpdated(streamId: $id) }
"""
)
params = {"id": id}
return await self.subscribe(
query=query,
params=params,
callback=callback,
return_type="streamUpdated",
schema=Stream,
)
metrics.track(metrics.SDK, self.account, {"name": "Subscription Stream Updated"})
return super().stream_updated(id, callback)
@check_wsclient
async def stream_removed(self, callback: Callable = None):
"""Subscribes to stream removed event for your profile. Use this to display an up-to-date list of streams for your profile. NOTE: If someone revokes your permissions on a stream, this subscription will be triggered with an extra value of revokedBy in the payload.
async def stream_removed(self, callback: Optional[Callable] = None):
"""Subscribes to stream removed event for your profile.
Use this to display an up-to-date list of streams for your profile.
NOTE: If someone revokes your permissions on a stream,
this subscription will be triggered with an extra value of revokedBy
in the payload.
Arguments:
callback {Callable[Dict]} -- a function that takes the returned dict as an argument and executes each time a stream is removed
callback {Callable[Dict]}
-- a function that takes the returned dict as an argument
and executes each time a stream is removed
Returns:
dict -- dict containing 'id' of stream removed and optionally 'revokedBy'
"""
query = gql(
"""
subscription { userStreamRemoved }
"""
)
return await self.subscribe(
query=query,
callback=callback,
return_type="userStreamRemoved",
parse_response=False,
)
metrics.track(metrics.SDK, self.account, {"name": "Subscription Stream Removed"})
return super().stream_removed(callback)
@check_wsclient
async def subscribe(
self,
query: DocumentNode,
params: Dict = None,
callback: Callable = None,
return_type: Union[str, List] = None,
params: Optional[Dict] = None,
callback: Optional[Callable] = None,
return_type: Optional[Union[str, List]] = None,
schema=None,
parse_response: bool = True,
):
+52 -192
View File
@@ -1,20 +1,25 @@
from typing import List, Optional, Union
from datetime import datetime, timezone
from gql import gql
from specklepy.logging import metrics
from specklepy.logging.exceptions import SpeckleException
from specklepy.api.resource import ResourceBase
from specklepy.api.models import ActivityCollection, PendingStreamCollaborator, User
from typing import List, Optional, Union
from deprecated import deprecated
from gql import gql
from specklepy.api.models import ActivityCollection, PendingStreamCollaborator, User
from specklepy.api.resource import ResourceBase
NAME = "user"
from specklepy.logging.exceptions import SpeckleException
from specklepy.logging import metrics
from specklepy.core.api.resources.user import Resource as CoreResource
DEPRECATION_VERSION = "2.9.0"
DEPRECATION_TEXT = "The user resource is deprecated, please use the active_user or other_user resources"
DEPRECATION_TEXT = (
"The user resource is deprecated, please use the active_user or other_user"
" resources"
)
class Resource(ResourceBase):
class Resource(CoreResource):
"""API Access class for users"""
def __init__(self, account, basepath, client, server_version) -> None:
@@ -22,7 +27,6 @@ class Resource(ResourceBase):
account=account,
basepath=basepath,
client=client,
name=NAME,
server_version=server_version,
)
self.schema = User
@@ -40,34 +44,16 @@ class Resource(ResourceBase):
Returns:
User -- the retrieved user
"""
metrics.track(metrics.USER, self.account, {"name": "get"})
query = gql(
"""
query User($id: String) {
user(id: $id) {
id
email
name
bio
company
avatar
verified
profiles
role
}
}
"""
)
params = {"id": id}
return self.make_request(query=query, params=params, return_type="user")
metrics.track(metrics.SDK, self.account, {"name": "User Get_deprecated"})
return super().get(id)
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
def search(
self, search_query: str, limit: int = 25
) -> Union[List[User], SpeckleException]:
"""Searches for user by name or email. The search query must be at least 3 characters long
"""
Searches for user by name or email.
The search query must be at least 3 characters long
Arguments:
search_query {str} -- a string to search for
@@ -75,33 +61,8 @@ class Resource(ResourceBase):
Returns:
List[User] -- a list of User objects that match the search query
"""
if len(search_query) < 3:
return SpeckleException(
message="User search query must be at least 3 characters"
)
metrics.track(metrics.USER, self.account, {"name": "search"})
query = gql(
"""
query UserSearch($search_query: String!, $limit: Int!) {
userSearch(query: $search_query, limit: $limit) {
items {
id
name
bio
company
avatar
verified
}
}
}
"""
)
params = {"search_query": search_query, "limit": limit}
return self.make_request(
query=query, params=params, return_type=["userSearch", "items"]
)
metrics.track(metrics.SDK, self.account, {"name": "User Search_deprecated"})
return super().search(search_query, limit)
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
def update(
@@ -122,26 +83,9 @@ class Resource(ResourceBase):
Returns:
bool -- True if your profile was updated successfully
"""
metrics.track(metrics.USER, self.account, {"name": "update"})
query = gql(
"""
mutation UserUpdate($user: UserUpdateInput!) {
userUpdate(user: $user)
}
"""
)
params = {"name": name, "company": company, "bio": bio, "avatar": avatar}
params = {"user": {k: v for k, v in params.items() if v is not None}}
if not params["user"]:
return SpeckleException(
message="You must provide at least one field to update your user profile"
)
return self.make_request(
query=query, params=params, return_type="userUpdate", parse_response=False
)
#metrics.track(metrics.USER, self.account, {"name": "update"})
metrics.track(metrics.SDK, self.account, {"name": "User Update_deprecated"})
return super().update(name, company, bio, avatar)
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
def activity(
@@ -154,57 +98,27 @@ class Resource(ResourceBase):
cursor: Optional[datetime] = None,
):
"""
Get the activity from a given stream in an Activity collection. Step into the activity `items` for the list of activity.
If no id argument is provided, will return the current authenticated user's activity (as extracted from the authorization header).
Get the activity from a given stream in an Activity collection.
Step into the activity `items` for the list of activity.
If no id argument is provided, will return the current authenticated
user's activity (as extracted from the authorization header).
Note: all timestamps arguments should be `datetime` of any tz as they will be converted to UTC ISO format strings
Note: all timestamps arguments should be `datetime` of any tz as
they will be converted to UTC ISO format strings
user_id {str} -- the id of the user to get the activity from
action_type {str} -- filter results to a single action type (eg: `commit_create` or `commit_receive`)
action_type {str} -- filter results to a single action type
(eg: `commit_create` or `commit_receive`)
limit {int} -- max number of Activity items to return
before {datetime} -- latest cutoff for activity (ie: return all activity _before_ this time)
after {datetime} -- oldest cutoff for activity (ie: return all activity _after_ this time)
before {datetime}
-- latest cutoff for activity (ie: return all activity _before_ this time)
after {datetime}
-- oldest cutoff for activity (ie: return all activity _after_ this time)
cursor {datetime} -- timestamp cursor for pagination
"""
query = gql(
"""
query UserActivity($user_id: String, $action_type: String, $before:DateTime, $after: DateTime, $cursor: DateTime, $limit: Int){
user(id: $user_id) {
activity(actionType: $action_type, before: $before, after: $after, cursor: $cursor, limit: $limit) {
totalCount
cursor
items {
actionType
info
userId
streamId
resourceId
resourceType
message
time
}
}
}
}
"""
)
params = {
"user_id": user_id,
"limit": limit,
"action_type": action_type,
"before": before.astimezone(timezone.utc).isoformat() if before else before,
"after": after.astimezone(timezone.utc).isoformat() if after else after,
"cursor": cursor.astimezone(timezone.utc).isoformat() if cursor else cursor,
}
return self.make_request(
query=query,
params=params,
return_type=["user", "activity"],
schema=ActivityCollection,
)
metrics.track(metrics.SDK, self.account, {"name": "User Activity_deprecated"})
return super().activity(user_id, limit, action_type, before, after, cursor)
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
def get_all_pending_invites(self) -> List[PendingStreamCollaborator]:
@@ -213,38 +127,13 @@ class Resource(ResourceBase):
Requires Speckle Server version >= 2.6.4
Returns:
List[PendingStreamCollaborator] -- a list of pending invites for the current user
List[PendingStreamCollaborator]
-- a list of pending invites for the current user
"""
metrics.track(metrics.INVITE, self.account, {"name": "get"})
self._check_invites_supported()
query = gql(
"""
query StreamInvites {
streamInvites{
id
token
inviteId
streamId
streamName
title
role
invitedBy {
id
name
company
avatar
}
}
}
"""
)
return self.make_request(
query=query,
return_type="streamInvites",
schema=PendingStreamCollaborator,
)
#metrics.track(metrics.INVITE, self.account, {"name": "get"})
metrics.track(metrics.SDK, self.account, {"name": "User GetAllInvites_deprecated"})
return super().get_all_pending_invites()
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
def get_pending_invite(
@@ -260,39 +149,10 @@ class Resource(ResourceBase):
token {str} -- the token of the invite to look for (optional)
Returns:
PendingStreamCollaborator -- the invite for the given stream (or None if it isn't found)
PendingStreamCollaborator
-- the invite for the given stream (or None if it isn't found)
"""
metrics.track(metrics.INVITE, self.account, {"name": "get"})
self._check_invites_supported()
query = gql(
"""
query StreamInvite($streamId: String!, $token: String) {
streamInvite(streamId: $streamId, token: $token) {
id
token
streamId
streamName
title
role
invitedBy {
id
name
company
avatar
}
}
}
"""
)
params = {"streamId": stream_id}
if token:
params["token"] = token
return self.make_request(
query=query,
params=params,
return_type="streamInvite",
schema=PendingStreamCollaborator,
)
#metrics.track(metrics.INVITE, self.account, {"name": "get"})
metrics.track(metrics.SDK, self.account, {"name": "User GetInvite_deprecated"})
return super().get_pending_invite(stream_id, token)
+38 -114
View File
@@ -1,23 +1,29 @@
from urllib.parse import unquote, urlparse
from warnings import warn
from urllib.parse import urlparse, unquote
from specklepy.api.client import SpeckleClient
from specklepy.api.credentials import (
Account,
get_account_from_token,
get_local_accounts,
)
from specklepy.logging import metrics
from specklepy.api.client import SpeckleClient
from specklepy.transports.server.server import ServerTransport
from specklepy.logging.exceptions import SpeckleException, SpeckleWarning
from specklepy.transports.server.server import ServerTransport
from specklepy.logging import metrics
from specklepy.core.api.wrapper import StreamWrapper as CoreStreamWrapper
class StreamWrapper:
class StreamWrapper(CoreStreamWrapper):
"""
The `StreamWrapper` gives you some handy helpers to deal with urls and get authenticated clients and transports.
The `StreamWrapper` gives you some handy helpers to deal with urls and
get authenticated clients and transports.
Construct a `StreamWrapper` with a stream, branch, commit, or object URL. The corresponding ids will be stored
in the wrapper. If you have local accounts on the machine, you can use the `get_account` and `get_client` methods
to get a local account for the server. You can also pass a token into `get_client` if you don't have a corresponding
Construct a `StreamWrapper` with a stream, branch, commit, or object URL.
The corresponding ids will be stored
in the wrapper. If you have local accounts on the machine,
you can use the `get_account` and `get_client` methods
to get a local account for the server. You can also pass a token into `get_client`
if you don't have a corresponding
local account for the server.
```py
@@ -44,127 +50,45 @@ class StreamWrapper:
_client: SpeckleClient = None
_account: Account = None
def __repr__(self):
return f"StreamWrapper( server: {self.host}, stream_id: {self.stream_id}, type: {self.type} )"
def __str__(self) -> str:
return self.__repr__()
@property
def type(self) -> str:
if self.object_id:
return "object"
elif self.commit_id:
return "commit"
elif self.branch_name:
return "branch"
else:
return "stream" if self.stream_id else "invalid"
def __init__(self, url: str) -> None:
self.stream_url = url
parsed = urlparse(url)
self.host = parsed.netloc
self.use_ssl = parsed.scheme == "https"
segments = parsed.path.strip("/").split("/", 3)
metrics.track(metrics.STREAM_WRAPPER, self.get_account())
if not segments or len(segments) < 2:
raise SpeckleException(
f"Cannot parse {url} into a stream wrapper class - invalid URL provided."
)
while segments:
segment = segments.pop(0)
if segments and segment.lower() == "streams":
self.stream_id = segments.pop(0)
elif segments and segment.lower() == "commits":
self.commit_id = segments.pop(0)
elif segments and segment.lower() == "branches":
self.branch_name = unquote(segments.pop(0))
elif segments and segment.lower() == "objects":
self.object_id = segments.pop(0)
elif segment.lower() == "globals":
self.branch_name = "globals"
if segments:
self.commit_id = segments.pop(0)
else:
raise SpeckleException(
f"Cannot parse {url} into a stream wrapper class - invalid URL provided."
)
if not self.stream_id:
raise SpeckleException(
f"Cannot parse {url} into a stream wrapper class - no stream id found."
)
@property
def server_url(self):
return f"{'https' if self.use_ssl else 'http'}://{self.host}"
super().__init__(url = url)
def get_account(self, token: str = None) -> Account:
"""
Gets an account object for this server from the local accounts db (added via Speckle Manager or a json file)
Gets an account object for this server from the local accounts db
(added via Speckle Manager or a json file)
"""
if self._account and self._account.token:
return self._account
self._account = next(
(
a
for a in get_local_accounts()
if self.host == urlparse(a.serverInfo.url).netloc
),
None,
)
if not self._account:
self._account = get_account_from_token(token, self.server_url)
if self._client:
self._client.authenticate_with_account(self._account)
return self._account
metrics.track(metrics.SDK, custom_props={"name": "Stream Wrapper Get Account"})
return super().get_account(token)
def get_client(self, token: str = None) -> SpeckleClient:
"""
Gets an authenticated client for this server. You may provide a token if there aren't any local accounts on this
machine. If no account is found and no token is provided, an unauthenticated client is returned.
Gets an authenticated client for this server.
You may provide a token if there aren't any local accounts on this
machine. If no account is found and no token is provided,
an unauthenticated client is returned.
Arguments:
token {str} -- optional token if no local account is available (defaults to None)
token {str}
-- optional token if no local account is available (defaults to None)
Returns:
SpeckleClient -- authenticated with a corresponding local account or the provided token
SpeckleClient
-- authenticated with a corresponding local account or the provided token
"""
if self._client and token is None:
return self._client
if not self._account or not self._account.token:
self.get_account(token)
if not self._client:
self._client = SpeckleClient(host=self.host, use_ssl=self.use_ssl)
if self._account.token is None and token is None:
warn(f"No local account found for server {self.host}", SpeckleWarning)
return self._client
if self._account.token:
self._client.authenticate_with_account(self._account)
else:
self._client.authenticate_with_token(token)
return self._client
metrics.track(metrics.SDK, custom_props={"name": "Stream Wrapper Get Client"})
return super().get_client(token)
def get_transport(self, token: str = None) -> ServerTransport:
"""
Gets a server transport for this stream using an authenticated client. If there is no local account for this
server and the client was not authenticated with a token, this will throw an exception.
Gets a server transport for this stream using an authenticated client.
If there is no local account for this
server and the client was not authenticated with a token,
this will throw an exception.
Returns:
ServerTransport -- constructed for this stream with a pre-authenticated client
ServerTransport -- constructed for this stream
with a pre-authenticated client
"""
if not self._account or not self._account.token:
self.get_account(token)
return ServerTransport(self.stream_id, account=self._account)
metrics.track(metrics.SDK, custom_props={"name": "Stream Wrapper Get Transport"})
return super().get_transport(token)
+6
View File
@@ -0,0 +1,6 @@
"""
This is the Core SDK module of `specklepy`.
This module should be kept in sync with the functionalities of our other SDKs especially
C# Core https://github.com/specklesystems/speckle-sharp/tree/main/Core/Core
"""
View File
+242
View File
@@ -0,0 +1,242 @@
import re
from typing import Dict
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
from specklepy.core.api import resources
from specklepy.core.api.credentials import Account, get_account_from_token
from specklepy.core.api.resources import (
user,
active_user,
branch,
commit,
object,
other_user,
server,
stream,
subscriptions,
)
from specklepy.logging import metrics
from specklepy.logging.exceptions import SpeckleException, SpeckleWarning
class SpeckleClient:
"""
The `SpeckleClient` is your entry point for interacting with
your Speckle Server's GraphQL API.
You'll need to have access to a server to use it,
or you can use our public server `speckle.xyz`.
To authenticate the client, you'll need to have downloaded
the [Speckle Manager](https://speckle.guide/#speckle-manager)
and added your account.
```py
from specklepy.api.client import SpeckleClient
from specklepy.api.credentials import get_default_account
# initialise the client
client = SpeckleClient(host="speckle.xyz") # or whatever your host is
# client = SpeckleClient(host="localhost:3000", use_ssl=False) or use local server
# authenticate the client with an account (account has been added in Speckle Manager)
account = get_default_account()
client.authenticate_with_account(account)
# create a new stream. this returns the stream id
new_stream_id = client.stream.create(name="a shiny new stream")
# use that stream id to get the stream from the server
new_stream = client.stream.get(id=new_stream_id)
```
"""
DEFAULT_HOST = "speckle.xyz"
USE_SSL = True
def __init__(self, host: str = DEFAULT_HOST, use_ssl: bool = USE_SSL) -> None:
ws_protocol = "ws"
http_protocol = "http"
if use_ssl:
ws_protocol = "wss"
http_protocol = "https"
# sanitise host input by removing protocol and trailing slash
host = re.sub(r"((^\w+:|^)\/\/)|(\/$)", "", host)
self.url = f"{http_protocol}://{host}"
self.graphql = f"{self.url}/graphql"
self.ws_url = f"{ws_protocol}://{host}/graphql"
self.account = Account()
self.httpclient = Client(
transport=RequestsHTTPTransport(url=self.graphql, verify=True, retries=3)
)
self.wsclient = None
self._init_resources()
# ? Check compatibility with the server - i think we can skip this at this point? save a request
# try:
# server_info = self.server.get()
# if isinstance(server_info, Exception):
# raise server_info
# if not isinstance(server_info, ServerInfo):
# raise Exception("Couldn't get ServerInfo")
# except Exception as ex:
# raise SpeckleException(
# f"{self.url} is not a compatible Speckle Server", ex
# ) from ex
def __repr__(self):
return (
f"SpeckleClient( server: {self.url}, authenticated:"
f" {self.account.token is not None} )"
)
@deprecated(
version="2.6.0",
reason=(
"Renamed: please use `authenticate_with_account` or"
" `authenticate_with_token` instead."
),
)
def authenticate(self, token: str) -> None:
"""Authenticate the client using a personal access token
The token is saved in the client object and a synchronous GraphQL
entrypoint is created
Arguments:
token {str} -- an api token
"""
self.authenticate_with_token(token)
self._set_up_client()
def authenticate_with_token(self, token: str) -> None:
"""
Authenticate the client using a personal access token.
The token is saved in the client object and a synchronous GraphQL
entrypoint is created
Arguments:
token {str} -- an api token
"""
self.account = get_account_from_token(token, self.url)
self._set_up_client()
def authenticate_with_account(self, account: Account) -> None:
"""Authenticate the client using an Account object
The account is saved in the client object and a synchronous GraphQL
entrypoint is created
Arguments:
account {Account} -- the account object which can be found with
`get_default_account` or `get_local_accounts`
"""
self.account = account
self._set_up_client()
def _set_up_client(self) -> None:
headers = {
"Authorization": f"Bearer {self.account.token}",
"Content-Type": "application/json",
"apollographql-client-name": metrics.HOST_APP,
"apollographql-client-version": metrics.HOST_APP_VERSION,
}
httptransport = RequestsHTTPTransport(
url=self.graphql, headers=headers, verify=True, retries=3
)
wstransport = WebsocketsTransport(
url=self.ws_url,
init_payload={"Authorization": f"Bearer {self.account.token}"},
)
self.httpclient = Client(transport=httptransport)
self.wsclient = Client(transport=wstransport)
self._init_resources()
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)
def _init_resources(self) -> None:
self.server = server.Resource(
account=self.account, basepath=self.url, client=self.httpclient
)
server_version = None
try:
server_version = self.server.version()
except Exception:
pass
self.user = user.Resource(
account=self.account,
basepath=self.url,
client=self.httpclient,
server_version=server_version,
)
self.other_user = other_user.Resource(
account=self.account,
basepath=self.url,
client=self.httpclient,
server_version=server_version,
)
self.active_user = active_user.Resource(
account=self.account,
basepath=self.url,
client=self.httpclient,
server_version=server_version,
)
self.stream = stream.Resource(
account=self.account,
basepath=self.url,
client=self.httpclient,
server_version=server_version,
)
self.commit = commit.Resource(
account=self.account, basepath=self.url, client=self.httpclient
)
self.branch = branch.Resource(
account=self.account, basepath=self.url, client=self.httpclient
)
self.object = object.Resource(
account=self.account, basepath=self.url, client=self.httpclient
)
self.subscribe = subscriptions.Resource(
account=self.account,
basepath=self.ws_url,
client=self.wsclient,
)
def __getattr__(self, name):
try:
attr = getattr(resources, name)
return attr.Resource(
account=self.account, basepath=self.url, client=self.httpclient
)
except AttributeError:
raise SpeckleException(
f"Method {name} is not supported by the SpeckleClient class"
)
+154
View File
@@ -0,0 +1,154 @@
import os
from pathlib import Path
from typing import List, Optional
from pydantic import BaseModel, Field # pylint: disable=no-name-in-module
from specklepy.core.api.models import ServerInfo
from specklepy.core.helpers import speckle_path_provider
from specklepy.logging import metrics
from specklepy.logging.exceptions import SpeckleException
from specklepy.transports.sqlite import SQLiteTransport
class UserInfo(BaseModel):
name: Optional[str] = None
email: Optional[str] = None
company: Optional[str] = None
id: Optional[str] = None
class Account(BaseModel):
isDefault: bool = False
token: Optional[str] = None
refreshToken: Optional[str] = None
serverInfo: ServerInfo = Field(default_factory=ServerInfo)
userInfo: UserInfo = Field(default_factory=UserInfo)
id: Optional[str] = None
def __repr__(self) -> str:
return (
f"Account(email: {self.userInfo.email}, server: {self.serverInfo.url},"
f" isDefault: {self.isDefault})"
)
def __str__(self) -> str:
return self.__repr__()
@classmethod
def from_token(cls, token: str, server_url: str = None):
acct = cls(token=token)
acct.serverInfo.url = server_url
return acct
def get_local_accounts(base_path: Optional[str] = None) -> List[Account]:
"""Gets all the accounts present in this environment
Arguments:
base_path {str} -- custom base path if you are not using the system default
Returns:
List[Account] -- list of all local accounts or an empty list if
no accounts were found
"""
accounts: List[Account] = []
try:
account_storage = SQLiteTransport(scope="Accounts", base_path=base_path)
res = account_storage.get_all_objects()
account_storage.close()
if res:
accounts.extend(Account.model_validate_json(r[1]) for r in res)
except SpeckleException:
# cannot open SQLiteTransport, probably because of the lack
# of disk write permissions
pass
json_acct_files = []
json_path = str(speckle_path_provider.accounts_folder_path())
try:
os.makedirs(json_path, exist_ok=True)
json_acct_files.extend(
file for file in os.listdir(json_path) if file.endswith(".json")
)
except Exception:
# cannot find or get the json account paths
pass
if json_acct_files:
try:
accounts.extend(
Account.model_validate_json(Path(json_path, json_file).read_text())
# Account.parse_file(os.path.join(json_path, json_file))
for json_file in json_acct_files
)
except Exception as ex:
raise SpeckleException(
"Invalid json accounts could not be read. Please fix or remove them.",
ex,
) from ex
return accounts
def get_default_account(base_path: Optional[str] = None) -> Optional[Account]:
"""
Gets this environment's default account if any. If there is no default,
the first found will be returned and set as default.
Arguments:
base_path {str} -- custom base path if you are not using the system default
Returns:
Account -- the default account or None if no local accounts were found
"""
accounts = get_local_accounts(base_path=base_path)
if not accounts:
return None
default = next((acc for acc in accounts if acc.isDefault), None)
if not default:
default = accounts[0]
default.isDefault = True
#metrics.initialise_tracker(default)
return default
def get_account_from_token(token: str, server_url: str = None) -> Account:
"""Gets the local account for the token if it exists
Arguments:
token {str} -- the api token
Returns:
Account -- the local account with this token or a shell account containing
just the token and url if no local account is found
"""
accounts = get_local_accounts()
if not accounts:
return Account.from_token(token, server_url)
acct = next((acc for acc in accounts if acc.token == token), None)
if acct:
return acct
if server_url:
url = server_url.lower()
acct = next(
(acc for acc in accounts if url in acc.serverInfo.url.lower()), None
)
if acct:
return acct
return Account.from_token(token, server_url)
class StreamWrapper:
def __init__(self, url: str = None) -> None:
raise SpeckleException(
message=(
"The StreamWrapper has moved as of v2.6.0! Please import from"
" specklepy.api.wrapper"
),
exception=DeprecationWarning(),
)
+116
View File
@@ -0,0 +1,116 @@
from dataclasses import dataclass
from enum import Enum
from unicodedata import name
class HostAppVersion(Enum):
v = "v"
v6 = "v6"
v7 = "v7"
v2019 = "v2019"
v2020 = "v2020"
v2021 = "v2021"
v2022 = "v2022"
v2023 = "v2023"
v2024 = "v2024"
v2025 = "v2025"
vSandbox = "vSandbox"
vRevit = "vRevit"
vRevit2021 = "vRevit2021"
vRevit2022 = "vRevit2022"
vRevit2023 = "vRevit2023"
vRevit2024 = "vRevit2024"
vRevit2025 = "vRevit2025"
v25 = "v25"
v26 = "v26"
def __repr__(self) -> str:
return self.value
def __str__(self) -> str:
return self.value
@dataclass
class HostApplication:
name: str
slug: str
def get_version(self, version: HostAppVersion) -> str:
return f"{name.replace(' ', '')}{str(version).strip('v')}"
RHINO = HostApplication("Rhino", "rhino")
GRASSHOPPER = HostApplication("Grasshopper", "grasshopper")
REVIT = HostApplication("Revit", "revit")
DYNAMO = HostApplication("Dynamo", "dynamo")
UNITY = HostApplication("Unity", "unity")
GSA = HostApplication("GSA", "gsa")
CIVIL = HostApplication("Civil 3D", "civil3d")
AUTOCAD = HostApplication("AutoCAD", "autocad")
MICROSTATION = HostApplication("MicroStation", "microstation")
OPENROADS = HostApplication("OpenRoads", "openroads")
OPENRAIL = HostApplication("OpenRail", "openrail")
OPENBUILDINGS = HostApplication("OpenBuildings", "openbuildings")
ETABS = HostApplication("ETABS", "etabs")
SAP2000 = HostApplication("SAP2000", "sap2000")
CSIBRIDGE = HostApplication("CSIBridge", "csibridge")
SAFE = HostApplication("SAFE", "safe")
TEKLASTRUCTURES = HostApplication("Tekla Structures", "teklastructures")
DXF = HostApplication("DXF Converter", "dxf")
EXCEL = HostApplication("Excel", "excel")
UNREAL = HostApplication("Unreal", "unreal")
POWERBI = HostApplication("Power BI", "powerbi")
BLENDER = HostApplication("Blender", "blender")
QGIS = HostApplication("QGIS", "qgis")
ARCGIS = HostApplication("ArcGIS", "arcgis")
SKETCHUP = HostApplication("SketchUp", "sketchup")
ARCHICAD = HostApplication("Archicad", "archicad")
TOPSOLID = HostApplication("TopSolid", "topsolid")
PYTHON = HostApplication("Python", "python")
NET = HostApplication(".NET", "net")
OTHER = HostApplication("Other", "other")
_app_name_host_app_mapping = {
"dynamo": DYNAMO,
"revit": REVIT,
"autocad": AUTOCAD,
"civil": CIVIL,
"rhino": RHINO,
"grasshopper": GRASSHOPPER,
"unity": UNITY,
"gsa": GSA,
"microstation": MICROSTATION,
"openroads": OPENROADS,
"openrail": OPENRAIL,
"openbuildings": OPENBUILDINGS,
"etabs": ETABS,
"sap": SAP2000,
"csibridge": CSIBRIDGE,
"safe": SAFE,
"teklastructures": TEKLASTRUCTURES,
"dxf": DXF,
"excel": EXCEL,
"unreal": UNREAL,
"powerbi": POWERBI,
"blender": BLENDER,
"qgis": QGIS,
"arcgis": ARCGIS,
"sketchup": SKETCHUP,
"archicad": ARCHICAD,
"topsolid": TOPSOLID,
"python": PYTHON,
"net": NET,
}
def get_host_app_from_string(app_name: str) -> HostApplication:
app_name = app_name.lower().replace(" ", "")
for partial_app_name, host_app in _app_name_host_app_mapping.items():
if partial_app_name in app_name:
return host_app
return HostApplication(app_name, app_name)
if __name__ == "__main__":
print(HostAppVersion.v)
+198
View File
@@ -0,0 +1,198 @@
from datetime import datetime
from typing import List, Optional
from pydantic import BaseModel, Field
class Collaborator(BaseModel):
id: Optional[str] = None
name: Optional[str] = None
role: Optional[str] = None
avatar: Optional[str] = None
class Commit(BaseModel):
id: Optional[str] = None
message: Optional[str] = None
authorName: Optional[str] = None
authorId: Optional[str] = None
authorAvatar: Optional[str] = None
branchName: Optional[str] = None
createdAt: Optional[datetime] = None
sourceApplication: Optional[str] = None
referencedObject: Optional[str] = None
totalChildrenCount: Optional[int] = None
parents: Optional[List[str]] = None
def __repr__(self) -> str:
return (
f"Commit( id: {self.id}, message: {self.message}, referencedObject:"
f" {self.referencedObject}, authorName: {self.authorName}, branchName:"
f" {self.branchName}, createdAt: {self.createdAt} )"
)
def __str__(self) -> str:
return self.__repr__()
class Commits(BaseModel):
totalCount: Optional[int] = None
cursor: Optional[datetime] = None
items: List[Commit] = []
class Object(BaseModel):
id: Optional[str] = None
speckleType: Optional[str] = None
applicationId: Optional[str] = None
totalChildrenCount: Optional[int] = None
createdAt: Optional[datetime] = None
class Branch(BaseModel):
id: Optional[str] = None
name: Optional[str] = None
description: Optional[str] = None
commits: Optional[Commits] = None
class Branches(BaseModel):
totalCount: Optional[int] = None
cursor: Optional[datetime] = None
items: List[Branch] = []
class Stream(BaseModel):
id: Optional[str] = None
name: Optional[str] = None
role: Optional[str] = None
isPublic: Optional[bool] = None
description: Optional[str] = None
createdAt: Optional[datetime] = None
updatedAt: Optional[datetime] = None
collaborators: List[Collaborator] = Field(default_factory=list)
branches: Optional[Branches] = None
commit: Optional[Commit] = None
object: Optional[Object] = None
commentCount: Optional[int] = None
favoritedDate: Optional[datetime] = None
favoritesCount: Optional[int] = None
def __repr__(self):
return (
f"Stream( id: {self.id}, name: {self.name}, description:"
f" {self.description}, isPublic: {self.isPublic})"
)
def __str__(self) -> str:
return self.__repr__()
class Streams(BaseModel):
totalCount: Optional[int] = None
cursor: Optional[datetime] = None
items: List[Stream] = []
class User(BaseModel):
id: Optional[str] = None
email: Optional[str] = None
name: Optional[str] = None
bio: Optional[str] = None
company: Optional[str] = None
avatar: Optional[str] = None
verified: Optional[bool] = None
role: Optional[str] = None
streams: Optional[Streams] = None
def __repr__(self):
return (
f"User( id: {self.id}, name: {self.name}, email: {self.email}, company:"
f" {self.company} )"
)
def __str__(self) -> str:
return self.__repr__()
class LimitedUser(BaseModel):
"""Limited user type, for showing public info about a user to another user."""
id: str
name: Optional[str] = None
bio: Optional[str] = None
company: Optional[str] = None
avatar: Optional[str] = None
verified: Optional[bool] = None
role: Optional[str] = None
class PendingStreamCollaborator(BaseModel):
id: Optional[str] = None
inviteId: Optional[str] = None
streamId: Optional[str] = None
streamName: Optional[str] = None
title: Optional[str] = None
role: Optional[str] = None
invitedBy: Optional[User] = None
user: Optional[User] = None
token: Optional[str] = None
def __repr__(self):
return (
f"PendingStreamCollaborator( inviteId: {self.inviteId}, streamId:"
f" {self.streamId}, role: {self.role}, title: {self.title}, invitedBy:"
f" {self.user.name if self.user else None})"
)
def __str__(self) -> str:
return self.__repr__()
class Activity(BaseModel):
actionType: Optional[str] = None
info: Optional[dict] = None
userId: Optional[str] = None
streamId: Optional[str] = None
resourceId: Optional[str] = None
resourceType: Optional[str] = None
message: Optional[str] = None
time: Optional[datetime] = None
def __repr__(self) -> str:
return (
f"Activity( streamId: {self.streamId}, actionType: {self.actionType},"
f" message: {self.message}, userId: {self.userId} )"
)
def __str__(self) -> str:
return self.__repr__()
class ActivityCollection(BaseModel):
totalCount: Optional[int] = None
items: Optional[List[Activity]] = None
cursor: Optional[datetime] = None
def __repr__(self) -> str:
return (
f"ActivityCollection( totalCount: {self.totalCount}, items:"
f" {len(self.items) if self.items else 0}, cursor:"
f" {self.cursor.isoformat() if self.cursor else None} )"
)
def __str__(self) -> str:
return self.__repr__()
class ServerInfo(BaseModel):
name: Optional[str] = None
company: Optional[str] = None
url: Optional[str] = None
description: Optional[str] = None
adminContact: Optional[str] = None
canonicalUrl: Optional[str] = None
roles: Optional[List[dict]] = None
scopes: Optional[List[dict]] = None
authStrategies: Optional[List[dict]] = None
version: Optional[str] = None
+139
View File
@@ -0,0 +1,139 @@
from typing import List, Optional
#from specklepy.logging import metrics
from specklepy.logging.exceptions import SpeckleException
from specklepy.objects.base import Base
from specklepy.serialization.base_object_serializer import BaseObjectSerializer
from specklepy.transports.abstract_transport import AbstractTransport
from specklepy.transports.sqlite import SQLiteTransport
def send(
base: Base,
transports: Optional[List[AbstractTransport]] = None,
use_default_cache: bool = True,
):
"""Sends an object via the provided transports. Defaults to the local cache.
Arguments:
obj {Base} -- the object you want to send
transports {list} -- where you want to send them
use_default_cache {bool} -- toggle for the default cache.
If set to false, it will only send to the provided transports
Returns:
str -- the object id of the sent object
"""
if not transports and not use_default_cache:
raise SpeckleException(
message=(
"You need to provide at least one transport: cannot send with an empty"
" transport list and no default cache"
)
)
if isinstance(transports, AbstractTransport):
transports = [transports]
if transports is None:
transports = []
if use_default_cache:
transports.insert(0, SQLiteTransport())
serializer = BaseObjectSerializer(write_transports=transports)
obj_hash, _ = serializer.write_json(base=base)
return obj_hash
def receive(
obj_id: str,
remote_transport: Optional[AbstractTransport] = None,
local_transport: Optional[AbstractTransport] = None,
) -> Base:
"""Receives an object from a transport.
Arguments:
obj_id {str} -- the id of the object to receive
remote_transport {Transport} -- the transport to receive from
local_transport {Transport} -- the local cache to check for existing objects
(defaults to `SQLiteTransport`)
Returns:
Base -- the base object
"""
if not local_transport:
local_transport = SQLiteTransport()
serializer = BaseObjectSerializer(read_transport=local_transport)
# try local transport first. if the parent is there, we assume all the children are there and continue with deserialization using the local transport
obj_string = local_transport.get_object(obj_id)
if obj_string:
return serializer.read_json(obj_string=obj_string)
if not remote_transport:
raise SpeckleException(
message=(
"Could not find the specified object using the local transport, and you"
" didn't provide a fallback remote from which to pull it."
)
)
obj_string = remote_transport.copy_object_and_children(
id=obj_id, target_transport=local_transport
)
return serializer.read_json(obj_string=obj_string)
def serialize(base: Base, write_transports: List[AbstractTransport] = []) -> str:
"""
Serialize a base object. If no write transports are provided,
the object will be serialized
without detaching or chunking any of the attributes.
Arguments:
base {Base} -- the object to serialize
write_transports {List[AbstractTransport]}
-- optional: the transports to write to
Returns:
str -- the serialized object
"""
serializer = BaseObjectSerializer(write_transports=write_transports)
return serializer.write_json(base)[1]
def deserialize(
obj_string: str, read_transport: Optional[AbstractTransport] = None
) -> Base:
"""
Deserialize a string object into a Base object.
If the object contains referenced child objects that are not stored in the local db,
a read transport needs to be provided in order to recompose
the base with the children objects.
Arguments:
obj_string {str} -- the string object to deserialize
read_transport {AbstractTransport}
-- the transport to fetch children objects from
(defaults to SQLiteTransport)
Returns:
Base -- the deserialized object
"""
if not read_transport:
read_transport = SQLiteTransport()
serializer = BaseObjectSerializer(read_transport=read_transport)
return serializer.read_json(obj_string=obj_string)
__all__ = ["receive", "send", "serialize", "deserialize"]
+131
View File
@@ -0,0 +1,131 @@
from threading import Lock
from typing import Any, Dict, List, Optional, Tuple, Type, Union
from gql.client import Client
from gql.transport.exceptions import TransportQueryError
from graphql import DocumentNode
from specklepy.core.api.credentials import Account
from specklepy.logging.exceptions import (
GraphQLException,
SpeckleException,
UnsupportedException,
)
from specklepy.serialization.base_object_serializer import BaseObjectSerializer
from specklepy.transports.sqlite import SQLiteTransport
class ResourceBase(object):
def __init__(
self,
account: Account,
basepath: str,
client: Client,
name: str,
server_version: Optional[Tuple[Any, ...]] = None,
) -> None:
self.account = account
self.basepath = basepath
self.client = client
self.name = name
self.server_version = server_version
self.schema: Optional[Type] = None
self.__lock = Lock()
def _step_into_response(self, response: dict, return_type: Union[str, List, None]):
"""Step into the dict to get the relevant data"""
if return_type is None:
return response
if isinstance(return_type, str):
return response[return_type]
if isinstance(return_type, List):
for key in return_type:
response = response[key]
return response
def _parse_response(self, response: Union[dict, list, None], schema=None):
"""Try to create a class instance from the response"""
if response is None:
return None
if isinstance(response, list):
return [self._parse_response(response=r, schema=schema) for r in response]
if schema:
return schema.model_validate(response)
elif self.schema:
try:
return self.schema.model_validate(response)
except Exception:
s = BaseObjectSerializer(read_transport=SQLiteTransport())
return s.recompose_base(response)
else:
return response
def make_request(
self,
query: DocumentNode,
params: Optional[Dict] = None,
return_type: Union[str, List, None] = None,
schema=None,
parse_response: bool = True,
) -> Any:
"""Executes the GraphQL query"""
try:
with self.__lock:
response = self.client.execute(query, variable_values=params)
except Exception as ex:
if isinstance(ex, TransportQueryError):
return GraphQLException(
message=(
f"Failed to execute the GraphQL {self.name} request. Errors:"
f" {ex.errors}"
),
errors=ex.errors,
data=ex.data,
)
else:
return SpeckleException(
message=(
f"Failed to execute the GraphQL {self.name} request. Inner"
f" exception: {ex}"
),
exception=ex,
)
response = self._step_into_response(response=response, return_type=return_type)
if parse_response:
return self._parse_response(response=response, schema=schema)
else:
return response
def _check_server_version_at_least(
self, target_version: Tuple[Any, ...], unsupported_message: Optional[str] = None
):
"""Use this check to guard against making unsupported requests on older servers.
Arguments:
target_version {tuple}
the minimum server version in the format (major, minor, patch, (tag, build))
eg (2, 6, 3) for a stable build and (2, 6, 4, 'alpha', 4711) for alpha
"""
if not unsupported_message:
unsupported_message = (
"The client method used is not supported on Speckle Server versions"
f" prior to v{'.'.join(target_version)}"
)
# if version is dev, it should be supported... (or not)
if self.server_version == ("dev",):
return
if self.server_version and self.server_version < target_version:
raise UnsupportedException(unsupported_message)
def _check_invites_supported(self):
"""Invites are only supported for Speckle Server >= 2.6.4.
Use this check to guard against making unsupported requests on older servers.
"""
self._check_server_version_at_least(
(2, 6, 4),
"Stream invites are only supported as of Speckle Server v2.6.4. Please"
" update your Speckle Server to use this method or use the"
" `grant_permission` flow instead.",
)
@@ -0,0 +1,264 @@
from datetime import datetime, timezone
from typing import List, Optional
from gql import gql
from specklepy.core.api.models import ActivityCollection, PendingStreamCollaborator, User
from specklepy.core.api.resource import ResourceBase
from specklepy.logging.exceptions import SpeckleException
NAME = "active_user"
class Resource(ResourceBase):
"""API Access class for users"""
def __init__(self, account, basepath, client, server_version) -> None:
super().__init__(
account=account,
basepath=basepath,
client=client,
name=NAME,
server_version=server_version,
)
self.schema = User
def get(self) -> User:
"""Gets the profile of a user. If no id argument is provided,
will return the current authenticated user's profile
(as extracted from the authorization header).
Arguments:
id {str} -- the user id
Returns:
User -- the retrieved user
"""
query = gql(
"""
query User {
activeUser {
id
email
name
bio
company
avatar
verified
profiles
role
}
}
"""
)
params = {}
return self.make_request(query=query, params=params, return_type="activeUser")
def update(
self,
name: Optional[str] = None,
company: Optional[str] = None,
bio: Optional[str] = None,
avatar: Optional[str] = None,
):
"""Updates your user profile. All arguments are optional.
Arguments:
name {str} -- your name
company {str} -- the company you may or may not work for
bio {str} -- tell us about yourself
avatar {str} -- a nice photo of yourself
Returns @deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT):
bool -- True if your profile was updated successfully
"""
query = gql(
"""
mutation UserUpdate($user: UserUpdateInput!) {
userUpdate(user: $user)
}
"""
)
params = {"name": name, "company": company, "bio": bio, "avatar": avatar}
params = {"user": {k: v for k, v in params.items() if v is not None}}
if not params["user"]:
return SpeckleException(
message=(
"You must provide at least one field to update your user profile"
)
)
return self.make_request(
query=query, params=params, return_type="userUpdate", parse_response=False
)
def activity(
self,
limit: int = 20,
action_type: Optional[str] = None,
before: Optional[datetime] = None,
after: Optional[datetime] = None,
cursor: Optional[datetime] = None,
):
"""
Get the activity from a given stream in an Activity collection.
Step into the activity `items` for the list of activity.
If no id argument is provided, will return the current authenticated user's
activity (as extracted from the authorization header).
Note: all timestamps arguments should be `datetime` of any tz as they will be
converted to UTC ISO format strings
user_id {str} -- the id of the user to get the activity from
action_type {str} -- filter results to a single action type
(eg: `commit_create` or `commit_receive`)
limit {int} -- max number of Activity items to return
before {datetime} -- latest cutoff for activity
(ie: return all activity _before_ this time)
after {datetime} -- oldest cutoff for activity
(ie: return all activity _after_ this time)
cursor {datetime} -- timestamp cursor for pagination
"""
query = gql(
"""
query UserActivity(
$action_type: String,
$before:DateTime,
$after: DateTime,
$cursor: DateTime,
$limit: Int
){
activeUser {
activity(
actionType: $action_type,
before: $before,
after: $after,
cursor: $cursor,
limit: $limit
) {
totalCount
cursor
items {
actionType
info
userId
streamId
resourceId
resourceType
message
time
}
}
}
}
"""
)
params = {
"limit": limit,
"action_type": action_type,
"before": before.astimezone(timezone.utc).isoformat() if before else before,
"after": after.astimezone(timezone.utc).isoformat() if after else after,
"cursor": cursor.astimezone(timezone.utc).isoformat() if cursor else cursor,
}
return self.make_request(
query=query,
params=params,
return_type=["activeUser", "activity"],
schema=ActivityCollection,
)
def get_all_pending_invites(self) -> List[PendingStreamCollaborator]:
"""Get all of the active user's pending stream invites
Requires Speckle Server version >= 2.6.4
Returns:
List[PendingStreamCollaborator]
-- a list of pending invites for the current user
"""
self._check_invites_supported()
query = gql(
"""
query StreamInvites {
streamInvites{
id
token
inviteId
streamId
streamName
title
role
invitedBy {
id
name
company
avatar
}
}
}
"""
)
return self.make_request(
query=query,
return_type="streamInvites",
schema=PendingStreamCollaborator,
)
def get_pending_invite(
self, stream_id: str, token: Optional[str] = None
) -> Optional[PendingStreamCollaborator]:
"""Get a particular pending invite for the active user on a given stream.
If no invite_id is provided, any valid invite will be returned.
Requires Speckle Server version >= 2.6.4
Arguments:
stream_id {str} -- the id of the stream to look for invites on
token {str} -- the token of the invite to look for (optional)
Returns:
PendingStreamCollaborator
-- the invite for the given stream (or None if it isn't found)
"""
self._check_invites_supported()
query = gql(
"""
query StreamInvite($streamId: String!, $token: String) {
streamInvite(streamId: $streamId, token: $token) {
id
token
streamId
streamName
title
role
invitedBy {
id
name
company
avatar
}
}
}
"""
)
params = {"streamId": stream_id}
if token:
params["token"] = token
return self.make_request(
query=query,
params=params,
return_type="streamInvite",
schema=PendingStreamCollaborator,
)
+219
View File
@@ -0,0 +1,219 @@
from typing import Optional
from gql import gql
from specklepy.core.api.models import Branch
from specklepy.core.api.resource import ResourceBase
NAME = "branch"
class Resource(ResourceBase):
"""API Access class for branches"""
def __init__(self, account, basepath, client) -> None:
super().__init__(
account=account,
basepath=basepath,
client=client,
name=NAME,
)
self.schema = Branch
def create(
self, stream_id: str, name: str, description: str = "No description provided"
) -> str:
"""Create a new branch on this stream
Arguments:
name {str} -- the name of the new branch
description {str} -- a short description of the branch
Returns:
id {str} -- the newly created branch's id
"""
query = gql(
"""
mutation BranchCreate($branch: BranchCreateInput!) {
branchCreate(branch: $branch)
}
"""
)
params = {
"branch": {
"streamId": stream_id,
"name": name,
"description": description,
}
}
return self.make_request(
query=query, params=params, return_type="branchCreate", parse_response=False
)
def get(self, stream_id: str, name: str, commits_limit: int = 10):
"""Get a branch by name from a stream
Arguments:
stream_id {str} -- the id of the stream to get the branch from
name {str} -- the name of the branch to get
commits_limit {int} -- maximum number of commits to get
Returns:
Branch -- the fetched branch with its latest commits
"""
query = gql(
"""
query BranchGet($stream_id: String!, $name: String!, $commits_limit: Int!) {
stream(id: $stream_id) {
branch(name: $name) {
id,
name,
description,
commits (limit: $commits_limit) {
totalCount,
cursor,
items {
id,
referencedObject,
sourceApplication,
totalChildrenCount,
message,
authorName,
authorId,
branchName,
parents,
createdAt
}
}
}
}
}
"""
)
params = {"stream_id": stream_id, "name": name, "commits_limit": commits_limit}
return self.make_request(
query=query, params=params, return_type=["stream", "branch"]
)
def list(self, stream_id: str, branches_limit: int = 10, commits_limit: int = 10):
"""Get a list of branches from a given stream
Arguments:
stream_id {str} -- the id of the stream to get the branches from
branches_limit {int} -- maximum number of branches to get
commits_limit {int} -- maximum number of commits to get
Returns:
List[Branch] -- the branches on the stream
"""
query = gql(
"""
query BranchesGet(
$stream_id: String!,
$branches_limit: Int!,
$commits_limit: Int!
) {
stream(id: $stream_id) {
branches(limit: $branches_limit) {
items {
id
name
description
commits(limit: $commits_limit) {
totalCount
items{
id
message
referencedObject
sourceApplication
parents
authorId
authorName
branchName
createdAt
}
}
}
}
}
}
"""
)
params = {
"stream_id": stream_id,
"branches_limit": branches_limit,
"commits_limit": commits_limit,
}
return self.make_request(
query=query, params=params, return_type=["stream", "branches", "items"]
)
def update(
self,
stream_id: str,
branch_id: str,
name: Optional[str] = None,
description: Optional[str] = None,
):
"""Update a branch
Arguments:
stream_id {str} -- the id of the stream containing the branch to update
branch_id {str} -- the id of the branch to update
name {str} -- optional: the updated branch name
description {str} -- optional: the updated branch description
Returns:
bool -- True if update is successful
"""
query = gql(
"""
mutation BranchUpdate($branch: BranchUpdateInput!) {
branchUpdate(branch: $branch)
}
"""
)
params = {
"branch": {
"streamId": stream_id,
"id": branch_id,
}
}
if name:
params["branch"]["name"] = name
if description:
params["branch"]["description"] = description
return self.make_request(
query=query, params=params, return_type="branchUpdate", parse_response=False
)
def delete(self, stream_id: str, branch_id: str):
"""Delete a branch
Arguments:
stream_id {str} -- the id of the stream containing the branch to delete
branch_id {str} -- the branch to delete
Returns:
bool -- True if deletion is successful
"""
query = gql(
"""
mutation BranchDelete($branch: BranchDeleteInput!) {
branchDelete(branch: $branch)
}
"""
)
params = {"branch": {"streamId": stream_id, "id": branch_id}}
return self.make_request(
query=query, params=params, return_type="branchDelete", parse_response=False
)
+237
View File
@@ -0,0 +1,237 @@
from typing import List, Optional
from gql import gql
from specklepy.core.api.models import Commit
from specklepy.core.api.resource import ResourceBase
NAME = "commit"
class Resource(ResourceBase):
"""API Access class for commits"""
def __init__(self, account, basepath, client) -> None:
super().__init__(
account=account,
basepath=basepath,
client=client,
name=NAME,
)
self.schema = Commit
def get(self, stream_id: str, commit_id: str) -> Commit:
"""
Gets a commit given a stream and the commit id
Arguments:
stream_id {str} -- the stream where we can find the commit
commit_id {str} -- the id of the commit you want to get
Returns:
Commit -- the retrieved commit object
"""
query = gql(
"""
query Commit($stream_id: String!, $commit_id: String!) {
stream(id: $stream_id) {
commit(id: $commit_id) {
id
message
referencedObject
authorId
authorName
authorAvatar
branchName
createdAt
sourceApplication
totalChildrenCount
parents
}
}
}
"""
)
params = {"stream_id": stream_id, "commit_id": commit_id}
return self.make_request(
query=query, params=params, return_type=["stream", "commit"]
)
def list(self, stream_id: str, limit: int = 10) -> List[Commit]:
"""
Get a list of commits on a given stream
Arguments:
stream_id {str} -- the stream where the commits are
limit {int} -- the maximum number of commits to fetch (default = 10)
Returns:
List[Commit] -- a list of the most recent commit objects
"""
query = gql(
"""
query Commits($stream_id: String!, $limit: Int!) {
stream(id: $stream_id) {
commits(limit: $limit) {
items {
id
message
referencedObject
authorName
authorId
authorName
authorAvatar
branchName
createdAt
sourceApplication
totalChildrenCount
parents
}
}
}
}
"""
)
params = {"stream_id": stream_id, "limit": limit}
return self.make_request(
query=query, params=params, return_type=["stream", "commits", "items"]
)
def create(
self,
stream_id: str,
object_id: str,
branch_name: str = "main",
message: str = "",
source_application: str = "python",
parents: List[str] = None,
) -> str:
"""
Creates a commit on a branch
Arguments:
stream_id {str} -- the stream you want to commit to
object_id {str} -- the hash of your commit object
branch_name {str}
-- the name of the branch to commit to (defaults to "main")
message {str}
-- optional: a message to give more information about the commit
source_application{str}
-- optional: the application from which the commit was created
(defaults to "python")
parents {List[str]} -- optional: the id of the parent commits
Returns:
str -- the id of the created commit
"""
query = gql(
"""
mutation CommitCreate ($commit: CommitCreateInput!)
{ commitCreate(commit: $commit)}
"""
)
params = {
"commit": {
"streamId": stream_id,
"branchName": branch_name,
"objectId": object_id,
"message": message,
"sourceApplication": source_application,
}
}
if parents:
params["commit"]["parents"] = parents
return self.make_request(
query=query, params=params, return_type="commitCreate", parse_response=False
)
def update(self, stream_id: str, commit_id: str, message: str) -> bool:
"""
Update a commit
Arguments:
stream_id {str}
-- the id of the stream that contains the commit you'd like to update
commit_id {str} -- the id of the commit you'd like to update
message {str} -- the updated commit message
Returns:
bool -- True if the operation succeeded
"""
query = gql(
"""
mutation CommitUpdate($commit: CommitUpdateInput!)
{ commitUpdate(commit: $commit)}
"""
)
params = {
"commit": {"streamId": stream_id, "id": commit_id, "message": message}
}
return self.make_request(
query=query, params=params, return_type="commitUpdate", parse_response=False
)
def delete(self, stream_id: str, commit_id: str) -> bool:
"""
Delete a commit
Arguments:
stream_id {str}
-- the id of the stream that contains the commit you'd like to delete
commit_id {str} -- the id of the commit you'd like to delete
Returns:
bool -- True if the operation succeeded
"""
query = gql(
"""
mutation CommitDelete($commit: CommitDeleteInput!)
{ commitDelete(commit: $commit)}
"""
)
params = {"commit": {"streamId": stream_id, "id": commit_id}}
return self.make_request(
query=query, params=params, return_type="commitDelete", parse_response=False
)
def received(
self,
stream_id: str,
commit_id: str,
source_application: str = "python",
message: Optional[str] = None,
) -> bool:
"""
Mark a commit object a received by the source application.
"""
query = gql(
"""
mutation CommitReceive($receivedInput:CommitReceivedInput!){
commitReceive(input:$receivedInput)
}
"""
)
params = {
"receivedInput": {
"sourceApplication": source_application,
"streamId": stream_id,
"commitId": commit_id,
"message": "message",
}
}
try:
return self.make_request(
query=query,
params=params,
return_type="commitReceive",
parse_response=False,
)
except Exception as ex:
print(ex.with_traceback)
return False
@@ -0,0 +1,92 @@
from typing import Dict, List
from gql import gql
from specklepy.core.api.resource import ResourceBase
from specklepy.objects.base import Base
NAME = "object"
class Resource(ResourceBase):
"""API Access class for objects"""
def __init__(self, account, basepath, client) -> None:
super().__init__(
account=account,
basepath=basepath,
client=client,
name=NAME,
)
self.schema = Base
def get(self, stream_id: str, object_id: str) -> Base:
"""
Get a stream object
Arguments:
stream_id {str} -- the id of the stream for the object
object_id {str} -- the hash of the object you want to get
Returns:
Base -- the returned Base object
"""
query = gql(
"""
query Object($stream_id: String!, $object_id: String!) {
stream(id: $stream_id) {
id
name
object(id: $object_id) {
id
speckleType
applicationId
createdAt
totalChildrenCount
data
}
}
}
"""
)
params = {"stream_id": stream_id, "object_id": object_id}
return self.make_request(
query=query,
params=params,
return_type=["stream", "object", "data"],
)
def create(self, stream_id: str, objects: List[Dict]) -> str:
"""
Not advised - generally, you want to use `operations.send()`.
Create a new object on a stream.
To send a base object, you can prepare it by running it through the
`BaseObjectSerializer.traverse_base()` function to get a valid (serialisable)
object to send.
NOTE: this does not create a commit - you can create one with
`SpeckleClient.commit.create`.
Dynamic fields will be located in the 'data' dict of the received `Base` object
Arguments:
stream_id {str} -- the id of the stream you want to send the object to
objects {List[Dict]}
-- a list of base dictionary objects (NOTE: must be json serialisable)
Returns:
str -- the id of the object
"""
query = gql(
"""
mutation ObjectCreate($object_input: ObjectCreateInput!) {
objectCreate(objectInput: $object_input)
}
"""
)
params = {"object_input": {"streamId": stream_id, "objects": objects}}
return self.make_request(
query=query, params=params, return_type="objectCreate", parse_response=False
)
@@ -0,0 +1,172 @@
from datetime import datetime, timezone
from typing import List, Optional, Union
from gql import gql
from specklepy.core.api.models import ActivityCollection, LimitedUser
from specklepy.core.api.resource import ResourceBase
from specklepy.logging.exceptions import SpeckleException
NAME = "other_user"
class Resource(ResourceBase):
"""API Access class for other users, that are not the currently active user."""
def __init__(self, account, basepath, client, server_version) -> None:
super().__init__(
account=account,
basepath=basepath,
client=client,
name=NAME,
server_version=server_version,
)
self.schema = LimitedUser
def get(self, id: str) -> LimitedUser:
"""
Gets the profile of another user.
Arguments:
id {str} -- the user id
Returns:
LimitedUser -- the retrieved profile of another user
"""
query = gql(
"""
query OtherUser($id: String!) {
otherUser(id: $id) {
id
name
bio
company
avatar
verified
role
}
}
"""
)
params = {"id": id}
return self.make_request(query=query, params=params, return_type="otherUser")
def search(
self, search_query: str, limit: int = 25
) -> Union[List[LimitedUser], SpeckleException]:
"""Searches for user by name or email. The search query must be at least
3 characters long
Arguments:
search_query {str} -- a string to search for
limit {int} -- the maximum number of results to return
Returns:
List[LimitedUser] -- a list of User objects that match the search query
"""
if len(search_query) < 3:
return SpeckleException(
message="User search query must be at least 3 characters"
)
query = gql(
"""
query UserSearch($search_query: String!, $limit: Int!) {
userSearch(query: $search_query, limit: $limit) {
items {
id
name
bio
company
avatar
verified
}
}
}
"""
)
params = {"search_query": search_query, "limit": limit}
return self.make_request(
query=query, params=params, return_type=["userSearch", "items"]
)
def activity(
self,
user_id: str,
limit: int = 20,
action_type: Optional[str] = None,
before: Optional[datetime] = None,
after: Optional[datetime] = None,
cursor: Optional[datetime] = None,
) -> ActivityCollection:
"""
Get the activity from a given stream in an Activity collection.
Step into the activity `items` for the list of activity.
Note: all timestamps arguments should be `datetime` of
any tz as they will be converted to UTC ISO format strings
user_id {str} -- the id of the user to get the activity from
action_type {str} -- filter results to a single action type
(eg: `commit_create` or `commit_receive`)
limit {int} -- max number of Activity items to return
before {datetime} -- latest cutoff for activity
(ie: return all activity _before_ this time)
after {datetime} -- oldest cutoff for activity
(ie: return all activity _after_ this time)
cursor {datetime} -- timestamp cursor for pagination
"""
query = gql(
"""
query UserActivity(
$user_id: String!,
$action_type: String,
$before:DateTime,
$after: DateTime,
$cursor: DateTime,
$limit: Int
){
otherUser(id: $user_id) {
activity(
actionType: $action_type,
before: $before,
after: $after,
cursor: $cursor,
limit: $limit
) {
totalCount
cursor
items {
actionType
info
userId
streamId
resourceId
resourceType
message
time
}
}
}
}
"""
)
params = {
"user_id": user_id,
"limit": limit,
"action_type": action_type,
"before": before.astimezone(timezone.utc).isoformat() if before else before,
"after": after.astimezone(timezone.utc).isoformat() if after else after,
"cursor": cursor.astimezone(timezone.utc).isoformat() if cursor else cursor,
}
return self.make_request(
query=query,
params=params,
return_type=["otherUser", "activity"],
schema=ActivityCollection,
)
+174
View File
@@ -0,0 +1,174 @@
import re
from typing import Any, Dict, List, Tuple
from gql import gql
from specklepy.core.api.models import ServerInfo
from specklepy.core.api.resource import ResourceBase
from specklepy.logging.exceptions import GraphQLException
NAME = "server"
class Resource(ResourceBase):
"""API Access class for the server"""
def __init__(self, account, basepath, client) -> None:
super().__init__(
account=account,
basepath=basepath,
client=client,
name=NAME,
)
def get(self) -> ServerInfo:
"""Get the server info
Returns:
dict -- the server info in dictionary form
"""
query = gql(
"""
query Server {
serverInfo {
name
company
description
adminContact
canonicalUrl
version
roles {
name
description
resourceTarget
}
scopes {
name
description
}
authStrategies{
id
name
icon
}
}
}
"""
)
return self.make_request(
query=query, return_type="serverInfo", schema=ServerInfo
)
def version(self) -> Tuple[Any, ...]:
"""Get the server version
Returns:
the server version in the format (major, minor, patch, (tag, build))
eg (2, 6, 3) for a stable build and (2, 6, 4, 'alpha', 4711) for alpha
"""
# not tracking as it will be called along with other mutations / queries as a check
query = gql(
"""
query Server {
serverInfo {
version
}
}
"""
)
ver = self.make_request(
query=query, return_type=["serverInfo", "version"], parse_response=False
)
if isinstance(ver, Exception):
raise GraphQLException(
f"Could not get server version for {self.basepath}", [ver]
)
# pylint: disable=consider-using-generator; (list comp is faster)
return tuple(
[
int(segment) if segment.isdigit() else segment
for segment in re.split(r"\.|-", ver)
]
)
def apps(self) -> Dict:
"""Get the apps registered on the server
Returns:
dict -- a dictionary of apps registered on the server
"""
query = gql(
"""
query Apps {
apps{
id
name
description
termsAndConditionsLink
trustByDefault
logo
author {
id
name
avatar
}
}
}
"""
)
return self.make_request(query=query, return_type="apps", parse_response=False)
def create_token(self, name: str, scopes: List[str], lifespan: int) -> str:
"""Create a personal API token
Arguments:
scopes {List[str]} -- the scopes to grant with this token
name {str} -- a name for your new token
lifespan {int} -- duration before the token expires
Returns:
str -- the new API token. note: this is the only time you'll see the token!
"""
query = gql(
"""
mutation TokenCreate($token: ApiTokenCreateInput!) {
apiTokenCreate(token: $token)
}
"""
)
params = {"token": {"scopes": scopes, "name": name, "lifespan": lifespan}}
return self.make_request(
query=query,
params=params,
return_type="apiTokenCreate",
parse_response=False,
)
def revoke_token(self, token: str) -> bool:
"""Revokes (deletes) a personal API token
Arguments:
token {str} -- the token to revoke (delete)
Returns:
bool -- True if the token was successfully deleted
"""
query = gql(
"""
mutation TokenRevoke($token: String!) {
apiTokenRevoke(token: $token)
}
"""
)
params = {"token": token}
return self.make_request(
query=query,
params=params,
return_type="apiTokenRevoke",
parse_response=False,
)
+751
View File
@@ -0,0 +1,751 @@
from datetime import datetime, timezone
from typing import List, Optional
from deprecated import deprecated
from gql import gql
from specklepy.core.api.models import ActivityCollection, PendingStreamCollaborator, Stream
from specklepy.core.api.resource import ResourceBase
from specklepy.logging.exceptions import SpeckleException, UnsupportedException
NAME = "stream"
class Resource(ResourceBase):
"""API Access class for streams"""
def __init__(self, account, basepath, client, server_version) -> None:
super().__init__(
account=account,
basepath=basepath,
client=client,
name=NAME,
server_version=server_version,
)
self.schema = Stream
def get(self, id: str, branch_limit: int = 10, commit_limit: int = 10) -> Stream:
"""Get the specified stream from the server
Arguments:
id {str} -- the stream id
branch_limit {int} -- the maximum number of branches to return
commit_limit {int} -- the maximum number of commits to return
Returns:
Stream -- the retrieved stream
"""
query = gql(
"""
query Stream($id: String!, $branch_limit: Int!, $commit_limit: Int!) {
stream(id: $id) {
id
name
role
description
isPublic
createdAt
updatedAt
commentCount
favoritesCount
collaborators {
id
name
role
avatar
}
branches(limit: $branch_limit) {
totalCount
cursor
items {
id
name
description
commits(limit: $commit_limit) {
totalCount
cursor
items {
id
message
authorId
createdAt
authorName
referencedObject
sourceApplication
}
}
}
}
}
}
"""
)
params = {"id": id, "branch_limit": branch_limit, "commit_limit": commit_limit}
return self.make_request(query=query, params=params, return_type="stream")
def list(self, stream_limit: int = 10) -> List[Stream]:
"""Get a list of the user's streams
Arguments:
stream_limit {int} -- The maximum number of streams to return
Returns:
List[Stream] -- A list of Stream objects
"""
query = gql(
"""
query User($stream_limit: Int!) {
user {
id
bio
name
email
avatar
company
verified
profiles
role
streams(limit: $stream_limit) {
totalCount
cursor
items {
id
name
role
isPublic
createdAt
updatedAt
description
commentCount
favoritesCount
collaborators {
id
name
role
}
}
}
}
}
"""
)
params = {"stream_limit": stream_limit}
return self.make_request(
query=query, params=params, return_type=["user", "streams", "items"]
)
def create(
self,
name: str = "Anonymous Python Stream",
description: str = "No description provided",
is_public: bool = True,
) -> str:
"""Create a new stream
Arguments:
name {str} -- the name of the string
description {str} -- a short description of the stream
is_public {bool}
-- whether or not the stream can be viewed by anyone with the id
Returns:
id {str} -- the id of the newly created stream
"""
query = gql(
"""
mutation StreamCreate($stream: StreamCreateInput!) {
streamCreate(stream: $stream)
}
"""
)
params = {
"stream": {"name": name, "description": description, "isPublic": is_public}
}
return self.make_request(
query=query, params=params, return_type="streamCreate", parse_response=False
)
def update(
self,
id: str,
name: Optional[str] = None,
description: Optional[str] = None,
is_public: Optional[bool] = None,
) -> bool:
"""Update an existing stream
Arguments:
id {str} -- the id of the stream to be updated
name {str} -- the name of the string
description {str} -- a short description of the stream
is_public {bool}
-- whether or not the stream can be viewed by anyone with the id
Returns:
bool -- whether the stream update was successful
"""
query = gql(
"""
mutation StreamUpdate($stream: StreamUpdateInput!) {
streamUpdate(stream: $stream)
}
"""
)
params = {
"id": id,
"name": name,
"description": description,
"isPublic": is_public,
}
# remove None values so graphql doesn't cry
params = {"stream": {k: v for k, v in params.items() if v is not None}}
return self.make_request(
query=query, params=params, return_type="streamUpdate", parse_response=False
)
def delete(self, id: str) -> bool:
"""Delete a stream given its id
Arguments:
id {str} -- the id of the stream to delete
Returns:
bool -- whether the deletion was successful
"""
query = gql(
"""
mutation StreamDelete($id: String!) {
streamDelete(id: $id)
}
"""
)
params = {"id": id}
return self.make_request(
query=query, params=params, return_type="streamDelete", parse_response=False
)
def search(
self,
search_query: str,
limit: int = 25,
branch_limit: int = 10,
commit_limit: int = 10,
):
"""Search for streams by name, description, or id
Arguments:
search_query {str} -- a string to search for
limit {int} -- the maximum number of results to return
branch_limit {int} -- the maximum number of branches to return
commit_limit {int} -- the maximum number of commits to return
Returns:
List[Stream] -- a list of Streams that match the search query
"""
query = gql(
"""
query StreamSearch(
$search_query: String!,
$limit: Int!,
$branch_limit:Int!,
$commit_limit:Int!
) {
streams(query: $search_query, limit: $limit) {
items {
id
name
role
description
isPublic
createdAt
updatedAt
collaborators {
id
name
role
avatar
}
branches(limit: $branch_limit) {
totalCount
cursor
items {
id
name
description
commits(limit: $commit_limit) {
totalCount
cursor
items {
id
referencedObject
message
authorName
authorId
createdAt
}
}
}
}
}
}
}
"""
)
params = {
"search_query": search_query,
"limit": limit,
"branch_limit": branch_limit,
"commit_limit": commit_limit,
}
return self.make_request(
query=query, params=params, return_type=["streams", "items"]
)
def favorite(self, stream_id: str, favorited: bool = True):
"""Favorite or unfavorite the given stream.
Arguments:
stream_id {str} -- the id of the stream to favorite / unfavorite
favorited {bool}
-- whether to favorite (True) or unfavorite (False) the stream
Returns:
Stream -- the stream with its `id`, `name`, and `favoritedDate`
"""
query = gql(
"""
mutation StreamFavorite($stream_id: String!, $favorited: Boolean!) {
streamFavorite(streamId: $stream_id, favorited: $favorited) {
id
name
favoritedDate
favoritesCount
}
}
"""
)
params = {
"stream_id": stream_id,
"favorited": favorited,
}
return self.make_request(
query=query, params=params, return_type=["streamFavorite"]
)
def get_all_pending_invites(
self, stream_id: str
) -> List[PendingStreamCollaborator]:
"""Get all of the pending invites on a stream.
You must be a `stream:owner` to query this.
Requires Speckle Server version >= 2.6.4
Arguments:
stream_id {str} -- the stream id from which to get the pending invites
Returns:
List[PendingStreamCollaborator]
-- a list of pending invites for the specified stream
"""
self._check_invites_supported()
query = gql(
"""
query StreamInvites($streamId: String!) {
stream(id: $streamId){
pendingCollaborators {
id
token
inviteId
streamId
streamName
title
role
invitedBy{
id
name
company
avatar
}
user {
id
name
company
avatar
}
}
}
}
"""
)
params = {"streamId": stream_id}
return self.make_request(
query=query,
params=params,
return_type=["stream", "pendingCollaborators"],
schema=PendingStreamCollaborator,
)
def invite(
self,
stream_id: str,
email: Optional[str] = None,
user_id: Optional[str] = None,
role: str = "stream:contributor", # should default be reviewer?
message: Optional[str] = None,
):
"""Invite someone to a stream using either their email or user id
Requires Speckle Server version >= 2.6.4
Arguments:
stream_id {str} -- the id of the stream to invite the user to
email {str} -- the email of the user to invite (use this OR `user_id`)
user_id {str} -- the id of the user to invite (use this OR `email`)
role {str}
-- the role to assign to the user (defaults to `stream:contributor`)
message {str}
-- a message to send along with this invite to the specified user
Returns:
bool -- True if the operation was successful
"""
self._check_invites_supported()
if email is None and user_id is None:
raise SpeckleException(
"You must provide either an email or a user id to use the"
" `stream.invite` method"
)
query = gql(
"""
mutation StreamInviteCreate($input: StreamInviteCreateInput!) {
streamInviteCreate(input: $input)
}
"""
)
params = {
"email": email,
"userId": user_id,
"streamId": stream_id,
"message": message,
"role": role,
}
params = {"input": {k: v for k, v in params.items() if v is not None}}
return self.make_request(
query=query,
params=params,
return_type="streamInviteCreate",
parse_response=False,
)
def invite_batch(
self,
stream_id: str,
emails: Optional[List[str]] = None,
user_ids: Optional[List[None]] = None,
message: Optional[str] = None,
) -> bool:
"""Invite a batch of users to a specified stream.
Requires Speckle Server version >= 2.6.4
Arguments:
stream_id {str} -- the id of the stream to invite the user to
emails {List[str]}
-- the email of the user to invite (use this and/or `user_ids`)
user_id {List[str]}
-- the id of the user to invite (use this and/or `emails`)
message {str}
-- a message to send along with this invite to the specified user
Returns:
bool -- True if the operation was successful
"""
self._check_invites_supported()
if emails is None and user_ids is None:
raise SpeckleException(
"You must provide either an email or a user id to use the"
" `stream.invite` method"
)
query = gql(
"""
mutation StreamInviteBatchCreate($input: [StreamInviteCreateInput!]!) {
streamInviteBatchCreate(input: $input)
}
"""
)
email_invites = [
{"streamId": stream_id, "message": message, "email": email}
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 else [])
if user_id is not None
]
params = {"input": [*email_invites, *user_invites]}
return self.make_request(
query=query,
params=params,
return_type="streamInviteBatchCreate",
parse_response=False,
)
def invite_cancel(self, stream_id: str, invite_id: str) -> bool:
"""Cancel an existing stream invite
Requires Speckle Server version >= 2.6.4
Arguments:
stream_id {str} -- the id of the stream invite
invite_id {str} -- the id of the invite to use
Returns:
bool -- true if the operation was successful
"""
self._check_invites_supported()
query = gql(
"""
mutation StreamInviteCancel($streamId: String!, $inviteId: String!) {
streamInviteCancel(streamId: $streamId, inviteId: $inviteId)
}
"""
)
params = {"streamId": stream_id, "inviteId": invite_id}
return self.make_request(
query=query,
params=params,
return_type="streamInviteCancel",
parse_response=False,
)
def invite_use(self, stream_id: str, token: str, accept: bool = True) -> bool:
"""Accept or decline a stream invite
Requires Speckle Server version >= 2.6.4
Arguments:
stream_id {str}
-- the id of the stream for which the user has a pending invite
token {str} -- the token of the invite to use
accept {bool} -- whether or not to accept the invite (defaults to True)
Returns:
bool -- true if the operation was successful
"""
self._check_invites_supported()
query = gql(
"""
mutation StreamInviteUse(
$accept: Boolean!,
$streamId: String!,
$token: String!
) {
streamInviteUse(accept: $accept, streamId: $streamId, token: $token)
}
"""
)
params = {"streamId": stream_id, "token": token, "accept": accept}
return self.make_request(
query=query,
params=params,
return_type="streamInviteUse",
parse_response=False,
)
def update_permission(self, stream_id: str, user_id: str, role: str):
"""Updates permissions for a user on a given stream
Valid for Speckle Server >=2.6.4
Arguments:
stream_id {str} -- the id of the stream to grant permissions to
user_id {str} -- the id of the user to grant permissions for
role {str} -- the role to grant the user
Returns:
bool -- True if the operation was successful
"""
if self.server_version and (
self.server_version != ("dev",) and self.server_version < (2, 6, 4)
):
raise UnsupportedException(
"Server mutation `update_permission` is only supported as of Speckle"
" Server v2.6.4. Please update your Speckle Server to use this method"
" or use the `grant_permission` method instead."
)
query = gql(
"""
mutation StreamUpdatePermission(
$permission_params: StreamUpdatePermissionInput!
) {
streamUpdatePermission(permissionParams: $permission_params)
}
"""
)
params = {
"permission_params": {
"streamId": stream_id,
"userId": user_id,
"role": role,
}
}
return self.make_request(
query=query,
params=params,
return_type="streamUpdatePermission",
parse_response=False,
)
def revoke_permission(self, stream_id: str, user_id: str):
"""Revoke permissions from a user on a given stream
Arguments:
stream_id {str} -- the id of the stream to revoke permissions from
user_id {str} -- the id of the user to revoke permissions from
Returns:
bool -- True if the operation was successful
"""
query = gql(
"""
mutation StreamRevokePermission(
$permission_params: StreamRevokePermissionInput!
) {
streamRevokePermission(permissionParams: $permission_params)
}
"""
)
params = {"permission_params": {"streamId": stream_id, "userId": user_id}}
return self.make_request(
query=query,
params=params,
return_type="streamRevokePermission",
parse_response=False,
)
def activity(
self,
stream_id: str,
action_type: Optional[str] = None,
limit: int = 20,
before: Optional[datetime] = None,
after: Optional[datetime] = None,
cursor: Optional[datetime] = None,
):
"""
Get the activity from a given stream in an Activity collection.
Step into the activity `items` for the list of activity.
Note: all timestamps arguments should be `datetime` of any tz
as they will be converted to UTC ISO format strings
stream_id {str} -- the id of the stream to get activity from
action_type {str}
-- filter results to a single action type
(eg: `commit_create` or `commit_receive`)
limit {int} -- max number of Activity items to return
before {datetime}
-- latest cutoff for activity (ie: return all activity _before_ this time)
after {datetime}
-- oldest cutoff for activity (ie: return all activity _after_ this time)
cursor {datetime} -- timestamp cursor for pagination
"""
query = gql(
"""
query StreamActivity(
$stream_id: String!,
$action_type: String,
$before:DateTime,
$after: DateTime,
$cursor: DateTime,
$limit: Int
){
stream(id: $stream_id) {
activity(
actionType: $action_type,
before: $before,
after: $after,
cursor: $cursor,
limit: $limit
) {
totalCount
cursor
items {
actionType
info
userId
streamId
resourceId
resourceType
message
time
}
}
}
}
"""
)
try:
params = {
"stream_id": stream_id,
"limit": limit,
"action_type": action_type,
"before": before.astimezone(timezone.utc).isoformat()
if before
else before,
"after": after.astimezone(timezone.utc).isoformat() if after else after,
"cursor": cursor.astimezone(timezone.utc).isoformat()
if cursor
else cursor,
}
except AttributeError as e:
raise SpeckleException(
"Could not get stream activity - `before`, `after`, and `cursor` must"
" be in `datetime` format if provided",
ValueError(),
) from e
return self.make_request(
query=query,
params=params,
return_type=["stream", "activity"],
schema=ActivityCollection,
)
@@ -0,0 +1,138 @@
from functools import wraps
from typing import Callable, Dict, List, Optional, Union
from gql import gql
from graphql import DocumentNode
from specklepy.core.api.resource import ResourceBase
from specklepy.core.api.resources.stream import Stream
from specklepy.logging.exceptions import SpeckleException
NAME = "subscribe"
def check_wsclient(function):
@wraps(function)
async def check_wsclient_wrapper(self, *args, **kwargs):
if self.client is None:
raise SpeckleException(
"You must authenticate before you can subscribe to events"
)
else:
return await function(self, *args, **kwargs)
return check_wsclient_wrapper
class Resource(ResourceBase):
"""API Access class for subscriptions"""
def __init__(self, account, basepath, client) -> None:
super().__init__(
account=account,
basepath=basepath,
client=client,
name=NAME,
)
@check_wsclient
async def stream_added(self, callback: Optional[Callable] = None):
"""Subscribes to new stream added event for your profile.
Use this to display an up-to-date list of streams.
Arguments:
callback {Callable[Stream]} -- a function that takes the updated stream
as an argument and executes each time a stream is added
Returns:
Stream -- the update stream
"""
query = gql(
"""
subscription { userStreamAdded }
"""
)
return await self.subscribe(
query=query, callback=callback, return_type="userStreamAdded", schema=Stream
)
@check_wsclient
async def stream_updated(self, id: str, callback: Optional[Callable] = None):
"""
Subscribes to stream updated event.
Use this in clients/components that pertain only to this stream.
Arguments:
id {str} -- the stream id of the stream to subscribe to
callback {Callable[Stream]}
-- a function that takes the updated stream
as an argument and executes each time the stream is updated
Returns:
Stream -- the update stream
"""
query = gql(
"""
subscription Update($id: String!) { streamUpdated(streamId: $id) }
"""
)
params = {"id": id}
return await self.subscribe(
query=query,
params=params,
callback=callback,
return_type="streamUpdated",
schema=Stream,
)
@check_wsclient
async def stream_removed(self, callback: Optional[Callable] = None):
"""Subscribes to stream removed event for your profile.
Use this to display an up-to-date list of streams for your profile.
NOTE: If someone revokes your permissions on a stream,
this subscription will be triggered with an extra value of revokedBy
in the payload.
Arguments:
callback {Callable[Dict]}
-- a function that takes the returned dict as an argument
and executes each time a stream is removed
Returns:
dict -- dict containing 'id' of stream removed and optionally 'revokedBy'
"""
query = gql(
"""
subscription { userStreamRemoved }
"""
)
return await self.subscribe(
query=query,
callback=callback,
return_type="userStreamRemoved",
parse_response=False,
)
@check_wsclient
async def subscribe(
self,
query: DocumentNode,
params: Optional[Dict] = None,
callback: Optional[Callable] = None,
return_type: Optional[Union[str, List]] = None,
schema=None,
parse_response: bool = True,
):
# if self.client.transport.websocket is None:
# TODO: add multiple subs to the same ws connection
async with self.client as session:
async for res in session.subscribe(query, variable_values=params):
res = self._step_into_response(response=res, return_type=return_type)
if parse_response:
res = self._parse_response(response=res, schema=schema)
if callback is not None:
callback(res)
else:
return res
+322
View File
@@ -0,0 +1,322 @@
from datetime import datetime, timezone
from typing import List, Optional, Union
from deprecated import deprecated
from gql import gql
from specklepy.core.api.models import ActivityCollection, PendingStreamCollaborator, User
from specklepy.core.api.resource import ResourceBase
from specklepy.logging.exceptions import SpeckleException
NAME = "user"
DEPRECATION_VERSION = "2.9.0"
DEPRECATION_TEXT = (
"The user resource is deprecated, please use the active_user or other_user"
" resources"
)
class Resource(ResourceBase):
"""API Access class for users"""
def __init__(self, account, basepath, client, server_version) -> None:
super().__init__(
account=account,
basepath=basepath,
client=client,
name=NAME,
server_version=server_version,
)
self.schema = User
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
def get(self, id: Optional[str] = None) -> User:
"""
Gets the profile of a user.
If no id argument is provided, will return the current authenticated
user's profile (as extracted from the authorization header).
Arguments:
id {str} -- the user id
Returns:
User -- the retrieved user
"""
query = gql(
"""
query User($id: String) {
user(id: $id) {
id
email
name
bio
company
avatar
verified
profiles
role
}
}
"""
)
params = {"id": id}
return self.make_request(query=query, params=params, return_type="user")
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
def search(
self, search_query: str, limit: int = 25
) -> Union[List[User], SpeckleException]:
"""
Searches for user by name or email.
The search query must be at least 3 characters long
Arguments:
search_query {str} -- a string to search for
limit {int} -- the maximum number of results to return
Returns:
List[User] -- a list of User objects that match the search query
"""
if len(search_query) < 3:
return SpeckleException(
message="User search query must be at least 3 characters"
)
query = gql(
"""
query UserSearch($search_query: String!, $limit: Int!) {
userSearch(query: $search_query, limit: $limit) {
items {
id
name
bio
company
avatar
verified
}
}
}
"""
)
params = {"search_query": search_query, "limit": limit}
return self.make_request(
query=query, params=params, return_type=["userSearch", "items"]
)
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
def update(
self,
name: Optional[str] = None,
company: Optional[str] = None,
bio: Optional[str] = None,
avatar: Optional[str] = None,
):
"""Updates your user profile. All arguments are optional.
Arguments:
name {str} -- your name
company {str} -- the company you may or may not work for
bio {str} -- tell us about yourself
avatar {str} -- a nice photo of yourself
Returns:
bool -- True if your profile was updated successfully
"""
query = gql(
"""
mutation UserUpdate($user: UserUpdateInput!) {
userUpdate(user: $user)
}
"""
)
params = {"name": name, "company": company, "bio": bio, "avatar": avatar}
params = {"user": {k: v for k, v in params.items() if v is not None}}
if not params["user"]:
return SpeckleException(
message=(
"You must provide at least one field to update your user profile"
)
)
return self.make_request(
query=query, params=params, return_type="userUpdate", parse_response=False
)
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
def activity(
self,
user_id: Optional[str] = None,
limit: int = 20,
action_type: Optional[str] = None,
before: Optional[datetime] = None,
after: Optional[datetime] = None,
cursor: Optional[datetime] = None,
):
"""
Get the activity from a given stream in an Activity collection.
Step into the activity `items` for the list of activity.
If no id argument is provided, will return the current authenticated
user's activity (as extracted from the authorization header).
Note: all timestamps arguments should be `datetime` of any tz as
they will be converted to UTC ISO format strings
user_id {str} -- the id of the user to get the activity from
action_type {str} -- filter results to a single action type
(eg: `commit_create` or `commit_receive`)
limit {int} -- max number of Activity items to return
before {datetime}
-- latest cutoff for activity (ie: return all activity _before_ this time)
after {datetime}
-- oldest cutoff for activity (ie: return all activity _after_ this time)
cursor {datetime} -- timestamp cursor for pagination
"""
query = gql(
"""
query UserActivity(
$user_id: String,
$action_type: String,
$before:DateTime,
$after: DateTime,
$cursor: DateTime,
$limit: Int
){
user(id: $user_id) {
activity(
actionType: $action_type,
before: $before,
after: $after,
cursor: $cursor,
limit: $limit
) {
totalCount
cursor
items {
actionType
info
userId
streamId
resourceId
resourceType
message
time
}
}
}
}
"""
)
params = {
"user_id": user_id,
"limit": limit,
"action_type": action_type,
"before": before.astimezone(timezone.utc).isoformat() if before else before,
"after": after.astimezone(timezone.utc).isoformat() if after else after,
"cursor": cursor.astimezone(timezone.utc).isoformat() if cursor else cursor,
}
return self.make_request(
query=query,
params=params,
return_type=["user", "activity"],
schema=ActivityCollection,
)
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
def get_all_pending_invites(self) -> List[PendingStreamCollaborator]:
"""Get all of the active user's pending stream invites
Requires Speckle Server version >= 2.6.4
Returns:
List[PendingStreamCollaborator]
-- a list of pending invites for the current user
"""
self._check_invites_supported()
query = gql(
"""
query StreamInvites {
streamInvites{
id
token
inviteId
streamId
streamName
title
role
invitedBy {
id
name
company
avatar
}
}
}
"""
)
return self.make_request(
query=query,
return_type="streamInvites",
schema=PendingStreamCollaborator,
)
@deprecated(version=DEPRECATION_VERSION, reason=DEPRECATION_TEXT)
def get_pending_invite(
self, stream_id: str, token: Optional[str] = None
) -> Optional[PendingStreamCollaborator]:
"""Get a particular pending invite for the active user on a given stream.
If no invite_id is provided, any valid invite will be returned.
Requires Speckle Server version >= 2.6.4
Arguments:
stream_id {str} -- the id of the stream to look for invites on
token {str} -- the token of the invite to look for (optional)
Returns:
PendingStreamCollaborator
-- the invite for the given stream (or None if it isn't found)
"""
self._check_invites_supported()
query = gql(
"""
query StreamInvite($streamId: String!, $token: String) {
streamInvite(streamId: $streamId, token: $token) {
id
token
streamId
streamName
title
role
invitedBy {
id
name
company
avatar
}
}
}
"""
)
params = {"streamId": stream_id}
if token:
params["token"] = token
return self.make_request(
query=query,
params=params,
return_type="streamInvite",
schema=PendingStreamCollaborator,
)
+186
View File
@@ -0,0 +1,186 @@
from urllib.parse import unquote, urlparse
from warnings import warn
from specklepy.core.api.client import SpeckleClient
from specklepy.core.api.credentials import (
Account,
get_account_from_token,
get_local_accounts,
)
from specklepy.logging.exceptions import SpeckleException, SpeckleWarning
from specklepy.transports.server.server import ServerTransport
class StreamWrapper:
"""
The `StreamWrapper` gives you some handy helpers to deal with urls and
get authenticated clients and transports.
Construct a `StreamWrapper` with a stream, branch, commit, or object URL.
The corresponding ids will be stored
in the wrapper. If you have local accounts on the machine,
you can use the `get_account` and `get_client` methods
to get a local account for the server. You can also pass a token into `get_client`
if you don't have a corresponding
local account for the server.
```py
from specklepy.api.wrapper import StreamWrapper
# provide any stream, branch, commit, object, or globals url
wrapper = StreamWrapper("https://speckle.xyz/streams/3073b96e86/commits/604bea8cc6")
# get an authenticated SpeckleClient if you have a local account for the server
client = wrapper.get_client()
# get an authenticated ServerTransport if you have a local account for the server
transport = wrapper.get_transport()
```
"""
stream_url: str = None
use_ssl: bool = True
host: str = None
stream_id: str = None
commit_id: str = None
object_id: str = None
branch_name: str = None
_client: SpeckleClient = None
_account: Account = None
def __repr__(self):
return (
f"StreamWrapper( server: {self.host}, stream_id: {self.stream_id}, type:"
f" {self.type} )"
)
def __str__(self) -> str:
return self.__repr__()
@property
def type(self) -> str:
if self.object_id:
return "object"
elif self.commit_id:
return "commit"
elif self.branch_name:
return "branch"
else:
return "stream" if self.stream_id else "invalid"
def __init__(self, url: str) -> None:
self.stream_url = url
parsed = urlparse(url)
self.host = parsed.netloc
self.use_ssl = parsed.scheme == "https"
segments = parsed.path.strip("/").split("/", 3)
if not segments or len(segments) < 2:
raise SpeckleException(
f"Cannot parse {url} into a stream wrapper class - invalid URL"
" provided."
)
while segments:
segment = segments.pop(0)
if segments and segment.lower() == "streams":
self.stream_id = segments.pop(0)
elif segments and segment.lower() == "commits":
self.commit_id = segments.pop(0)
elif segments and segment.lower() == "branches":
self.branch_name = unquote(segments.pop(0))
elif segments and segment.lower() == "objects":
self.object_id = segments.pop(0)
elif segment.lower() == "globals":
self.branch_name = "globals"
if segments:
self.commit_id = segments.pop(0)
else:
raise SpeckleException(
f"Cannot parse {url} into a stream wrapper class - invalid URL"
" provided."
)
if not self.stream_id:
raise SpeckleException(
f"Cannot parse {url} into a stream wrapper class - no stream id found."
)
@property
def server_url(self):
return f"{'https' if self.use_ssl else 'http'}://{self.host}"
def get_account(self, token: str = None) -> Account:
"""
Gets an account object for this server from the local accounts db
(added via Speckle Manager or a json file)
"""
if self._account and self._account.token:
return self._account
self._account = next(
(
a
for a in get_local_accounts()
if self.host == urlparse(a.serverInfo.url).netloc
),
None,
)
if not self._account:
self._account = get_account_from_token(token, self.server_url)
if self._client:
self._client.authenticate_with_account(self._account)
return self._account
def get_client(self, token: str = None) -> SpeckleClient:
"""
Gets an authenticated client for this server.
You may provide a token if there aren't any local accounts on this
machine. If no account is found and no token is provided,
an unauthenticated client is returned.
Arguments:
token {str}
-- optional token if no local account is available (defaults to None)
Returns:
SpeckleClient
-- authenticated with a corresponding local account or the provided token
"""
if self._client and token is None:
return self._client
if not self._account or not self._account.token:
self.get_account(token)
if not self._client:
self._client = SpeckleClient(host=self.host, use_ssl=self.use_ssl)
if self._account.token is None and token is None:
warn(f"No local account found for server {self.host}", SpeckleWarning)
return self._client
if self._account.token:
self._client.authenticate_with_account(self._account)
else:
self._client.authenticate_with_token(token)
return self._client
def get_transport(self, token: str = None) -> ServerTransport:
"""
Gets a server transport for this stream using an authenticated client.
If there is no local account for this
server and the client was not authenticated with a token,
this will throw an exception.
Returns:
ServerTransport -- constructed for this stream
with a pre-authenticated client
"""
if not self._account or not self._account.token:
self.get_account(token)
return ServerTransport(self.stream_id, account=self._account)
+1
View File
@@ -0,0 +1 @@
"""Common helpers module for Core."""
@@ -0,0 +1,127 @@
"""
Provides uniform and consistent path helpers for `specklepy`
"""
import os
import sys
from pathlib import Path
from typing import Optional
from specklepy.logging.exceptions import SpeckleException
_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)
_blob_folder_name = "Blobs"
def override_blob_storage_folder(blob_folder_name: str) -> None:
"""Override the global Blob storage folder name."""
global _blob_folder_name
_blob_folder_name = blob_folder_name
_accounts_folder_name = "Accounts"
def override_accounts_folder_name(accounts_folder_name: str) -> None:
"""Override the global Accounts folder name."""
global _accounts_folder_name
_accounts_folder_name = accounts_folder_name
_objects_folder_name = "Objects"
def override_objects_folder_name(objects_folder_name: str) -> None:
"""Override global Objects folder name."""
global _objects_folder_name
_objects_folder_name = objects_folder_name
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 SpeckleException(
message="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 SpeckleException(
message="Failed to initialize user application data path.", exception=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,
)
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)
def blob_storage_path(path: Optional[Path] = None) -> Path:
return _ensure_folder_exists(path or user_speckle_folder_path(), _blob_folder_name)
+8 -3
View File
@@ -13,9 +13,11 @@ class SpeckleException(Exception):
class SpeckleInvalidUnitException(SpeckleException):
def __init__(self, invalid_unit: Any) -> None:
super().__init__(
message=f"Invalid units: expected type str but received {type(invalid_unit)} ({invalid_unit}).",
message=(
"Invalid units: expected type str but received"
f" {type(invalid_unit)} ({invalid_unit})."
),
exception=None,
)
@@ -27,7 +29,10 @@ class SerializationException(SpeckleException):
self.unhandled_type = type(obj)
def __str__(self) -> str:
return f"SpeckleException: Could not serialize object of type {self.unhandled_type}"
return (
"SpeckleException: Could not serialize object of type"
f" {self.unhandled_type}"
)
class GraphQLException(SpeckleException):
+33 -25
View File
@@ -1,14 +1,14 @@
import sys
import queue
import hashlib
import getpass
import logging
from typing import Optional
import requests
import threading
import platform
import contextlib
import getpass
import hashlib
import logging
import platform
import queue
import sys
import threading
from typing import Optional
import requests
"""
Anonymous telemetry to help us understand how to make a better Speckle.
@@ -23,23 +23,25 @@ LOG = logging.getLogger(__name__)
METRICS_TRACKER = None
# actions
SDK = "SDK Action"
CONNECTOR = "Connector Action"
RECEIVE = "Receive"
SEND = "Send"
STREAM = "Stream Action"
PERMISSION = "Permission Action"
INVITE = "Invite Action"
COMMIT = "Commit Action"
BRANCH = "Branch Action"
USER = "User Action"
OTHER_USER = "Other User Action"
SERVER = "Server Action"
CLIENT = "Speckle Client"
STREAM_WRAPPER = "Stream Wrapper"
# not in use since 2.15
ACCOUNTS = "Get Local Accounts"
SERIALIZE = "serialization/serialize"
BRANCH = "Branch Action"
CLIENT = "Speckle Client"
COMMIT = "Commit Action"
DESERIALIZE = "serialization/deserialize"
INVITE = "Invite Action"
OTHER_USER = "Other User Action"
PERMISSION = "Permission Action"
SERIALIZE = "serialization/serialize"
SERVER = "Server Action"
STREAM = "Stream Action"
STREAM_WRAPPER = "Stream Wrapper"
USER = "User Action"
def disable():
@@ -58,7 +60,11 @@ def set_host_app(host_app: str, host_app_version: Optional[str] = None):
HOST_APP_VERSION = host_app_version or HOST_APP_VERSION
def track(action: str, account: "Account" = None, custom_props: Optional[dict] = None):
def track(
action: str,
account=None,
custom_props: Optional[dict] = None,
):
if not TRACK:
return
try:
@@ -84,7 +90,7 @@ def track(action: str, account: "Account" = None, custom_props: Optional[dict] =
LOG.debug(f"Error queueing metrics request: {str(ex)}")
def initialise_tracker(account: "Account" = None):
def initialise_tracker(account=None):
global METRICS_TRACKER
if not METRICS_TRACKER:
METRICS_TRACKER = MetricsTracker()
@@ -92,7 +98,7 @@ def initialise_tracker(account: "Account" = None):
if account and account.userInfo.email:
METRICS_TRACKER.set_last_user(account.userInfo.email)
if account and account.serverInfo.url:
METRICS_TRACKER.set_last_server(account.userInfo.email)
METRICS_TRACKER.set_last_server(account.serverInfo.url)
class Singleton(type):
@@ -135,7 +141,9 @@ class MetricsTracker(metaclass=Singleton):
self.last_server = self.hash(server)
def hash(self, value: str):
return hashlib.md5(value.lower().encode("utf-8")).hexdigest().upper()
inputList = value.lower().split("://")
input = inputList[len(inputList)-1].split("/")[0].split('?')[0]
return hashlib.md5(input.encode("utf-8")).hexdigest().upper()
def _send_tracking_requests(self):
session = requests.Session()
+16
View File
@@ -0,0 +1,16 @@
from typing import Optional
from specklepy.objects import Base
class CRS(Base, speckle_type="Objects.GIS.CRS"):
"""A Coordinate Reference System stored in wkt format"""
name: Optional[str] = None
authority_id: Optional[str] = None
wkt: Optional[str] = None
units_native: Optional[str] = None
offset_x: Optional[float] = None
offset_y: Optional[float] = None
rotation: Optional[float] = None
+22
View File
@@ -0,0 +1,22 @@
"""Builtin Speckle object kit."""
from specklepy.objects.GIS.layers import (
VectorLayer,
RasterLayer,
)
from specklepy.objects.GIS.geometry import (
GisPolygonGeometry,
GisPolygonElement,
GisLineElement,
GisPointElement,
GisRasterElement,
)
from specklepy.objects.GIS.CRS import (
CRS,
)
__all__ = ["VectorLayer", "RasterLayer",
"GisPolygonGeometry", "GisPolygonElement", "GisLineElement", "GisPointElement", "GisRasterElement",
"CRS"]
+53
View File
@@ -0,0 +1,53 @@
from typing import Optional, Union, List
from specklepy.objects.geometry import Point, Line, Polyline, Circle, Arc, Polycurve, Mesh
from specklepy.objects import Base
from deprecated import deprecated
class GisPolygonGeometry(Base, speckle_type="Objects.GIS.PolygonGeometry", detachable={"displayValue"}):
"""GIS Polygon Geometry"""
boundary: Optional[Union[Polyline, Arc, Line, Circle, Polycurve]] = None
voids: Optional[List[Union[Polyline, Arc, Line, Circle, Polycurve]] ] = None
displayValue: Optional[List[Mesh]] = None
class GisPolygonElement(Base, speckle_type="Objects.GIS.PolygonElement"):
"""GIS Polygon element"""
geometry: Optional[List[GisPolygonGeometry]] = None
attributes: Optional[Base] = None
class GisLineElement(Base, speckle_type="Objects.GIS.LineElement"):
"""GIS Polyline element"""
geometry: Optional[List[Union[Polyline, Arc, Line, Circle, Polycurve]]] = None,
attributes: Optional[Base] = None,
class GisPointElement(Base, speckle_type="Objects.GIS.PointElement"):
"""GIS Point element"""
geometry: Optional[List[Point]] = None,
attributes: Optional[Base] = None,
class GisRasterElement(Base, speckle_type="Objects.GIS.RasterElement", detachable={"displayValue"}):
"""GIS Raster element"""
band_count: Optional[int] = None
band_names: Optional[List[str]] = None
x_origin: Optional[float] = None
y_origin: Optional[float] = None
x_size: Optional[int] = None
y_size: Optional[int] = None
x_resolution: Optional[float] = None
y_resolution: Optional[float] = None
noDataValue: Optional[List[float]] = None
displayValue: Optional[List[Mesh]] = None
class GisTopography(GisRasterElement, speckle_type="Objects.GIS.GisTopography", detachable={"displayValue"}):
"""GIS Raster element with 3d Topography representation"""
class GisNonGeometryElement(Base, speckle_type="Objects.GIS.NonGeometryElement"):
"""GIS Table feature"""
attributes: Optional[Base] = None
+138
View File
@@ -0,0 +1,138 @@
from typing import Any, Dict, List, Union, Optional
from specklepy.objects.base import Base
from specklepy.objects.other import Collection
from specklepy.objects.GIS.CRS import CRS
from deprecated import deprecated
@deprecated(version="2.15", reason="Use VectorLayer or RasterLayer instead")
class Layer(Base, detachable={"features"}):
"""A GIS Layer"""
def __init__(
self,
name:str=None,
crs:CRS=None,
units: str = "m",
features: Optional[List[Base]] = None,
layerType: str = "None",
geomType: str = "None",
renderer: Optional[dict[str, Any]] = None,
**kwargs
) -> None:
super().__init__(**kwargs)
self.name = name
self.crs = crs
self.units = units
self.type = layerType
self.features = features or []
self.geomType = geomType
self.renderer = renderer or {}
@deprecated(version="2.16", reason="Use VectorLayer or RasterLayer instead")
class VectorLayer(
Collection,
detachable={"elements"},
speckle_type="VectorLayer",
serialize_ignore={"features"}):
"""GIS Vector Layer"""
name: Optional[str]=None
crs: Optional[Union[CRS, Base]]=None
units: Optional[str] = None
elements: Optional[List[Base]] = None
attributes: Optional[Base] = None
geomType: Optional[str] = "None"
renderer: Optional[Dict[str, Any]] = None
collectionType = "VectorLayer"
@property
@deprecated(version="2.14", reason="Use elements")
def features(self) -> Optional[List[Base]]:
return self.elements
@features.setter
def features(self, value: Optional[List[Base]]) -> None:
self.elements = value
@deprecated(version="2.16", reason="Use VectorLayer or RasterLayer instead")
class RasterLayer(
Collection,
detachable={"elements"},
speckle_type="RasterLayer",
serialize_ignore={"features"}):
"""GIS Raster Layer"""
name: Optional[str] = None
crs: Optional[Union[CRS, Base]]=None
units: Optional[str] = None
rasterCrs: Optional[Union[CRS, Base]]=None
elements: Optional[List[Base]] = None
geomType: Optional[str] = "None"
renderer: Optional[Dict[str, Any]] = None
collectionType = "RasterLayer"
@property
@deprecated(version="2.14", reason="Use elements")
def features(self) -> Optional[List[Base]]:
return self.elements
@features.setter
def features(self, value: Optional[List[Base]]) -> None:
self.elements = value
class VectorLayer(
Collection,
detachable={"elements"},
speckle_type="Objects.GIS.VectorLayer",
serialize_ignore={"features"}):
"""GIS Vector Layer"""
name: Optional[str]=None
crs: Optional[Union[CRS, Base]]=None
units: Optional[str] = None
elements: Optional[List[Base]] = None
attributes: Optional[Base] = None
geomType: Optional[str] = "None"
renderer: Optional[Dict[str, Any]] = None
collectionType = "VectorLayer"
@property
@deprecated(version="2.14", reason="Use elements")
def features(self) -> Optional[List[Base]]:
return self.elements
@features.setter
def features(self, value: Optional[List[Base]]) -> None:
self.elements = value
class RasterLayer(
Collection,
detachable={"elements"},
speckle_type="Objects.GIS.RasterLayer",
serialize_ignore={"features"}):
"""GIS Raster Layer"""
name: Optional[str] = None
crs: Optional[Union[CRS, Base]]=None
units: Optional[str] = None
rasterCrs: Optional[Union[CRS, Base]]=None
elements: Optional[List[Base]] = None
geomType: Optional[str] = "None"
renderer: Optional[Dict[str, Any]] = None
collectionType = "RasterLayer"
@property
@deprecated(version="2.14", reason="Use elements")
def features(self) -> Optional[List[Base]]:
return self.elements
@features.setter
def features(self, value: Optional[List[Base]]) -> None:
self.elements = value
+2 -1
View File
@@ -1,5 +1,6 @@
"""Builtin Speckle object kit."""
from specklepy.objects import encoding, geometry, other, primitive, structural, units
from specklepy.objects.base import Base
__all__ = ["Base"]
__all__ = ["Base", "encoding", "geometry", "other", "units", "structural", "primitive"]
+231 -81
View File
@@ -1,21 +1,25 @@
import contextlib
from enum import Enum
from inspect import isclass
from typing import (
Any,
ClassVar,
Dict,
ForwardRef,
List,
Optional,
Union,
Set,
Tuple,
Type,
Union,
get_type_hints,
)
import contextlib
from enum import EnumMeta
from warnings import warn
from stringcase import pascalcase
from specklepy.logging.exceptions import SpeckleException, SpeckleInvalidUnitException
from specklepy.objects.units import get_units_from_string, Units
from specklepy.objects.units import Units, get_units_from_string
from specklepy.transports.memory import MemoryTransport
PRIMITIVES = (int, float, str, bool)
@@ -90,7 +94,9 @@ class _RegisteringBase:
"""
speckle_type: ClassVar[str]
_type_registry: ClassVar[Dict[str, "Base"]] = {}
_speckle_type_override: ClassVar[Optional[str]] = None
_speckle_namespace: ClassVar[Optional[str]] = None
_type_registry: ClassVar[Dict[str, Type["Base"]]] = {}
_attr_types: ClassVar[Dict[str, Type]] = {}
# dict of chunkable props and their max chunk size
_chunkable: Dict[str, int] = {}
@@ -98,22 +104,61 @@ class _RegisteringBase:
_detachable: Set[str] = set() # list of defined detachable props
_serialize_ignore: Set[str] = set()
class Config:
validate_assignment = True
@classmethod
def get_registered_type(cls, speckle_type: str) -> Optional[Type["Base"]]:
"""Get the registered type from the protected mapping via the `speckle_type`"""
for full_name in reversed(speckle_type.split(":")):
maybe_type = cls._type_registry.get(full_name, None)
if maybe_type:
return maybe_type
return None
@classmethod
def get_registered_type(
cls, speckle_type: str
) -> Union["Base", Type["Base"], None]:
"""Get the registered type from the protected mapping via the `speckle_type`"""
return cls._type_registry.get(speckle_type, None)
def _determine_speckle_type(cls) -> str:
"""
This method brings the speckle_type construction in par with peckle-sharp/Core.
The implementation differs, because in Core the basis of the speckle_type if
type.FullName, which includes the dotnet namespace name too.
Copying that behavior is hard in python, where the concept of namespaces
means something entirely different.
So we enabled a speckle_type override mechanism, that enables
"""
base_name = "Base"
if cls.__name__ == base_name:
return base_name
bases = [
b._full_name()
for b in reversed(cls.mro())
if issubclass(b, Base) and b.__name__ != base_name
]
return ":".join(bases)
@classmethod
def _full_name(cls) -> str:
base_name = "Base"
if cls.__name__ == base_name:
return base_name
if cls._speckle_type_override:
return cls._speckle_type_override
# convert the module names to PascalCase to match c# namespace naming convention
# also drop specklepy from the beginning
namespace = ".".join(
pascalcase(m)
for m in filter(lambda name: name != "specklepy", cls.__module__.split("."))
)
return f"{namespace}.{cls.__name__}"
def __init_subclass__(
cls,
speckle_type: str = None,
chunkable: Dict[str, int] = None,
detachable: Set[str] = None,
serialize_ignore: Set[str] = None,
speckle_type: Optional[str] = None,
chunkable: Optional[Dict[str, int]] = None,
detachable: Optional[Set[str]] = None,
serialize_ignore: Optional[Set[str]] = None,
**kwargs: Dict[str, Any],
):
"""
@@ -123,14 +168,15 @@ class _RegisteringBase:
initialization. This is reused to register each subclassing type into a class
level dictionary.
"""
if speckle_type in cls._type_registry:
cls._speckle_type_override = speckle_type
cls.speckle_type = cls._determine_speckle_type()
if cls._full_name() in cls._type_registry:
raise ValueError(
f"The speckle_type: {speckle_type} is already registered for type: "
f"{cls._type_registry[speckle_type].__name__}. "
f"Please choose a different type name."
f"{cls._type_registry[cls._full_name()].__name__}. "
"Please choose a different type name."
)
cls.speckle_type = speckle_type or cls.__name__
cls._type_registry[cls.speckle_type] = cls # type: ignore
cls._type_registry[cls._full_name()] = cls # type: ignore
try:
cls._attr_types = get_type_hints(cls)
except Exception:
@@ -145,11 +191,138 @@ class _RegisteringBase:
super().__init_subclass__(**kwargs)
# T = TypeVar("T")
# how i wish the code below would be correct, but we're also parsing into floats
# and converting into strings if the original type is string, but the value isn't
# def _validate_type(t: type, value: T) -> Tuple[bool, T]:
def _validate_type(t: Optional[type], value: Any) -> Tuple[bool, Any]:
# this should be reworked. Its only ok to return null for Optionals...
# if t is None and value is None:
if value is None:
return True, value
# after fixing the None t above, this should be
# if t is Any:
# if t is None:
if t is None or t is Any:
return True, value
if isclass(t) and issubclass(t, Enum):
if isinstance(value, t):
return True, value
if value in t._value2member_map_:
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:
# below is what in nicer for >= py38
# t_1, t_2 = get_args(t)
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():
valid_key, _ = _validate_type(t_key, dict_key)
valid_value, _ = _validate_type(t_value, dict_value)
if valid_key and valid_value:
return True, value
return False, value
if origin is list:
if not isinstance(value, list):
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
return False, value
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
values = []
for t_item, v_item in zip(args, value):
item_valid, item_value = _validate_type(t_item, v_item)
if not item_valid:
return False, value
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, TypeError):
if t is float and value is not None:
return True, float(value)
# TODO: dafuq, i had to add this not list check
# but it would also fail for objects and other complex values
if t is str and value and not isinstance(value, list):
return True, str(value)
return False, value
class Base(_RegisteringBase):
id: Union[str, None] = None
totalChildrenCount: Union[int, None] = None
applicationId: Union[str, None] = None
_units: Union[Units, None] = None
_units: Union[None, str] = None
def __init__(self, **kwargs) -> None:
super().__init__()
@@ -217,11 +390,14 @@ class Base(_RegisteringBase):
@classmethod
def update_forward_refs(cls) -> None:
"""
Attempts to populate the internal defined types dict for type checking sometime after defining the class.
This is already done when defining the class, but can be called again if references to undefined types were
Attempts to populate the internal defined types dict for type checking
sometime after defining the class.
This is already done when defining the class, but can be called
again if references to undefined types were
included.
See `objects.geometry` for an example of how this is used with the Brep class definitions
See `objects.geometry` for an example of how this is used with
the Brep class definitions.
"""
try:
cls._attr_types = get_type_hints(cls)
@@ -242,55 +418,27 @@ class Base(_RegisteringBase):
"Invalid Name: Base member names cannot contain characters '.' or '/'",
)
def _type_check(self, name: str, value: Any):
def _type_check(self, name: str, value: Any) -> Any:
"""
Lightweight type checking of values before setting them
NOTE: Does not check subscripted types within generics as the performance hit of checking
each item within a given collection isn't worth it. Eg if you have a type Dict[str, float],
NOTE: Does not check subscripted types within generics as the performance hit
of checking each item within a given collection isn't worth it.
Eg if you have a type Dict[str, float],
we will only check if the value you're trying to set is a dict.
"""
types = getattr(self, "_attr_types", {})
t = types.get(name, None)
if t is None or t is Any:
return value
valid, checked_value = _validate_type(t, value)
if value is None:
return None
if isinstance(t, EnumMeta) and (value in t._value2member_map_):
return t(value)
if t.__module__ == "typing":
origin = getattr(t, "__origin__")
t = (
tuple(getattr(sub_t, "__origin__", sub_t) for sub_t in t.__args__)
if origin is Union
else origin
)
if not isinstance(t, (type, tuple)):
warn(
f"Unrecognised type '{t}' provided for attribute '{name}'. Type will not been validated."
)
return value
if isinstance(value, t):
return value
# to be friendly, we'll parse ints and strs into floats, but not the other way around
# (to avoid unexpected rounding)
if isinstance(t, tuple):
t = t[0]
with contextlib.suppress(ValueError):
if t is float:
return float(value)
if t is str and value:
return str(value)
if valid:
return checked_value
raise SpeckleException(
f"Cannot set '{self.__class__.__name__}.{name}': it expects type '{t.__name__}', but received type '{type(value).__name__}'"
f"Cannot set '{self.__class__.__name__}.{name}':"
f"it expects type '{str(t)}',"
f"but received type '{type(value).__name__}'"
)
def add_chunkable_attrs(self, **kwargs: int) -> None:
@@ -298,7 +446,8 @@ class Base(_RegisteringBase):
Mark defined attributes as chunkable for serialisation
Arguments:
kwargs {int} -- the name of the attribute as the keyword and the chunk size as the arg
kwargs {int} -- the name of the attribute as the keyword
and the chunk size as the arg
"""
chunkable = {k: v for k, v in kwargs.items() if isinstance(v, int)}
self._chunkable = dict(self._chunkable, **chunkable)
@@ -308,28 +457,25 @@ class Base(_RegisteringBase):
Mark defined attributes as detachable for serialisation
Arguments:
names {Set[str]} -- the names of the attributes to detach as a set of strings
names {Set[str]} -- the names of the attributes to detach as a set of string
"""
self._detachable = self._detachable.union(names)
@property
def units(self) -> Union[str, None]:
if self._units:
return self._units.value
return None
return self._units
@units.setter
def units(self, value: Union[str, Units, None]):
if value == None:
units = value
"""While this property accepts any string value, geometry expects units to be specific strings (see Units enum)"""
if isinstance(value, str) or value is None:
self._units = value
elif isinstance(value, Units):
units: Units = value
self._units = value.value
else:
units = get_units_from_string(value)
self._units = units
# except SpeckleInvalidUnitException as ex:
# warn(f"Units are reset to None. Reason {ex.message}")
# self._units = None
raise SpeckleInvalidUnitException(
f"Unknown type {type(value)} received for units"
)
def get_member_names(self) -> List[str]:
"""Get all of the property names on this object, dynamic or not"""
@@ -359,13 +505,17 @@ class Base(_RegisteringBase):
def get_id(self, decompose: bool = False) -> str:
"""
Gets the id (a unique hash) of this object. This method fully serializes the object which,
in the case of large objects (with many sub-objects), has a tangible cost. Avoid using it!
Gets the id (a unique hash) of this object.
This method fully serializes the object which,
in the case of large objects (with many sub-objects), has a tangible cost.
Avoid using it!
Note: the hash of a decomposed object differs from that of a non-decomposed object
Note: the hash of a decomposed object differs from that of a
non-decomposed object
Arguments:
decompose {bool} -- if True, will decompose the object in the process of hashing it
decompose {bool} -- if True, will decompose the object in
the process of hashing it
Returns:
str -- the hash (id) of the fully serialized object
+3 -3
View File
@@ -1,5 +1,5 @@
from enum import Enum
from typing import Any, Callable, List, Type, Dict
from typing import Any, Callable, Dict, List, Optional, Type
from specklepy.logging.exceptions import SpeckleException
from specklepy.objects.base import Base
@@ -43,7 +43,7 @@ def curve_from_list(args: List[float]):
class ObjectArray:
def __init__(self, data: list = None) -> None:
def __init__(self, data: Optional[list] = None) -> None:
self.data = data or []
@classmethod
@@ -68,7 +68,7 @@ class ObjectArray:
def decode_data(
data: List[Any], decoder: Callable[[List[Any]], Base], **kwargs: Dict[str, Any]
) -> List[Base]:
bases = []
bases: List[Base] = []
if not data:
return bases
index = 0
+13 -12
View File
@@ -1,5 +1,6 @@
from enum import Enum
from typing import List
from typing import List, Optional
from specklepy.objects.geometry import Point
from .base import Base
@@ -16,8 +17,8 @@ DETACHABLE = {"detach_this", "origin", "detached_list"}
class FakeGeo(Base, chunkable={"dots": 50}, detachable={"pointslist"}):
pointslist: List[Base] = None
dots: List[int] = None
pointslist: Optional[List[Base]] = None
dots: Optional[List[int]] = None
class FakeDirection(Enum):
@@ -28,15 +29,15 @@ class FakeDirection(Enum):
class FakeMesh(FakeGeo, chunkable=CHUNKABLE_PROPS, detachable=DETACHABLE):
vertices: List[float] = None
faces: List[int] = None
colors: List[int] = None
textureCoordinates: List[float] = None
cardinal_dir: FakeDirection = None
test_bases: List[Base] = None
detach_this: Base = None
detached_list: List[Base] = None
_origin: Point = None
vertices: Optional[List[float]] = None
faces: Optional[List[int]] = None
colors: Optional[List[int]] = None
textureCoordinates: Optional[List[float]] = None
cardinal_dir: Optional[FakeDirection] = None
test_bases: Optional[List[Base]] = None
detach_this: Optional[Base] = None
detached_list: Optional[List[Base]] = None
_origin: Optional[Point] = None
# def __init__(self, **kwargs) -> None:
# super(FakeMesh, self).__init__(**kwargs)
+215 -158
View File
@@ -1,39 +1,31 @@
from enum import Enum
from typing import Any, List, Optional
from .base import Base
from .encoding import CurveArray, CurveTypeEncoding, ObjectArray
from .units import get_encoding_from_units, get_units_from_encoding
from specklepy.objects.base import Base
from specklepy.objects.encoding import CurveArray, CurveTypeEncoding, ObjectArray
from specklepy.objects.primitive import Interval
from specklepy.objects.units import get_encoding_from_units, get_units_from_encoding
GEOMETRY = "Objects.Geometry."
class Interval(Base, speckle_type="Objects.Primitive.Interval"):
start: float = 0.0
end: float = 0.0
def length(self):
return abs(self.start - self.end)
@classmethod
def from_list(cls, args: List[Any]) -> "Interval":
return cls(start=args[0], end=args[1])
def to_list(self) -> List[Any]:
return [self.start, self.end]
class Point(Base, speckle_type=GEOMETRY + "Point"):
x: float = 0.0
y: float = 0.0
z: float = 0.0
def __repr__(self) -> str:
return f"{self.__class__.__name__}(x: {self.x}, y: {self.y}, z: {self.z}, id: {self.id}, speckle_type: {self.speckle_type})"
return (
f"{self.__class__.__name__}(x: {self.x}, y: {self.y}, z: {self.z}, id:"
f" {self.id}, speckle_type: {self.speckle_type})"
)
@classmethod
def from_list(cls, args: List[float]) -> "Point":
"""Create a new Point from a list of three floats representing the x, y, and z coordinates"""
"""
Create a new Point from a list of three floats
representing the x, y, and z coordinates
"""
return cls(x=args[0], y=args[1], z=args[2])
def to_list(self) -> List[Any]:
@@ -47,12 +39,50 @@ class Point(Base, speckle_type=GEOMETRY + "Point"):
return pt
class Vector(Point, speckle_type=GEOMETRY + "Vector"):
pass
class Pointcloud(
Base,
speckle_type=GEOMETRY + "Pointcloud",
chunkable={"points": 31250, "colors": 62500, "sizes": 62500},
):
points: Optional[List[float]] = None
colors: Optional[List[int]] = None
sizes: Optional[List[float]] = None
bbox: Optional["Box"] = None
class Vector(Base, speckle_type=GEOMETRY + "Vector"):
x: float = 0.0
y: float = 0.0
z: float = 0.0
applicationId: Optional[str] = None
def __repr__(self) -> str:
return (
f"{self.__class__.__name__} "
"(x: {self.x}, y: {self.y}, z: {self.z}, id: {self.id}, "
"speckle_type: {self.speckle_type})"
)
@classmethod
def from_list(cls, args: List[float]) -> "Vector":
"""
Create from a list of three floats representing the x, y, and z coordinates.
"""
return cls(x=args[0], y=args[1], z=args[2])
def to_list(self) -> List[float]:
return [self.x, self.y, self.z]
@classmethod
def from_coords(cls, x: float = 0.0, y: float = 0.0, z: float = 0.0) -> "Vector":
"""Create a new Point from x, y, and z values"""
v = Vector()
v.x, v.y, v.z = x, y, z
return v
class ControlPoint(Point, speckle_type=GEOMETRY + "ControlPoint"):
weight: float = None
weight: Optional[float] = None
class Plane(Base, speckle_type=GEOMETRY + "Plane"):
@@ -83,19 +113,19 @@ class Plane(Base, speckle_type=GEOMETRY + "Plane"):
class Box(Base, speckle_type=GEOMETRY + "Box"):
basePlane: Plane = Plane()
xSize: Interval = Interval()
ySize: Interval = Interval()
zSize: Interval = Interval()
xSize: Interval = Interval()
area: float = None
volume: float = None
area: Optional[float] = None
volume: Optional[float] = None
class Line(Base, speckle_type=GEOMETRY + "Line"):
start: Point = Point()
end: Point = None
domain: Interval = None
bbox: Box = None
length: float = None
end: Optional[Point] = None
domain: Optional[Interval] = None
bbox: Optional[Box] = None
length: Optional[float] = None
@classmethod
def from_list(cls, args: List[Any]) -> "Line":
@@ -118,18 +148,18 @@ class Line(Base, speckle_type=GEOMETRY + "Line"):
class Arc(Base, speckle_type=GEOMETRY + "Arc"):
radius: float = None
startAngle: float = None
endAngle: float = None
angleRadians: float = None
plane: Plane = None
domain: Interval = None
startPoint: Point = None
midPoint: Point = None
endPoint: Point = None
bbox: Box = None
area: float = None
length: float = None
radius: Optional[float] = None
startAngle: Optional[float] = None
endAngle: Optional[float] = None
angleRadians: Optional[float] = None
plane: Optional[Plane] = None
domain: Optional[Interval] = None
startPoint: Optional[Point] = None
midPoint: Optional[Point] = None
endPoint: Optional[Point] = None
bbox: Optional[Box] = None
area: Optional[float] = None
length: Optional[float] = None
@classmethod
def from_list(cls, args: List[Any]) -> "Arc":
@@ -163,12 +193,12 @@ class Arc(Base, speckle_type=GEOMETRY + "Arc"):
class Circle(Base, speckle_type=GEOMETRY + "Circle"):
radius: float = None
plane: Plane = None
domain: Interval = None
bbox: Box = None
area: float = None
length: float = None
radius: Optional[float] = None
plane: Optional[Plane] = None
domain: Optional[Interval] = None
bbox: Optional[Box] = None
area: Optional[float] = None
length: Optional[float] = None
@classmethod
def from_list(cls, args: List[Any]) -> "Circle":
@@ -190,14 +220,14 @@ class Circle(Base, speckle_type=GEOMETRY + "Circle"):
class Ellipse(Base, speckle_type=GEOMETRY + "Ellipse"):
firstRadius: float = None
secondRadius: float = None
plane: Plane = None
domain: Interval = None
trimDomain: Interval = None
bbox: Box = None
area: float = None
length: float = None
firstRadius: Optional[float] = None
secondRadius: Optional[float] = None
plane: Optional[Plane] = None
domain: Optional[Interval] = None
trimDomain: Optional[Interval] = None
bbox: Optional[Box] = None
area: Optional[float] = None
length: Optional[float] = None
@classmethod
def from_list(cls, args: List[Any]) -> "Ellipse":
@@ -221,12 +251,12 @@ class Ellipse(Base, speckle_type=GEOMETRY + "Ellipse"):
class Polyline(Base, speckle_type=GEOMETRY + "Polyline", chunkable={"value": 20000}):
value: List[float] = None
closed: bool = None
domain: Interval = None
bbox: Box = None
area: float = None
length: float = None
value: Optional[List[float]] = None
closed: Optional[bool] = None
domain: Optional[Interval] = None
bbox: Optional[Box] = None
area: Optional[float] = None
length: Optional[float] = None
@classmethod
def from_points(cls, points: List[Point]):
@@ -272,23 +302,50 @@ class Polyline(Base, speckle_type=GEOMETRY + "Polyline", chunkable={"value": 200
]
class SpiralType(Enum):
Biquadratic = (0,)
BiquadraticParabola = (1,)
Bloss = (2,)
Clothoid = (3,)
Cosine = (4,)
Cubic = (5,)
CubicParabola = (6,)
Radioid = (7,)
Sinusoid = (8,)
Unknown = 9
class Spiral(Base, speckle_type=GEOMETRY + "Spiral", detachable={"displayValue"}):
startPoint: Optional[Point] = None
endPoint: Optional[Point]
plane: Optional[Plane]
turns: Optional[int]
pitchAxis: Optional[Vector] = Vector()
pitch: float = 0
spiralType: Optional[SpiralType] = None
displayValue: Optional[Polyline] = None
bbox: Optional[Box] = None
length: Optional[float] = None
domain: Optional[Interval] = None
class Curve(
Base,
speckle_type=GEOMETRY + "Curve",
chunkable={"points": 20000, "weights": 20000, "knots": 20000},
):
degree: int = None
periodic: bool = None
rational: bool = None
points: List[float] = None
weights: List[float] = None
knots: List[float] = None
domain: Interval = None
displayValue: Polyline = None
closed: bool = None
bbox: Box = None
area: float = None
length: float = None
degree: Optional[int] = None
periodic: Optional[bool] = None
rational: Optional[bool] = None
points: Optional[List[float]] = None
weights: Optional[List[float]] = None
knots: Optional[List[float]] = None
domain: Optional[Interval] = None
displayValue: Optional[Polyline] = None
closed: Optional[bool] = None
bbox: Optional[Box] = None
area: Optional[float] = None
length: Optional[float] = None
def as_points(self) -> List[Point]:
"""Converts the `value` attribute to a list of Points"""
@@ -345,12 +402,12 @@ class Curve(
class Polycurve(Base, speckle_type=GEOMETRY + "Polycurve"):
segments: List[Base] = None
domain: Interval = None
closed: bool = None
bbox: Box = None
area: float = None
length: float = None
segments: Optional[List[Base]] = None
domain: Optional[Interval] = None
closed: Optional[bool] = None
bbox: Optional[Box] = None
area: Optional[float] = None
length: Optional[float] = None
@classmethod
def from_list(cls, args: List[Any]) -> "Polycurve":
@@ -375,17 +432,17 @@ class Polycurve(Base, speckle_type=GEOMETRY + "Polycurve"):
class Extrusion(Base, speckle_type=GEOMETRY + "Extrusion"):
capped: bool = None
profile: Base = None
pathStart: Point = None
pathEnd: Point = None
pathCurve: Base = None
pathTangent: Base = None
profiles: List[Base] = None
length: float = None
area: float = None
volume: float = None
bbox: Box = None
capped: Optional[bool] = None
profile: Optional[Base] = None
pathStart: Optional[Point] = None
pathEnd: Optional[Point] = None
pathCurve: Optional[Base] = None
pathTangent: Optional[Base] = None
profiles: Optional[List[Base]] = None
length: Optional[float] = None
area: Optional[float] = None
volume: Optional[float] = None
bbox: Optional[Box] = None
class Mesh(
@@ -398,21 +455,21 @@ class Mesh(
"textureCoordinates": 2000,
},
):
vertices: List[float] = None
faces: List[int] = None
colors: List[int] = None
textureCoordinates: List[float] = None
bbox: Box = None
area: float = None
volume: float = None
vertices: Optional[List[float]] = None
faces: Optional[List[int]] = None
colors: Optional[List[int]] = None
textureCoordinates: Optional[List[float]] = None
bbox: Optional[Box] = None
area: Optional[float] = None
volume: Optional[float] = None
@classmethod
def create(
cls,
vertices: List[float],
faces: List[int],
colors: List[int] = None,
texture_coordinates: List[float] = None,
colors: Optional[List[int]] = None,
texture_coordinates: Optional[List[float]] = None,
) -> "Mesh":
"""
Create a new Mesh from lists representing its vertices, faces,
@@ -430,20 +487,20 @@ class Mesh(
class Surface(Base, speckle_type=GEOMETRY + "Surface"):
degreeU: int = None
degreeV: int = None
rational: bool = None
area: float = None
pointData: List[float] = None
countU: int = None
countV: int = None
bbox: Box = None
closedU: bool = None
closedV: bool = None
domainU: Interval = None
domainV: Interval = None
knotsU: List[float] = None
knotsV: List[float] = None
degreeU: Optional[int] = None
degreeV: Optional[int] = None
rational: Optional[bool] = None
area: Optional[float] = None
pointData: Optional[List[float]] = None
countU: Optional[int] = None
countV: Optional[int] = None
bbox: Optional[Box] = None
closedU: Optional[bool] = None
closedV: Optional[bool] = None
domainU: Optional[Interval] = None
domainV: Optional[Interval] = None
knotsU: Optional[List[float]] = None
knotsV: Optional[List[float]] = None
@classmethod
def from_list(cls, args: List[Any]) -> "Surface":
@@ -493,11 +550,11 @@ class Surface(Base, speckle_type=GEOMETRY + "Surface"):
class BrepFace(Base, speckle_type=GEOMETRY + "BrepFace"):
_Brep: "Brep" = None
SurfaceIndex: int = None
OuterLoopIndex: int = None
OrientationReversed: bool = None
LoopIndices: List[int] = None
_Brep: Optional["Brep"] = None
SurfaceIndex: Optional[int] = None
OuterLoopIndex: Optional[int] = None
OrientationReversed: Optional[bool] = None
LoopIndices: Optional[List[int]] = None
@property
def _outer_loop(self):
@@ -533,13 +590,13 @@ class BrepFace(Base, speckle_type=GEOMETRY + "BrepFace"):
class BrepEdge(Base, speckle_type=GEOMETRY + "BrepEdge"):
_Brep: "Brep" = None
Curve3dIndex: int = None
TrimIndices: List[int] = None
StartIndex: int = None
EndIndex: int = None
ProxyCurveIsReversed: bool = None
Domain: Interval = None
_Brep: Optional["Brep"] = None
Curve3dIndex: Optional[int] = None
TrimIndices: Optional[List[int]] = None
StartIndex: Optional[int] = None
EndIndex: Optional[int] = None
ProxyCurveIsReversed: Optional[bool] = None
Domain: Optional[Interval] = None
@property
def _start_vertex(self):
@@ -600,10 +657,10 @@ class BrepLoopType(int, Enum):
class BrepLoop(Base, speckle_type=GEOMETRY + "BrepLoop"):
_Brep: "Brep" = None
FaceIndex: int = None
TrimIndices: List[int] = None
Type: BrepLoopType = None
_Brep: Optional["Brep"] = None
FaceIndex: Optional[Optional[int]] = None
TrimIndices: Optional[List[int]] = None
Type: Optional[BrepLoopType] = None
@property
def _face(self):
@@ -644,17 +701,17 @@ class BrepTrimType(int, Enum):
class BrepTrim(Base, speckle_type=GEOMETRY + "BrepTrim"):
_Brep: "Brep" = None
EdgeIndex: int = None
StartIndex: int = None
EndIndex: int = None
FaceIndex: int = None
LoopIndex: int = None
CurveIndex: int = None
IsoStatus: int = None
TrimType: BrepTrimType = None
IsReversed: bool = None
Domain: Interval = None
_Brep: Optional["Brep"] = None
EdgeIndex: Optional[int] = None
StartIndex: Optional[int] = None
EndIndex: Optional[int] = None
FaceIndex: Optional[int] = None
LoopIndex: Optional[int] = None
CurveIndex: Optional[int] = None
IsoStatus: Optional[int] = None
TrimType: Optional[BrepTrimType] = None
IsReversed: Optional[bool] = None
Domain: Optional[Interval] = None
@property
def _face(self):
@@ -731,21 +788,21 @@ class Brep(
"Faces",
},
):
provenance: str = None
bbox: Box = None
area: float = None
volume: float = None
_displayValue: List[Mesh] = None
Surfaces: List[Surface] = None
Curve3D: List[Base] = None
Curve2D: List[Base] = None
Vertices: List[Point] = None
Edges: List[BrepEdge] = None
Loops: List[BrepLoop] = None
Faces: List[BrepFace] = None
Trims: List[BrepTrim] = None
IsClosed: bool = None
Orientation: int = None
provenance: Optional[str] = None
bbox: Optional[Box] = None
area: Optional[float] = None
volume: Optional[float] = None
_displayValue: Optional[List[Mesh]] = None
Surfaces: Optional[List[Surface]] = None
Curve3D: Optional[List[Base]] = None
Curve2D: Optional[List[Base]] = None
Vertices: Optional[List[Point]] = None
Edges: Optional[List[BrepEdge]] = None
Loops: Optional[List[BrepLoop]] = None
Faces: Optional[List[BrepFace]] = None
Trims: Optional[List[BrepTrim]] = None
IsClosed: Optional[bool] = None
Orientation: Optional[int] = None
def _inject_self_into_children(self, children: Optional[List[Base]]) -> List[Base]:
if children is None:
@@ -841,7 +898,7 @@ class Brep(
def VerticesValue(self) -> List[Point]:
if self.Vertices is None:
return None
encoded_unit = get_encoding_from_units(self.Vertices[0]._units)
encoded_unit = get_encoding_from_units(self.Vertices[0].units)
values = [encoded_unit]
for vertex in self.Vertices:
values.extend(vertex.to_list())
@@ -856,7 +913,7 @@ class Brep(
for i in range(0, len(value), 3):
vertex = Point.from_list(value[i : i + 3])
vertex._units = units
vertex.units = units
vertices.append(vertex)
self.Vertices = vertices
@@ -0,0 +1,83 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Any, Collection, Dict, Generic, Iterable, List, Optional, Tuple, TypeVar
from attrs import define
from specklepy.objects.base import Base
ROOT: str = "__Root"
T = TypeVar('T')
PARENT_INFO = Tuple[Optional[str], str]
@define(slots=True)
class CommitObjectBuilder(ABC, Generic[T]):
converted: Dict[str, Base]
_parent_infos: Dict[str, Collection[PARENT_INFO]]
def __init__(self) -> None:
self.converted = {}
self._parent_infos = {}
@abstractmethod
def include_object(self, conversion_result: Base, native_object: T) -> None:
pass
def build_commit_object(self, root_commit_object: Base) -> None:
self.apply_relationships(self.converted.values(), root_commit_object)
def set_relationship(self, app_id: Optional[str], *parent_info : PARENT_INFO) -> None:
if not app_id:
return
self._parent_infos[app_id] = parent_info
def apply_relationships(self, to_add: Iterable[Base], root_commit_object: Base) -> None:
for c in to_add:
try:
self.apply_relationship(c, root_commit_object)
except Exception as ex:
print(f"Failed to add object {type(c)} to commit object: {ex}")
def apply_relationship(self, current: Base, root_commit_object: Base):
if not current.applicationId: raise Exception(f"Expected applicationId to have been set")
parents = self._parent_infos[current.applicationId]
for (parent_id, prop_name) in parents:
if not parent_id: continue
parent: Optional[Base]
if parent_id == ROOT:
parent = root_commit_object
else:
parent = self.converted[parent_id] if parent_id in self.converted else None
if not parent: continue
try:
elements = get_detached_prop(parent, prop_name)
if not isinstance(elements, list):
elements = []
set_detached_prop(parent, prop_name, elements)
elements.append(current)
return
except Exception as ex:
# A parent was found, but it was invalid (Likely because of a type mismatch on a `elements` property)
print(f"Failed to add object {type(current)} to a converted parent; {ex}")
raise Exception(f"Could not find a valid parent for object of type {type(current)}. Checked {len(parents)} potential parent, and non were converted!")
def get_detached_prop(speckle_object: Base, prop_name: str) -> Optional[Any]:
detached_prop_name = get_detached_prop_name(speckle_object, prop_name)
return getattr(speckle_object, detached_prop_name, None)
def set_detached_prop(speckle_object: Base, prop_name: str, value: Optional[Any]) -> None:
detached_prop_name = get_detached_prop_name(speckle_object, prop_name)
setattr(speckle_object, detached_prop_name, value)
def get_detached_prop_name(speckle_object: Base, prop_name: str) -> str:
return prop_name if hasattr(speckle_object, prop_name) else f"@{prop_name}"
@@ -0,0 +1,123 @@
from typing import Any, Callable, Collection, Iterable, Iterator, List, Optional, Set
from attrs import define
from typing_extensions import Protocol, final
from specklepy.objects import Base
class ITraversalRule(Protocol):
def get_members_to_traverse(self, o: Base) -> Set[str]:
"""Get the members to traverse."""
pass
def does_rule_hold(self, o: Base) -> bool:
"""Make sure the rule still holds."""
pass
@final
@define(slots=True, frozen=True)
class DefaultRule:
def get_members_to_traverse(self, _) -> Set[str]:
return set()
def does_rule_hold(self, _) -> bool:
return True
# we're creating a local protected "singleton"
_default_rule = DefaultRule()
@final
@define(slots=True, frozen=True)
class TraversalContext:
current: Base
member_name: Optional[str] = None
parent: Optional["TraversalContext"] = None
@final
@define(slots=True, frozen=True)
class GraphTraversal:
_rules: List[ITraversalRule]
def traverse(self, root: Base) -> Iterator[TraversalContext]:
stack: List[TraversalContext] = []
stack.append(TraversalContext(root))
while len(stack) > 0:
head = stack.pop()
yield head
current = head.current
active_rule = self._get_active_rule_or_default_rule(current)
members_to_traverse = active_rule.get_members_to_traverse(current)
for child_prop in members_to_traverse:
try:
if child_prop in {"speckle_type", "units", "applicationId"}: continue #debug: to avoid noisy exceptions, explicitly avoid checking ones we know will fail, this is not exhaustive
if getattr(current, child_prop, None):
value = current[child_prop]
self._traverse_member_to_stack(
stack, value, child_prop, head
)
except KeyError as ex:
# Unset application ids, and class variables like SpeckleType will throw when __getitem__ is called
pass
@staticmethod
def _traverse_member_to_stack(
stack: List[TraversalContext],
value: Any,
member_name: Optional[str] = None,
parent: Optional[TraversalContext] = None,
):
if isinstance(value, Base):
stack.append(TraversalContext(value, member_name, parent))
elif isinstance(value, list):
for obj in value:
GraphTraversal._traverse_member_to_stack(stack, obj, member_name, parent)
elif isinstance(value, dict):
for obj in value.values():
GraphTraversal._traverse_member_to_stack(stack, obj, member_name, parent)
@staticmethod
def traverse_member(value: Optional[Any]) -> Iterator[Base]:
if isinstance(value, Base):
yield value
elif isinstance(value, list):
for obj in value:
for o in GraphTraversal.traverse_member(obj):
yield o
elif isinstance(value, dict):
for obj in value.values():
for o in GraphTraversal.traverse_member(obj):
yield o
def _get_active_rule_or_default_rule(self, o: Base) -> ITraversalRule:
return self._get_active_rule(o) or _default_rule
def _get_active_rule(self, o: Base) -> Optional[ITraversalRule]:
for rule in self._rules:
if rule.does_rule_hold(o):
return rule
return None
@final
@define(slots=True, frozen=True)
class TraversalRule:
_conditions: Collection[Callable[[Base], bool]]
_members_to_traverse: Callable[[Base], Iterable[str]]
def get_members_to_traverse(self, o: Base) -> Set[str]:
return set(self._members_to_traverse(o))
def does_rule_hold(self, o: Base) -> bool:
for condition in self._conditions:
if condition(o):
return True
return False
+111 -27
View File
@@ -1,8 +1,12 @@
from typing import Any, List
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,
@@ -24,8 +28,23 @@ IDENTITY_TRANSFORM = [
]
class Material(Base, speckle_type=OTHER + "Material"):
"""Generic class for materials containing generic parameters."""
name: Optional[str] = None
class RevitMaterial(Material, speckle_type="Objects.Other.Revit." + "RevitMaterial"):
materialCategory: Optional[str] = None
materialClass: Optional[str] = None
shininess: Optional[int] = None
smoothness: Optional[int] = None
transparency: Optional[int] = None
parameters: Optional[Base] = None
class RenderMaterial(Base, speckle_type=OTHER + "RenderMaterial"):
name: str = None
name: Optional[str] = None
opacity: float = 1
metalness: float = 0
roughness: float = 1
@@ -33,36 +52,67 @@ class RenderMaterial(Base, speckle_type=OTHER + "RenderMaterial"):
emissive: int = -16777216 # black arbg
class MaterialQuantity(Base, speckle_type=OTHER + "MaterialQuantity"):
material: Optional[Material] = None
volume: Optional[float] = None
area: Optional[float] = None
class DisplayStyle(Base, speckle_type=OTHER + "DisplayStyle"):
"""
Minimal display style class.
Developed primarily for display styles in Rhino and AutoCAD.
Rhino object attributes uses OpenNURBS definition for linetypes and lineweights.
"""
name: Optional[str] = None
color: int = -2894893 # light gray arbg
linetype: Optional[str] = None
lineweight: float = 0
class Transform(
Base,
speckle_type=OTHER + "Transform",
serialize_ignore={"translation", "scaling", "is_identity"},
serialize_ignore={"translation", "scaling", "is_identity", "value"},
):
"""The 4x4 transformation matrix
The 3x3 sub-matrix determines scaling.
The 4th column defines translation, where the last value is a divisor (usually equal to 1).
The 4th column defines translation,
where the last value is a divisor (usually equal to 1).
"""
_value: List[float] = None
_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:
raise ValueError(
f"Could not create a Transform object with the requested value. Input must be a 16 element list of numbers. Value provided: {value}"
"Could not create a Transform object with the requested value. Input"
f" must be a 16 element list of numbers. Value provided: {value}"
) from error
if len(value) != 16:
raise ValueError(
f"Could not create a Transform object: input list should be 16 floats long, but was {len(value)} long"
"Could not create a Transform object: input list should be 16 floats"
f" long, but was {len(value)} long"
)
self._value = value
@@ -79,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
@@ -127,14 +177,16 @@ class Transform(
"""Transform a list of speckle Points
Arguments:
points {List[float]} -- a flat list of floats representing points to transform
points {List[float]}
-- a flat list of floats representing points to transform
Returns:
List[float] -- a new transformed list
"""
if len(points_value) % 3 != 0:
raise ValueError(
"Cannot apply transform as the points list is malformed: expected length to be multiple of 3"
"Cannot apply transform as the points list is malformed: expected"
" length to be multiple of 3"
)
transformed = []
for i in range(0, len(points_value), 3):
@@ -171,11 +223,13 @@ class Transform(
][:3]
@classmethod
def from_list(cls, value: List[float] = None) -> "Transform":
"""Returns a Transform object from a list of 16 numbers. If no value is provided, an identity transform will be returned.
def from_list(cls, value: Optional[List[float]] = None) -> "Transform":
"""Returns a Transform object from a list of 16 numbers.
If no value is provided, an identity transform will be returned.
Arguments:
value {List[float]} -- the matrix as a flat list of 16 numbers (defaults to the identity transform)
value {List[float]} -- the matrix as a flat list of 16 numbers
(defaults to the identity transform)
Returns:
Transform -- a complete transform object
@@ -188,27 +242,57 @@ class Transform(
class BlockDefinition(
Base, speckle_type=OTHER + "BlockDefinition", detachable={"geometry"}
):
name: str = None
basePoint: Point = None
geometry: List[Base] = None
name: Optional[str] = None
basePoint: Optional[Point] = None
geometry: Optional[List[Base]] = None
class Instance(
Base, speckle_type=OTHER + "Instance", detachable={"definition"}
):
transform: Optional[Transform] = None
definition: Optional[Base] = None
class BlockInstance(
Base, speckle_type=OTHER + "BlockInstance", detachable={"blockDefinition"}
Instance, speckle_type=OTHER + "BlockInstance", serialize_ignore={"blockDefinition"}
):
blockDefinition: BlockDefinition = None
transform: Transform = None
@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"):
name: str = None
class RevitParameter(
Base, speckle_type="Objects.BuiltElements.Revit.Parameter"
):
name: Optional[str] = None
value: Any = None
applicationUnitType: str = None # eg UnitType UT_Length
applicationUnit: str = None # DisplayUnitType eg DUT_MILLIMITERS
applicationInternalName: str = (
None # BuiltInParameterName or GUID for shared parameter
)
applicationUnitType: Optional[str] = None # eg UnitType UT_Length
applicationUnit: Optional[str] = None # DisplayUnitType eg DUT_MILLIMITERS
applicationInternalName: Optional[
str
] = None # BuiltInParameterName or GUID for shared 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
+25
View File
@@ -0,0 +1,25 @@
from typing import Any, List
from specklepy.objects.base import Base
NAMESPACE = "Objects.Primitive"
class Interval(Base, speckle_type=f"{NAMESPACE}.Interval"):
start: float = 0.0
end: float = 0.0
def length(self):
return abs(self.start - self.end)
@classmethod
def from_list(cls, args: List[Any]) -> "Interval":
return cls(start=args[0], end=args[1])
def to_list(self) -> List[Any]:
return [self.start, self.end]
class Interval2d(Base, speckle_type=f"{NAMESPACE}.Interval2d"):
u: Interval
v: Interval
+112 -7
View File
@@ -1,20 +1,103 @@
"""Builtin Speckle object kit."""
from specklepy.objects.structural.analysis import *
from specklepy.objects.structural.properties import *
from specklepy.objects.structural.material import *
from specklepy.objects.structural.geometry import *
from specklepy.objects.structural.loading import *
from specklepy.objects.structural.axis import Axis
from specklepy.objects.structural.analysis import (
Model,
ModelInfo,
ModelSettings,
ModelUnits,
)
from specklepy.objects.structural.axis import (
AxisType,
Axis
)
from specklepy.objects.structural.geometry import (
Element1D,
Element2D,
Element3D,
ElementType1D,
ElementType2D,
ElementType3D,
Node,
Restraint,
)
from specklepy.objects.structural.loading import (
ActionType,
BeamLoadType,
CombinationType,
FaceLoadType,
Load,
LoadAxisType,
LoadBeam,
LoadCase,
LoadCombinations,
LoadDirection,
LoadDirection2D,
LoadFace,
LoadGravity,
LoadNode,
LoadType,
)
from specklepy.objects.structural.materials import (
Concrete,
MaterialType,
Steel,
StructuralMaterial,
Timber,
)
from specklepy.objects.structural.properties import (
BaseReferencePoint,
MemberType,
Property,
Property1D,
Property2D,
Property3D,
PropertyDamper,
PropertyMass,
PropertySpring,
PropertyType2D,
PropertyType3D,
PropertyTypeDamper,
PropertyTypeSpring,
ReferenceSurface,
ReferenceSurfaceEnum,
SectionProfile,
ShapeType,
shapeType,
)
from specklepy.objects.structural.results import (
Result,
Result1D,
Result2D,
Result3D,
ResultGlobal,
ResultNode,
ResultSet1D,
ResultSet2D,
ResultSet3D,
ResultSetAll,
ResultSetNode,
)
__all__ = [
"Element1D",
"Element2D",
"Element3D",
"ElementType1D",
"ElementType2D",
"ElementType3D",
"AxisType",
"Axis",
"Node",
"Restraint",
"Load",
"LoadType",
"ActionType",
"BeamLoadType",
"FaceLoadType",
"LoadDirection",
"LoadDirection2D",
"LoadAxisType",
"CombinationType",
"LoadBeam",
"LoadCase",
"LoadCombinations",
@@ -25,8 +108,9 @@ __all__ = [
"ModelInfo",
"ModelSettings",
"ModelUnits",
"MaterialType",
"Concrete",
"Material",
"StructuralMaterial",
"Steel",
"Timber",
"Property",
@@ -37,4 +121,25 @@ __all__ = [
"PropertyMass",
"PropertySpring",
"SectionProfile",
"MemberType",
"BaseReferencePoint",
"ReferenceSurface",
"PropertyType2D",
"PropertyType3D",
"ShapeType",
"PropertyTypeSpring",
"PropertyTypeDamper",
"ReferenceSurfaceEnum",
"shapeType",
"Result",
"Result1D",
"ResultSet1D",
"Result2D",
"ResultSet2D",
"Result3D",
"ResultSet3D",
"ResultGlobal",
"ResultSetNode",
"ResultNode",
"ResultSetAll",
]
+33 -35
View File
@@ -1,51 +1,49 @@
from typing import List
from typing import List, Optional
from ..base import Base
from ..geometry import *
from .properties import *
from specklepy.objects.base import Base
STRUCTURAL_ANALYSIS = "Objects.Structural.Analysis."
class ModelUnits(Base, speckle_type=STRUCTURAL_ANALYSIS + "ModelUnits"):
length: str = None
sections: str = None
displacements: str = None
stress: str = None
force: str = None
mass: str = None
time: str = None
temperature: str = None
velocity: str = None
acceleration: str = None
energy: str = None
angle: str = None
strain: str = None
length: Optional[str] = None
sections: Optional[str] = None
displacements: Optional[str] = None
stress: Optional[str] = None
force: Optional[str] = None
mass: Optional[str] = None
time: Optional[str] = None
temperature: Optional[str] = None
velocity: Optional[str] = None
acceleration: Optional[str] = None
energy: Optional[str] = None
angle: Optional[str] = None
strain: Optional[str] = None
class ModelSettings(Base, speckle_type=STRUCTURAL_ANALYSIS + "ModelSettings"):
modelUnits: ModelUnits = None
steelCode: str = None
concreteCode: str = None
modelUnits: Optional[ModelUnits] = None
steelCode: Optional[str] = None
concreteCode: Optional[str] = None
coincidenceTolerance: float = 0.0
class ModelInfo(Base, speckle_type=STRUCTURAL_ANALYSIS + "ModelInfo"):
name: str = None
description: str = None
projectNumber: str = None
projectName: str = None
settings: ModelSettings = None
initials: str = None
application: str = None
name: Optional[str] = None
description: Optional[str] = None
projectNumber: Optional[str] = None
projectName: Optional[str] = None
settings: Optional[ModelSettings] = None
initials: Optional[str] = None
application: Optional[str] = None
class Model(Base, speckle_type=STRUCTURAL_ANALYSIS + "Model"):
specs: ModelInfo = None
nodes: List = None
elements: List = None
loads: List = None
restraints: List = None
properties: List = None
materials: List = None
layerDescription: str = None
specs: Optional[ModelInfo] = None
nodes: Optional[List] = None
elements: Optional[List] = None
loads: Optional[List] = None
restraints: Optional[List] = None
properties: Optional[List] = None
materials: Optional[List] = None
layerDescription: Optional[str] = None
+14 -5
View File
@@ -1,8 +1,17 @@
from ..base import Base
from ..geometry import Plane
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: str = None
axisType: str = None
plane: Plane = None
name: Optional[str] = None
axisType: Optional[AxisType] = None
plane: Optional[Plane] = None
+46 -44
View File
@@ -1,10 +1,17 @@
from enum import Enum
from typing import List
from typing import List, Optional
from ..base import Base
from ..geometry import *
from .properties import *
from .axis import Axis
from specklepy.objects.base import Base
from specklepy.objects.geometry import Line, Mesh, Plane, Point, Vector
from specklepy.objects.structural.axis import Axis
from specklepy.objects.structural.properties import (
Property1D,
Property2D,
Property3D,
PropertyDamper,
PropertyMass,
PropertySpring,
)
STRUCTURAL_GEOMETRY = "Objects.Structural.Geometry"
@@ -41,68 +48,63 @@ class ElementType3D(int, Enum):
class Restraint(Base, speckle_type=STRUCTURAL_GEOMETRY + ".Restraint"):
code: str = None
code: Optional[str] = None
stiffnessX: float = 0.0
stiffnessY: float = 0.0
stiffnessZ: float = 0.0
stiffnessXX: float = 0.0
stiffnessYY: float = 0.0
stiffnessZZ: float = 0.0
units: str = None
class Node(Base, speckle_type=STRUCTURAL_GEOMETRY + ".Node"):
name: str = None
basePoint: Point = None
constraintAxis: Axis = None
restraint: Restraint = None
springProperty: PropertySpring = None
massProperty: PropertyMass = None
damperProperty: PropertyDamper = None
units: str = None
name: Optional[str] = None
basePoint: Optional[Point] = None
constraintAxis: Optional[Axis] = None
restraint: Optional[Restraint] = None
springProperty: Optional[PropertySpring] = None
massProperty: Optional[PropertyMass] = None
damperProperty: Optional[PropertyDamper] = None
class Element1D(Base, speckle_type=STRUCTURAL_GEOMETRY + ".Element1D"):
name: str = None
baseLine: Line = None
property: Property1D = None
type: ElementType1D = None
end1Releases: Restraint = None
end2Releases: Restraint = None
end1Offset: Vector = None
end2Offset: Vector = None
orientationNode: Node = None
name: Optional[str] = None
baseLine: Optional[Line] = None
property: Optional[Property1D] = None
type: Optional[ElementType1D] = None
end1Releases: Optional[Restraint] = None
end2Releases: Optional[Restraint] = None
end1Offset: Optional[Vector] = None
end2Offset: Optional[Vector] = None
orientationNode: Optional[Node] = None
orinetationAngle: float = 0.0
localAxis: Plane = None
parent: Base = None
end1Node: Node = Node
end2Node: Node = Node
topology: List = None
displayMesh: Mesh = None
units: str = None
localAxis: Optional[Plane] = None
parent: Optional[Base] = None
end1Node: Optional[Node] = None
end2Node: Optional[Node] = None
topology: Optional[List] = None
displayMesh: Optional[Mesh] = None
class Element2D(Base, speckle_type=STRUCTURAL_GEOMETRY + ".Element2D"):
name: str = None
property: Property2D = None
type: ElementType2D = None
name: Optional[str] = None
property: Optional[Property2D] = None
type: Optional[ElementType2D] = None
offset: float = 0.0
orientationAngle: float = 0.0
parent: Base = None
topology: List = None
displayMesh: Mesh = None
units: str = None
parent: Optional[Base] = None
topology: Optional[List] = None
displayMesh: Optional[Mesh] = None
class Element3D(Base, speckle_type=STRUCTURAL_GEOMETRY + ".Element3D"):
name: str = None
baseMesh: Mesh = None
property: Property3D = None
type: ElementType3D = None
name: Optional[str] = None
baseMesh: Optional[Mesh] = None
property: Optional[Property3D] = None
type: Optional[ElementType3D] = None
orientationAngle: float = 0.0
parent: Base = None
parent: Optional[Base] = None
topology: List
units: str = None
# class Storey needs ependency on built elements first
+34 -41
View File
@@ -1,14 +1,14 @@
from enum import Enum
from typing import List
from typing import List, Optional
from ..base import Base
from .geometry import *
from specklepy.objects.base import Base
from specklepy.objects.geometry import Vector
from specklepy.objects.structural.axis import Axis
STRUCTURAL_LOADING = "Objects.Structural.Loading."
class LoadType(int, Enum):
none = 0
Dead = 1
SuperDead = 2
@@ -31,7 +31,6 @@ class LoadType(int, Enum):
class ActionType(int, Enum):
none = 0
Permanent = 1
Variable = 2
@@ -39,7 +38,6 @@ class ActionType(int, Enum):
class BeamLoadType(int, Enum):
Point = 0
Uniform = 1
Linear = 2
@@ -48,21 +46,18 @@ class BeamLoadType(int, Enum):
class FaceLoadType(int, Enum):
Constant = 0
Variable = 1
Point = 2
class LoadDirection2D(int, Enum):
X = 0
Y = 1
Z = 2
class LoadDirection(int, Enum):
X = 0
Y = 1
Z = 2
@@ -80,7 +75,6 @@ class LoadAxisType(int, Enum):
class CombinationType(int, Enum):
LinearAdd = 0
Envelope = 1
AbsoluteAdd = 2
@@ -89,56 +83,55 @@ class CombinationType(int, Enum):
class LoadCase(Base, speckle_type=STRUCTURAL_LOADING + "LoadCase"):
name: str = None
loadType: LoadType = None
group: str = None
actionType: ActionType = None
description: str = None
name: Optional[str] = None
loadType: Optional[LoadType] = None
group: Optional[str] = None
actionType: Optional[ActionType] = None
description: Optional[str] = None
class Load(Base, speckle_type=STRUCTURAL_LOADING + "Load"):
name: str = None
units: str = None
loadCase: LoadCase = None
name: Optional[str] = None
loadCase: Optional[LoadCase] = None
class LoadBeam(Load, speckle_type=STRUCTURAL_LOADING + "LoadBeam"):
elements: List = None
loadType: BeamLoadType = None
direction: LoadDirection = None
loadAxis: Axis = None
loadAxisType: LoadAxisType = None
isProjected: bool = None
values: List = None
positions: List = None
elements: Optional[List] = None
loadType: Optional[BeamLoadType] = None
direction: Optional[LoadDirection] = None
loadAxis: Optional[Axis] = None
loadAxisType: Optional[LoadAxisType] = None
isProjected: Optional[bool] = None
values: Optional[List] = None
positions: Optional[List] = None
class LoadCombinations(Base, speckle_type=STRUCTURAL_LOADING + "LoadCombination"):
name: str = None
name: Optional[str] = None
loadCases: List
loadFactors: List
combinationType: CombinationType
class LoadFace(Load, speckle_type=STRUCTURAL_LOADING + "LoadFace"):
elements: List = None
loadType: FaceLoadType = None
direction: LoadDirection2D = None
loadAxis: Axis = None
loadAxisType: LoadAxisType = None
isProjected: bool = None
values: List = None
positions: List = None
elements: Optional[List] = None
loadType: Optional[FaceLoadType] = None
direction: Optional[LoadDirection2D] = None
loadAxis: Optional[Axis] = None
loadAxisType: Optional[LoadAxisType] = None
isProjected: Optional[bool] = None
values: Optional[List] = None
positions: Optional[List] = None
class LoadGravity(Load, speckle_type=STRUCTURAL_LOADING + "LoadGravity"):
elements: List = None
nodes: List = None
gravityFactors: Vector = None
elements: Optional[List] = None
nodes: Optional[List] = None
gravityFactors: Optional[Vector] = None
class LoadNode(Load, speckle_type=STRUCTURAL_LOADING + "LoadNode"):
nodes: List = None
loadAxis: Axis = None
direction: LoadDirection = None
nodes: Optional[List] = None
loadAxis: Optional[Axis] = None
direction: Optional[LoadDirection] = None
value: float = 0.0
@@ -1,7 +1,7 @@
from enum import Enum
from typing import Optional
from ..base import Base
from specklepy.objects.base import Base
STRUCTURAL_MATERIALS = "Objects.Structural.Materials"
@@ -21,12 +21,14 @@ class MaterialType(int, Enum):
Other = 11
class Material(Base, speckle_type=STRUCTURAL_MATERIALS):
name: str = None
grade: str = None
materialType: MaterialType = None
designCode: str = None
codeYear: str = None
class StructuralMaterial(
Base, speckle_type=STRUCTURAL_MATERIALS + ".StructuralMaterial"
):
name: Optional[str] = None
grade: Optional[str] = None
materialType: Optional[MaterialType] = None
designCode: Optional[str] = None
codeYear: Optional[str] = None
strength: float = 0.0
elasticModulus: float = 0.0
poissonsRatio: float = 0.0
@@ -38,22 +40,22 @@ class Material(Base, speckle_type=STRUCTURAL_MATERIALS):
materialSafetyFactor: float = 0.0
class Concrete(Material, speckle_type=STRUCTURAL_MATERIALS + ".Concrete"):
class Concrete(StructuralMaterial):
compressiveStrength: float = 0.0
tensileStrength: float = 0.0
flexuralStrength: float = 0.0
maxCompressiveStrain: float = 0.0
maxTensileStrain: float = 0.0
maxAggregateSize: float = 0.0
lightweight: bool = None
lightweight: Optional[bool] = None
class Steel(Material, speckle_type=STRUCTURAL_MATERIALS + ".Steel"):
class Steel(StructuralMaterial, speckle_type=STRUCTURAL_MATERIALS + ".Steel"):
yieldStrength: float = 0.0
ultimateStrength: float = 0.0
maxStrain: float = 0.0
strainHardeningModulus: float = 0.0
class Timber(Material, speckle_type=STRUCTURAL_MATERIALS + ".Timber"):
species: str = None
class Timber(StructuralMaterial, speckle_type=STRUCTURAL_MATERIALS + ".Timber"):
species: Optional[str] = None
+27 -27
View File
@@ -1,12 +1,11 @@
from enum import Enum
from typing import Optional
from ..base import Base
from specklepy.objects.base import Base
from specklepy.objects.structural.axis import Axis
from specklepy.objects.structural.materials import StructuralMaterial
from .material import *
from .axis import Axis
STRUCTURAL_PROPERTY = "Objectives.Structural.Properties"
STRUCTURAL_PROPERTY = "Objects.Structural.Properties"
class MemberType(int, Enum):
@@ -39,7 +38,6 @@ class ReferenceSurface(int, Enum):
class PropertyType2D(int, Enum):
Stress = 0
Fabric = 1
Plate = 2
@@ -59,7 +57,7 @@ class PropertyType3D(int, Enum):
class ShapeType(int, Enum):
Rectangular = 0
Circular = 1
I = 2
I = 2 # noqa: E741
Tee = 3
Angle = 4
Channel = 5
@@ -67,6 +65,7 @@ class ShapeType(int, Enum):
Box = 7
Catalogue = 8
Explicit = 9
Undefined = 10
class PropertyTypeSpring(int, Enum):
@@ -89,36 +88,37 @@ class PropertyTypeDamper(int, Enum):
class Property(Base, speckle_type=STRUCTURAL_PROPERTY):
name: str = None
name: Optional[str] = None
class SectionProfile(Base, speckle_type=STRUCTURAL_PROPERTY + ".SectionProfile"):
name: str = None
shapeType: ShapeType = None
class SectionProfile(
Base, speckle_type=STRUCTURAL_PROPERTY + ".Profiles.SectionProfile"
):
name: Optional[str] = None
shapeType: Optional[ShapeType] = None
area: float = 0.0
Iyy: float = 0.0
Izz: float = 0.0
J: float = 0.0
Ky: float = 0.0
weight: float = 0.0
units: str = None
class Property1D(Property, speckle_type=STRUCTURAL_PROPERTY + ".Property1D"):
memberType: MemberType = None
Material: Material = None
SectionProfile: SectionProfile = None
BaseReferencePoint: BaseReferencePoint = None
memberType: Optional[MemberType] = None
material: Optional[StructuralMaterial] = None
profile: Optional[SectionProfile] = None
referencePoint: Optional[BaseReferencePoint] = None
offsetY: float = 0.0
offsetZ: float = 0.0
class Property2D(Property, speckle_type=STRUCTURAL_PROPERTY + ".Property2D"):
PropertyType2D: PropertyType2D = None
type: Optional[PropertyType2D] = None
thickness: float = 0.0
Material: Material = None
axis: Axis = None
referenceSurface: ReferenceSurface = None
material: Optional[StructuralMaterial] = None
orientationAxis: Optional[Axis] = None
refSurface: Optional[ReferenceSurface] = None
zOffset: float = 0.0
modifierInPlane: float = 0.0
modifierBending: float = 0.0
@@ -127,13 +127,13 @@ class Property2D(Property, speckle_type=STRUCTURAL_PROPERTY + ".Property2D"):
class Property3D(Property, speckle_type=STRUCTURAL_PROPERTY + ".Property3D"):
PropertyType3D: PropertyType3D = None
Material: Material = None
axis: Axis = None
type: Optional[PropertyType3D] = None
material: Optional[StructuralMaterial] = None
orientationAxis: Optional[Axis] = None
class PropertyDamper(Property, speckle_type=STRUCTURAL_PROPERTY + ".PropertyDamper"):
damperType: PropertyTypeDamper = None
damperType: Optional[PropertyTypeDamper] = None
dampingX: float = 0.0
dampingY: float = 0.0
dampingZ: float = 0.0
@@ -150,14 +150,14 @@ class PropertyMass(Property, speckle_type=STRUCTURAL_PROPERTY + ".PropertyMass")
inertiaXY: float = 0.0
inertiaYZ: float = 0.0
inertiaZX: float = 0.0
massModified: bool = None
massModified: Optional[bool] = None
massModifierX: float = 0.0
massModifierY: float = 0.0
massModifierZ: float = 0.0
class PropertySpring(Property, speckle_type=STRUCTURAL_PROPERTY + ".PropertySpring"):
springType: PropertyTypeSpring = None
springType: Optional[PropertyTypeSpring] = None
springCurveX: float = 0.0
stiffnessX: float = 0.0
springCurveY: float = 0.0
+130 -132
View File
@@ -1,18 +1,16 @@
from typing import List
from typing import List, Optional
from ..base import Base
from ..geometry import *
from .loading import *
from .geometry import *
from .analysis import Model
from specklepy.objects.base import Base
from specklepy.objects.structural.analysis import Model
from specklepy.objects.structural.geometry import Element1D, Element2D, Element3D, Node
STRUCTURAL_RESULTS = "Objects.Structural.Results."
class Result(Base, speckle_type=STRUCTURAL_RESULTS + "Result"):
resultCase: Base = None
permutation: str = None
description: str = None
resultCase: Optional[Base] = None
permutation: Optional[str] = None
description: Optional[str] = None
class ResultSet1D(Result, speckle_type=STRUCTURAL_RESULTS + "ResultSet1D"):
@@ -20,29 +18,29 @@ class ResultSet1D(Result, speckle_type=STRUCTURAL_RESULTS + "ResultSet1D"):
class Result1D(Result, speckle_type=STRUCTURAL_RESULTS + "Result1D"):
element: Element1D = None
position: float = 0.0
dispX: float = 0.0
dispY: float = 0.0
dispZ: float = 0.0
rotXX: float = 0.0
rotYY: float = 0.0
rotZZ: float = 0.0
forceX: float = 0.0
forceY: float = 0.0
forceZ: float = 0.0
momentXX: float = 0.0
momentYY: float = 0.0
momentZZ: float = 0.0
axialStress: float = 0.0
shearStressY: float = 0.0
shearStressZ: float = 0.0
bendingStressYPos: float = 0.0
bendingStressYNeg: float = 0.0
bendingStressZPos: float = 0.0
bendingStressZNeg: float = 0.0
combinedStressMax: float = 0.0
combinedStressMin: float = 0.0
element: Optional[Element1D] = None
position: Optional[float] = None
dispX: Optional[float] = None
dispY: Optional[float] = None
dispZ: Optional[float] = None
rotXX: Optional[float] = None
rotYY: Optional[float] = None
rotZZ: Optional[float] = None
forceX: Optional[float] = None
forceY: Optional[float] = None
forceZ: Optional[float] = None
momentXX: Optional[float] = None
momentYY: Optional[float] = None
momentZZ: Optional[float] = None
axialStress: Optional[float] = None
shearStressY: Optional[float] = None
shearStressZ: Optional[float] = None
bendingStressYPos: Optional[float] = None
bendingStressYNeg: Optional[float] = None
bendingStressZPos: Optional[float] = None
bendingStressZNeg: Optional[float] = None
combinedStressMax: Optional[float] = None
combinedStressMin: Optional[float] = None
class ResultSet2D(Result, speckle_type=STRUCTURAL_RESULTS + "ResultSet2D"):
@@ -50,37 +48,37 @@ class ResultSet2D(Result, speckle_type=STRUCTURAL_RESULTS + "ResultSet2D"):
class Result2D(Result, speckle_type=STRUCTURAL_RESULTS + "Result2D"):
element: Element2D = None
element: Optional[Element2D] = None
position: List
dispX: float = 0.0
dispY: float = 0.0
dispZ: float = 0.0
forceXX: float = 0.0
forceYY: float = 0.0
forceXY: float = 0.0
momentXX: float = 0.0
momentYY: float = 0.0
momentXY: float = 0.0
shearX: float = 0.0
shearY: float = 0.0
stressTopXX: float = 0.0
stressTopYY: float = 0.0
stressTopZZ: float = 0.0
stressTopXY: float = 0.0
stressTopYZ: float = 0.0
stressTopZX: float = 0.0
stressMidXX: float = 0.0
stressMidYY: float = 0.0
stressMidZZ: float = 0.0
stressMidXY: float = 0.0
stressMidYZ: float = 0.0
stressMidZX: float = 0.0
stressBotXX: float = 0.0
stressBotYY: float = 0.0
stressBotZZ: float = 0.0
stressBotXY: float = 0.0
stressBotYZ: float = 0.0
stressBotZX: float = 0.0
dispX: Optional[float] = None
dispY: Optional[float] = None
dispZ: Optional[float] = None
forceXX: Optional[float] = None
forceYY: Optional[float] = None
forceXY: Optional[float] = None
momentXX: Optional[float] = None
momentYY: Optional[float] = None
momentXY: Optional[float] = None
shearX: Optional[float] = None
shearY: Optional[float] = None
stressTopXX: Optional[float] = None
stressTopYY: Optional[float] = None
stressTopZZ: Optional[float] = None
stressTopXY: Optional[float] = None
stressTopYZ: Optional[float] = None
stressTopZX: Optional[float] = None
stressMidXX: Optional[float] = None
stressMidYY: Optional[float] = None
stressMidZZ: Optional[float] = None
stressMidXY: Optional[float] = None
stressMidYZ: Optional[float] = None
stressMidZX: Optional[float] = None
stressBotXX: Optional[float] = None
stressBotYY: Optional[float] = None
stressBotZZ: Optional[float] = None
stressBotXY: Optional[float] = None
stressBotYZ: Optional[float] = None
stressBotZX: Optional[float] = None
class ResultSet3D(Result, speckle_type=STRUCTURAL_RESULTS + "ResultSet3D"):
@@ -88,44 +86,44 @@ class ResultSet3D(Result, speckle_type=STRUCTURAL_RESULTS + "ResultSet3D"):
class Result3D(Result, speckle_type=STRUCTURAL_RESULTS + "Result3D"):
element: Element3D = None
element: Optional[Element3D] = None
position: List
dispX: float = 0.0
dispY: float = 0.0
dispZ: float = 0.0
stressXX: float = 0.0
stressYY: float = 0.0
stressZZ: float = 0.0
stressXY: float = 0.0
stressYZ: float = 0.0
stressZX: float = 0.0
dispX: Optional[float] = None
dispY: Optional[float] = None
dispZ: Optional[float] = None
stressXX: Optional[float] = None
stressYY: Optional[float] = None
stressZZ: Optional[float] = None
stressXY: Optional[float] = None
stressYZ: Optional[float] = None
stressZX: Optional[float] = None
class ResultGlobal(Result, speckle_type=STRUCTURAL_RESULTS + "ResultGlobal"):
model: Model = None
loadX: float = 0.0
loadY: float = 0.0
loadZ: float = 0.0
loadXX: float = 0.0
loadYY: float = 0.0
loadZZ: float = 0.0
reactionX: float = 0.0
reactionY: float = 0.0
reactionZ: float = 0.0
reactionXX: float = 0.0
reactionYY: float = 0.0
reactionZZ: float = 0.0
mode: float = 0.0
frequency: float = 0.0
loadFactor: float = 0.0
modalStiffness: float = 0.0
modalGeoStiffness: float = 0.0
effMassX: float = 0.0
effMassY: float = 0.0
effMassZ: float = 0.0
effMassXX: float = 0.0
effMassYY: float = 0.0
effMassZZ: float = 0.0
model: Optional[Model] = None
loadX: Optional[float] = None
loadY: Optional[float] = None
loadZ: Optional[float] = None
loadXX: Optional[float] = None
loadYY: Optional[float] = None
loadZZ: Optional[float] = None
reactionX: Optional[float] = None
reactionY: Optional[float] = None
reactionZ: Optional[float] = None
reactionXX: Optional[float] = None
reactionYY: Optional[float] = None
reactionZZ: Optional[float] = None
mode: Optional[float] = None
frequency: Optional[float] = None
loadFactor: Optional[float] = None
modalStiffness: Optional[float] = None
modalGeoStiffness: Optional[float] = None
effMassX: Optional[float] = None
effMassY: Optional[float] = None
effMassZ: Optional[float] = None
effMassXX: Optional[float] = None
effMassYY: Optional[float] = None
effMassZZ: Optional[float] = None
class ResultSetNode(Result, speckle_type=STRUCTURAL_RESULTS + "ResultSetNode"):
@@ -133,42 +131,42 @@ class ResultSetNode(Result, speckle_type=STRUCTURAL_RESULTS + "ResultSetNode"):
class ResultNode(Result, speckle_type=STRUCTURAL_RESULTS + " ResultNode"):
node: Node = None
dispX: float = 0.0
dispY: float = 0.0
dispZ: float = 0.0
rotXX: float = 0.0
rotYY: float = 0.0
rotZZ: float = 0.0
reactionX: float = 0.0
reactionY: float = 0.0
reactionZ: float = 0.0
reactionXX: float = 0.0
reactionYY: float = 0.0
reactionZZ: float = 0.0
constraintX: float = 0.0
constraintY: float = 0.0
constraintZ: float = 0.0
constraintXX: float = 0.0
constraintYY: float = 0.0
constraintZZ: float = 0.0
velX: float = 0.0
velY: float = 0.0
velZ: float = 0.0
velXX: float = 0.0
velYY: float = 0.0
velZZ: float = 0.0
accX: float = 0.0
accY: float = 0.0
accZ: float = 0.0
accXX: float = 0.0
accYY: float = 0.0
accZZ: float = 0.0
node: Optional[Node] = None
dispX: Optional[float] = None
dispY: Optional[float] = None
dispZ: Optional[float] = None
rotXX: Optional[float] = None
rotYY: Optional[float] = None
rotZZ: Optional[float] = None
reactionX: Optional[float] = None
reactionY: Optional[float] = None
reactionZ: Optional[float] = None
reactionXX: Optional[float] = None
reactionYY: Optional[float] = None
reactionZZ: Optional[float] = None
constraintX: Optional[float] = None
constraintY: Optional[float] = None
constraintZ: Optional[float] = None
constraintXX: Optional[float] = None
constraintYY: Optional[float] = None
constraintZZ: Optional[float] = None
velX: Optional[float] = None
velY: Optional[float] = None
velZ: Optional[float] = None
velXX: Optional[float] = None
velYY: Optional[float] = None
velZZ: Optional[float] = None
accX: Optional[float] = None
accY: Optional[float] = None
accZ: Optional[float] = None
accXX: Optional[float] = None
accYY: Optional[float] = None
accZZ: Optional[float] = None
class ResultSetAll(Base, speckle_type=None):
resultSet1D: ResultSet1D = None
resultSet2D: ResultSet2D = None
resultSet3D: ResultSet3D = None
resultsGlobal: ResultGlobal = None
resultsNode: ResultSetNode = None
resultSet1D: Optional[ResultSet1D] = None
resultSet2D: Optional[ResultSet2D] = None
resultSet3D: Optional[ResultSet3D] = None
resultsGlobal: Optional[ResultGlobal] = None
resultsNode: Optional[ResultSetNode] = None
+60 -8
View File
@@ -1,6 +1,14 @@
from typing import Union
from specklepy.logging.exceptions import SpeckleException, SpeckleInvalidUnitException
from enum import Enum
from typing import Union
from specklepy.logging.exceptions import SpeckleException, SpeckleInvalidUnitException
__all__ = [
"Units",
"get_encoding_from_units",
"get_units_from_encoding",
"get_units_from_string",
]
class Units(Enum):
@@ -27,6 +35,7 @@ UNITS_STRINGS = {
Units.none: ["none", "null"],
}
UNITS_ENCODINGS = {
Units.none: 0,
None: 0,
@@ -41,6 +50,20 @@ UNITS_ENCODINGS = {
}
UNIT_SCALE = {
Units.none: 1,
Units.mm: 0.001,
Units.cm: 0.01,
Units.m: 1.0,
Units.km: 1000.0,
Units.inches: 0.0254,
Units.feet: 0.3048,
Units.yards: 0.9144,
Units.miles: 1609.340,
}
"""Unit scaling factor to meters"""
def get_units_from_string(unit: str) -> Units:
if not isinstance(unit, str):
raise SpeckleInvalidUnitException(unit)
@@ -51,20 +74,49 @@ def get_units_from_string(unit: str) -> Units:
raise SpeckleInvalidUnitException(unit)
def get_units_from_encoding(unit: int):
def get_units_from_encoding(unit: int) -> Units:
for name, encoding in UNITS_ENCODINGS.items():
if unit == encoding:
return name
return name or Units.none
raise SpeckleException(
message=f"Could not understand what unit {unit} is referring to. Please enter a valid unit encoding (eg {UNITS_ENCODINGS})."
message=(
f"Could not understand what unit {unit} is referring to."
f"Please enter a valid unit encoding (eg {UNITS_ENCODINGS})."
)
)
def get_encoding_from_units(unit: Union[Units, None]):
def get_encoding_from_units(unit: Union[Units, str, None]):
maybe_sanitized_unit = unit
if isinstance(unit, str):
for unit_enum, aliases in UNITS_STRINGS.items():
if unit in aliases:
maybe_sanitized_unit = unit_enum
try:
return UNITS_ENCODINGS[unit]
return UNITS_ENCODINGS[maybe_sanitized_unit]
except KeyError as e:
raise SpeckleException(
message=f"No encoding exists for unit {unit}. Please enter a valid unit to encode (eg {UNITS_ENCODINGS})."
message=(
f"No encoding exists for unit {maybe_sanitized_unit}."
f"Please enter a valid unit to encode (eg {UNITS_ENCODINGS})."
)
) from e
def get_scale_factor_from_string(fromUnits: str, toUnits: str) -> float:
"""Returns a scalar to convert distance values from one unit system to another"""
return get_scale_factor(get_units_from_string(fromUnits), get_units_from_string(toUnits))
def get_scale_factor(fromUnits: Units, toUnits: Units) -> float:
"""Returns a scalar to convert distance values from one unit system to another"""
return get_scale_factor_to_meters(fromUnits) / get_scale_factor_to_meters(toUnits)
def get_scale_factor_to_meters(fromUnits: Units) -> float:
"""Returns a scalar to convert distance values from one unit system to meters"""
if fromUnits not in UNIT_SCALE:
raise ValueError(f"Invalid units provided: {fromUnits}")
return UNIT_SCALE[fromUnits]
-27
View File
@@ -1,27 +0,0 @@
import sys
from pathlib import Path
from appdirs import user_data_dir
def base_path(app_name) -> Path:
# from appdirs https://github.com/ActiveState/appdirs/blob/master/appdirs.py
# default mac path is not the one we use (we use unix path), so using special case for this
system = sys.platform
if system.startswith("java"):
import platform
os_name = platform.java_ver()[3][0]
if os_name.startswith("Mac"):
system = "darwin"
if system == "darwin":
return Path(Path.home(), ".config", app_name)
return Path(user_data_dir(appname=app_name, appauthor=False, roaming=True))
def accounts_path(app_name: str = "Speckle") -> Path:
"""
Gets the path where the Speckle applications are looking for accounts.
"""
return base_path(app_name).joinpath("Accounts")
@@ -1,21 +1,17 @@
import re
import ujson
import hashlib
import re
import warnings
from uuid import uuid4
from enum import Enum
from typing import Any, Dict, List, Optional, Tuple
from uuid import uuid4
from warnings import warn
from typing import Any, Dict, List, Tuple
from specklepy.objects.base import Base, DataChunk
from specklepy.logging.exceptions import (
SpeckleException,
SpeckleWarning,
)
from specklepy.transports.abstract_transport import AbstractTransport
import ujson
# import for serialization
import specklepy.objects.geometry
import specklepy.objects.other
from specklepy.logging.exceptions import SpeckleException, SpeckleWarning
from specklepy.objects.base import Base, DataChunk
from specklepy.transports.abstract_transport import AbstractTransport
PRIMITIVES = (int, float, str, bool)
@@ -31,7 +27,8 @@ def safe_json_loads(obj: str, obj_id=None) -> Any:
import json
warn(
f"Failed to deserialise object (id: {obj_id}). This is likely a ujson big int error - falling back to json. \nError: {err}",
f"Failed to deserialise object (id: {obj_id}). This is likely a ujson big"
f" int error - falling back to json. \nError: {err}",
SpeckleWarning,
)
return json.loads(obj)
@@ -49,7 +46,9 @@ class BaseObjectSerializer:
] # holds deserialized objects so objects with same id return the same instance
def __init__(
self, write_transports: List[AbstractTransport] = None, read_transport=None
self,
write_transports: Optional[List[AbstractTransport]] = None,
read_transport: Optional[AbstractTransport] = None,
) -> None:
self.write_transports = write_transports or []
self.read_transport = read_transport
@@ -65,21 +64,23 @@ class BaseObjectSerializer:
base {Base} -- the base object to be decomposed and serialized
Returns:
(str, str) -- a tuple containing the object id of the base object and the serialized object string
(str, str) -- a tuple containing the object id of the base object and
the serialized object string
"""
obj_id, obj = self.traverse_base(base)
return obj_id, ujson.dumps(obj)
def traverse_base(self, base: Base) -> Tuple[str, Dict]:
def traverse_base(self, base: Base) -> Tuple[str, Dict[str, Any]]:
"""Decomposes the given base object and builds a serializable dictionary
Arguments:
base {Base} -- the base object to be decomposed and serialized
Returns:
(str, dict) -- a tuple containing the object id of the base object and the constructed serializable dictionary
(str, dict) -- a tuple containing the object id of the base object and
the constructed serializable dictionary
"""
self.__reset_writer()
@@ -212,6 +213,8 @@ class BaseObjectSerializer:
Returns:
Any -- a serializable version of the given object
"""
if obj is None:
return None
if isinstance(obj, PRIMITIVES):
return obj
@@ -249,16 +252,19 @@ class BaseObjectSerializer:
else:
try:
return obj.dict()
except:
except Exception:
warn(
f"Failed to handle {type(obj)} in `BaseObjectSerializer.traverse_value`",
f"Failed to handle {type(obj)} in"
" `BaseObjectSerializer.traverse_value`",
SpeckleWarning,
)
return str(obj)
def detach_helper(self, ref_id: str) -> Dict[str, str]:
"""Helper to keep track of detached objects and their depth in the family tree and create reference objects to place in the parent object
"""
Helper to keep track of detached objects and their depth in the family tree
and create reference objects to place in the parent object
Arguments:
ref_id {str} -- the id of the fully traversed object
@@ -281,7 +287,10 @@ class BaseObjectSerializer:
}
def __reset_writer(self) -> None:
"""Reinitializes the lineage, and other variables that get used during the json writing process"""
"""
Reinitializes the lineage, and other variables that get used during the json
writing process
"""
self.detach_lineage = [True]
self.lineage = []
self.family_tree = {}
@@ -358,7 +367,8 @@ class BaseObjectSerializer:
base.__setattr__(prop, self.recompose_base(obj=ref_obj))
else:
warnings.warn(
f"Could not find the referenced child object of id `{ref_id}` in the given read transport: {self.read_transport.name}",
f"Could not find the referenced child object of id `{ref_id}`"
f" in the given read transport: {self.read_transport.name}",
SpeckleWarning,
)
base.__setattr__(prop, self.handle_value(value))
@@ -373,7 +383,8 @@ class BaseObjectSerializer:
return base
def handle_value(self, obj: Any):
"""Helper for recomposing a base object by handling the dictionary representation's values
"""Helper for recomposing a base object by handling the dictionary
representation's values
Arguments:
obj {Any} -- a value from the base object dictionary
@@ -419,7 +430,8 @@ class BaseObjectSerializer:
ref_obj_str = self.read_transport.get_object(id=ref_id)
if not ref_obj_str:
warnings.warn(
f"Could not find the referenced child object of id `{ref_id}` in the given read transport: {self.read_transport.name}",
f"Could not find the referenced child object of id `{ref_id}` in the"
f" given read transport: {self.read_transport.name}",
SpeckleWarning,
)
return obj
+15 -25
View File
@@ -1,24 +1,12 @@
from abc import ABC, abstractmethod
from typing import Optional, List, Dict
from pydantic import BaseModel
from pydantic.config import Extra
# __________________
# | |
# | this is v wip |
# | pls be careful |
# |__________________|
# (\__/) ||
# (•ㅅ•) ||
# /  
from typing import Dict, List, Optional
class AbstractTransport(ABC, BaseModel):
_name: str = "Abstract"
class AbstractTransport(ABC):
@property
@abstractmethod
def name(self):
return type(self)._name
pass
@abstractmethod
def begin_write(self) -> None:
@@ -27,7 +15,9 @@ class AbstractTransport(ABC, BaseModel):
@abstractmethod
def end_write(self) -> None:
"""Optional: signals to the transport that no more items will need to be written."""
"""
Optional: signals to the transport that no more items will need to be written.
"""
pass
@abstractmethod
@@ -48,7 +38,8 @@ class AbstractTransport(ABC, BaseModel):
Arguments:
id {str} -- the hash of the object
source_transport {AbstractTransport) -- the transport through which the object can be found
source_transport {AbstractTransport)
-- the transport through which the object can be found
"""
pass
@@ -60,7 +51,8 @@ class AbstractTransport(ABC, BaseModel):
id {str} -- the hash of the object
Returns:
str -- the full string representation of the object (or null if no object is found)
str -- the full string representation
of the object (or null if no object is found)
"""
pass
@@ -72,7 +64,8 @@ class AbstractTransport(ABC, BaseModel):
id_list -- List of object id to be checked
Returns:
Dict[str, bool] -- keys: input ids, values: whether the transport has that object
Dict[str, bool] -- keys: input ids, values:
whether the transport has that object
"""
pass
@@ -84,12 +77,9 @@ class AbstractTransport(ABC, BaseModel):
Arguments:
id {str} -- the id of the object you want to copy
target_transport {AbstractTransport} -- the transport you want to copy the object to
target_transport {AbstractTransport}
-- the transport you want to copy the object to
Returns:
str -- the string representation of the root object
"""
pass
class Config:
extra = Extra.allow
arbitrary_types_allowed = True
+10 -8
View File
@@ -1,16 +1,18 @@
from typing import Any, List, Dict
from typing import Any, Dict, List
from specklepy.transports.abstract_transport import AbstractTransport
class MemoryTransport(AbstractTransport):
_name: str = "Memory"
objects: dict = {}
saved_object_count: int = 0
def __init__(self, name="Memory") -> None:
super().__init__()
self._name = name
self.objects = {}
self.saved_object_count = 0
def __init__(self, name=None, **data: Any) -> None:
super().__init__(**data)
if name:
self._name = name
@property
def name(self) -> str:
return self._name
def __repr__(self) -> str:
return f"MemoryTransport(objects: {len(self.objects)})"
+3 -1
View File
@@ -1 +1,3 @@
from .server import ServerTransport
from specklepy.transports.server.server import ServerTransport
__all__ = ["ServerTransport"]
+28 -14
View File
@@ -1,10 +1,11 @@
import gzip
import json
import logging
import threading
import queue
import gzip
import threading
import requests
from specklepy.logging.exceptions import SpeckleException
LOG = logging.getLogger(__name__)
@@ -17,6 +18,7 @@ class BatchSender(object):
stream_id,
token,
max_batch_size_mb=1,
max_batch_length=20000,
batch_buffer_length=10,
thread_count=4,
):
@@ -25,6 +27,7 @@ class BatchSender(object):
self._token = token
self.max_size = int(max_batch_size_mb * 1000 * 1000)
self.max_batch_length = int(max_batch_length)
self._batches = queue.Queue(batch_buffer_length)
self._crt_batch = []
self._crt_batch_size = 0
@@ -38,7 +41,11 @@ class BatchSender(object):
self._create_threads()
crt_obj_size = len(obj)
if not self._crt_batch or self._crt_batch_size + crt_obj_size < self.max_size:
crt_batch_length = len(self._crt_batch)
if not self._crt_batch or (
self._crt_batch_size + crt_obj_size < self.max_size
and crt_batch_length < self.max_batch_length
):
self._crt_batch.append((id, obj))
self._crt_batch_size += crt_obj_size
return
@@ -89,17 +96,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)
@@ -107,7 +115,8 @@ class BatchSender(object):
if not new_objects:
LOG.info(
f"Uploading batch of {len(batch)} objects: all objects are already in the server"
f"Uploading batch of {len(batch)} objects: all objects are already in"
" the server"
)
return
@@ -126,11 +135,16 @@ class BatchSender(object):
if r.status_code != 201:
LOG.warning("Upload server response: %s", r.text)
raise SpeckleException(
message=f"Could not save the object to the server - status code {r.status_code}"
message=(
"Could not save the object to the server - status code"
f" {r.status_code} ({r.text[:1000]})"
)
)
except json.JSONDecodeError as error:
return SpeckleException(
f"Failed to send objects to {self.server_url}. Please ensure this stream ({self.stream_id}) exists on this server and that you have permission to send to it.",
f"Failed to send objects to {self.server_url}. Please ensure this"
f" stream ({self.stream_id}) exists on this server and that you have"
" permission to send to it.",
error,
)
+33 -27
View File
@@ -1,11 +1,11 @@
import json
import requests
from typing import Any, Dict, List, Optional
from warnings import warn
from typing import Any, Dict, List
import requests
from specklepy.api.client import SpeckleClient
from specklepy.api.credentials import Account, get_account_from_token
from specklepy.core.api.client import SpeckleClient
from specklepy.core.api.credentials import Account, get_account_from_token
from specklepy.logging.exceptions import SpeckleException, SpeckleWarning
from specklepy.transports.abstract_transport import AbstractTransport
@@ -14,10 +14,10 @@ from .batch_sender import BatchSender
class ServerTransport(AbstractTransport):
"""
The `ServerTransport` is the vehicle through which you transport objects to and from a Speckle Server. Provide it to
`operations.send()` or `operations.receive()`.
The `ServerTransport` is the vehicle through which you transport objects to and
from a Speckle Server. Provide it to `operations.send()` or `operations.receive()`.
The `ServerTransport` can be authenticted two different ways:
The `ServerTransport` can be authenticated two different ways:
1. by providing a `SpeckleClient`
2. by providing an `Account`
3. by providing a `token` and `url`
@@ -29,14 +29,15 @@ class ServerTransport(AbstractTransport):
# here's the data you want to send
block = Block(length=2, height=4)
# next create the server transport - this is the vehicle through which you will send and receive
# next create the server transport - this is the vehicle through which
# you will send and receive
transport = ServerTransport(stream_id=new_stream_id, client=client)
# this serialises the block and sends it to the transport
hash = operations.send(base=block, transports=[transport])
# you can now create a commit on your stream with this object
commid_id = client.commit.create(
commit_id = client.commit.create(
stream_id=new_stream_id,
obj_id=hash,
message="this is a block I made in speckle-py",
@@ -44,28 +45,25 @@ class ServerTransport(AbstractTransport):
```
"""
_name = "RemoteTransport"
url: str = None
stream_id: str = None
account: Account = None
saved_obj_count: int = 0
session: requests.Session = None
def __init__(
self,
stream_id: str,
client: SpeckleClient = None,
account: Account = None,
token: str = None,
url: str = None,
**data: Any,
client: Optional[SpeckleClient] = None,
account: Optional[Account] = None,
token: Optional[str] = None,
url: Optional[str] = None,
name: str = "RemoteTransport",
) -> None:
super().__init__(**data)
super().__init__()
if client is None and account is None and token is None and url is None:
raise SpeckleException(
"You must provide either a client or a token and url to construct a ServerTransport."
"You must provide either a client or a token and url to construct a"
" ServerTransport."
)
self._name = name
self.account = None
self.saved_obj_count = 0
if account:
self.account = account
url = account.serverInfo.url
@@ -74,7 +72,8 @@ class ServerTransport(AbstractTransport):
if not client.account.token:
warn(
SpeckleWarning(
f"Unauthenticated Speckle Client provided to Server Transport for {self.url}. Receiving from private streams will fail."
"Unauthenticated Speckle Client provided to Server Transport"
f" for {self.url}. Receiving from private streams will fail."
)
)
else:
@@ -94,6 +93,10 @@ class ServerTransport(AbstractTransport):
{"Authorization": f"Bearer {self.account.token}", "Accept": "text/plain"}
)
@property
def name(self) -> str:
return self._name
def begin_write(self) -> None:
self.saved_obj_count = 0
@@ -118,8 +121,10 @@ class ServerTransport(AbstractTransport):
# return obj
raise SpeckleException(
"Getting a single object using `ServerTransport.get_object()` is not implemented. To get an object from the server, please use the `SpeckleClient.object.get()` route",
NotImplementedError,
"Getting a single object using `ServerTransport.get_object()` is not"
" implemented. To get an object from the server, please use the"
" `SpeckleClient.object.get()` route",
NotImplementedError(),
)
def has_objects(self, id_list: List[str]) -> Dict[str, bool]:
@@ -134,7 +139,8 @@ class ServerTransport(AbstractTransport):
if r.status_code != 200:
raise SpeckleException(
f"Can't get object {self.stream_id}/{id}: HTTP error {r.status_code} ({r.text[:1000]})"
f"Can't get object {self.stream_id}/{id}: HTTP error"
f" {r.status_code} ({r.text[:1000]})"
)
root_obj_serialized = r.text
root_obj = json.loads(root_obj_serialized)
+30 -40
View File
@@ -1,38 +1,30 @@
import os
import sqlite3
from typing import Any, List, Dict, Optional, Tuple
from contextlib import closing
from specklepy.transports.abstract_transport import AbstractTransport
from typing import Any, Dict, List, Optional, Tuple
from specklepy.core.helpers import speckle_path_provider
from specklepy.logging.exceptions import SpeckleException
from specklepy.paths import base_path
from specklepy.transports.abstract_transport import AbstractTransport
class SQLiteTransport(AbstractTransport):
_name = "SQLite"
_base_path: str = None
_root_path: str = None
__connection: sqlite3.Connection = None
app_name: str = ""
scope: str = ""
saved_obj_count: int = 0
max_size: int = None
_current_batch: List[Tuple[str, str]] = None
_current_batch_size: int = None
def __init__(
self,
base_path: Optional[str] = None,
app_name: Optional[str] = None,
scope: str = None,
scope: Optional[str] = None,
max_batch_size_mb: float = 10.0,
**data: Any,
name: str = "SQLite",
) -> None:
super().__init__(**data)
super().__init__()
self._name = name
self.app_name = app_name or "Speckle"
self.scope = scope or "Objects"
self._base_path = base_path or self.get_base_path(self.app_name)
self.max_size = int(max_batch_size_mb * 1000 * 1000)
self._current_batch = []
self.saved_obj_count = 0
self._current_batch: List[Tuple[str, str]] = []
self._current_batch_size = 0
try:
@@ -44,32 +36,24 @@ class SQLiteTransport(AbstractTransport):
self.__initialise()
except Exception as ex:
raise SpeckleException(
f"SQLiteTransport could not initialise {self.scope}.db at {self._base_path}. Either provide a different `base_path` or use an alternative transport.",
f"SQLiteTransport could not initialise {self.scope}.db at"
f" {self._base_path}. Either provide a different `base_path` or use an"
" alternative transport.",
ex,
)
def __repr__(self) -> str:
return f"SQLiteTransport(app: '{self.app_name}', scope: '{self.scope}')"
@property
def name(self) -> str:
return self._name
@staticmethod
def get_base_path(app_name):
# # from appdirs https://github.com/ActiveState/appdirs/blob/master/appdirs.py
# # default mac path is not the one we use (we use unix path), so using special case for this
# system = sys.platform
# if system.startswith("java"):
# import platform
# os_name = platform.java_ver()[3][0]
# if os_name.startswith("Mac"):
# system = "darwin"
# if system != "darwin":
# return user_data_dir(appname=app_name, appauthor=False, roaming=True)
# path = os.path.expanduser("~/.config/")
# return os.path.join(path, app_name)
return str(base_path(app_name))
return str(
speckle_path_provider.user_application_data_path().joinpath(app_name)
)
def save_object_from_transport(
self, id: str, source_transport: AbstractTransport
@@ -78,14 +62,16 @@ class SQLiteTransport(AbstractTransport):
Arguments:
id {str} -- the object id
source_transport {AbstractTransport) -- the transport through which the object can be found
source_transport {AbstractTransport)
-- the transport through which the object can be found
"""
serialized_object = source_transport.get_object(id)
self.save_object(id, serialized_object)
def save_object(self, id: str, serialized_object: str) -> None:
"""
Adds an object to the current batch to be written to the db. If the current batch is full,
Adds an object to the current batch to be written to the db.
If the current batch is full,
the batch is written to the db and the current batch is reset.
Arguments:
@@ -117,7 +103,8 @@ class SQLiteTransport(AbstractTransport):
self.__connection.commit()
except Exception as ex:
raise SpeckleException(
f"Could not save the batch of objects to the local db. Inner exception: {ex}",
"Could not save the batch of objects to the local db. Inner exception:"
f" {ex}",
ex,
)
@@ -156,7 +143,10 @@ class SQLiteTransport(AbstractTransport):
raise NotImplementedError
def get_all_objects(self):
"""Returns all the objects in the store. NOTE: do not use for large collections!"""
"""
Returns all the objects in the store.
NOTE: do not use for large collections!
"""
self.__check_connection()
with closing(self.__connection.cursor()) as c:
rows = c.execute("SELECT * FROM objects").fetchall()
+2 -107
View File
@@ -1,113 +1,8 @@
import uuid
import random
import pytest
import requests
from specklepy.api.models import Stream
from specklepy.api.client import SpeckleClient
from specklepy.objects.base import Base
from specklepy.objects.geometry import Point
from specklepy.objects.fakemesh import FakeDirection, FakeMesh
from specklepy.logging import metrics
metrics.disable()
@pytest.fixture(scope="session")
def host():
return "localhost:3000"
def seed_user(host):
seed = uuid.uuid4().hex
user_dict = {
"email": f"{seed[0:7]}@spockle.com",
"password": "$uper$3cr3tP@ss",
"name": f"{seed[0:7]} Name",
"company": "test spockle",
}
r = requests.post(
url=f"http://{host}/auth/local/register?challenge=pyspeckletests",
data=user_dict,
# do not follow redirects here, they lead to the frontend, which might not be
# running in a test environment
# causing the response to not be OK in the end
allow_redirects=False
)
if not r.ok:
raise Exception(f"Cannot seed user: {r.reason}")
access_code = r.text.split("access_code=")[1]
r_tokens = requests.post(
url=f"http://{host}/auth/token",
json={
"appSecret": "spklwebapp",
"appId": "spklwebapp",
"accessCode": access_code,
"challenge": "pyspeckletests",
},
)
user_dict.update(**r_tokens.json())
return user_dict
@pytest.fixture(scope="session")
def user_dict(host):
return seed_user(host)
@pytest.fixture(scope="session")
def second_user_dict(host):
return seed_user(host)
@pytest.fixture(scope="session")
def client(host, user_dict):
client = SpeckleClient(host=host, use_ssl=False)
client.authenticate_with_token(user_dict["token"])
return client
@pytest.fixture(scope="session")
def second_client(host, second_user_dict):
client = SpeckleClient(host=host, use_ssl=False)
client.authenticate_with_token(second_user_dict["token"])
return client
@pytest.fixture(scope="session")
def sample_stream(client):
stream = Stream(
name="a sample stream for testing",
description="a stream created for testing",
isPublic=True,
)
stream.id = client.stream.create(stream.name, stream.description, stream.isPublic)
return stream
@pytest.fixture(scope="session")
def mesh():
mesh = FakeMesh()
mesh.name = "my_mesh"
mesh.vertices = [random.uniform(0, 10) for _ in range(1, 210)]
mesh.faces = list(range(1, 210))
mesh["@(100)colours"] = [random.uniform(0, 10) for _ in range(1, 210)]
mesh["@()default_chunk"] = [random.uniform(0, 10) for _ in range(1, 210)]
mesh.cardinal_dir = FakeDirection.WEST
mesh.test_bases = [Base(name=f"test {i}") for i in range(1, 22)]
mesh.detach_this = Base(name="predefined detached base")
mesh["@detach"] = Base(name="detached base")
mesh["@detached_list"] = [
42,
"some text",
[1, 2, 3],
Base(name="detached within a list"),
]
mesh.origin = Point(x=4, y=2)
return mesh
@pytest.fixture(scope="session")
View File
+114
View File
@@ -0,0 +1,114 @@
import random
import uuid
from urllib.parse import parse_qs, urlparse
import pytest
import requests
from specklepy.api.client import SpeckleClient
from specklepy.api.models import Stream
from specklepy.logging import metrics
from specklepy.objects.base import Base
from specklepy.objects.fakemesh import FakeDirection, FakeMesh
from specklepy.objects.geometry import Point
metrics.disable()
@pytest.fixture(scope="session")
def host():
return "localhost:3000"
def seed_user(host):
seed = uuid.uuid4().hex
user_dict = {
"email": f"{seed[0:7]}@spockle.com",
"password": "$uper$3cr3tP@ss",
"name": f"{seed[0:7]} Name",
"company": "test spockle",
}
r = requests.post(
url=f"http://{host}/auth/local/register?challenge=pyspeckletests",
data=user_dict,
# do not follow redirects here, they lead to the frontend, which might not be
# running in a test environment
# causing the response to not be OK in the end
allow_redirects=False,
)
if not r.ok:
raise Exception(f"Cannot seed user: {r.reason}")
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",
json={
"appSecret": "spklwebapp",
"appId": "spklwebapp",
"accessCode": access_code,
"challenge": "pyspeckletests",
},
)
user_dict.update(**r_tokens.json())
return user_dict
@pytest.fixture(scope="session")
def user_dict(host):
return seed_user(host)
@pytest.fixture(scope="session")
def second_user_dict(host):
return seed_user(host)
@pytest.fixture(scope="session")
def client(host, user_dict):
client = SpeckleClient(host=host, use_ssl=False)
client.authenticate_with_token(user_dict["token"])
return client
@pytest.fixture(scope="session")
def second_client(host, second_user_dict):
client = SpeckleClient(host=host, use_ssl=False)
client.authenticate_with_token(second_user_dict["token"])
return client
@pytest.fixture(scope="session")
def sample_stream(client):
stream = Stream(
name="a sample stream for testing",
description="a stream created for testing",
isPublic=True,
)
stream.id = client.stream.create(stream.name, stream.description, stream.isPublic)
return stream
@pytest.fixture(scope="session")
def mesh():
mesh = FakeMesh()
mesh.name = "my_mesh"
mesh.vertices = [random.uniform(0, 10) for _ in range(1, 210)]
mesh.faces = list(range(1, 210))
mesh["@(100)colours"] = [random.uniform(0, 10) for _ in range(1, 210)]
mesh["@()default_chunk"] = [random.uniform(0, 10) for _ in range(1, 210)]
mesh.cardinal_dir = FakeDirection.WEST
mesh.test_bases = [Base(name=f"test {i}") for i in range(1, 22)]
mesh.detach_this = Base(name="predefined detached base")
mesh["@detach"] = Base(name="detached base")
mesh["@detached_list"] = [
42,
"some text",
[1, 2, 3],
Base(name="detached within a list"),
]
mesh.origin = Point(x=4, y=2)
return mesh
@@ -0,0 +1,263 @@
"""Run integration tests with a speckle server."""
import os
import secrets
import string
from pathlib import Path
from typing import Dict
import pytest
from gql import gql
from speckle_automate.schema import AutomateBase
from specklepy.api import operations
from specklepy.api.client import SpeckleClient
from specklepy.objects.base import Base
from specklepy.transports.server import ServerTransport
from speckle_automate import (
AutomationContext,
AutomationRunData,
AutomationStatus,
run_function,
)
def crypto_random_string(length: int) -> str:
"""Generate a semi crypto random string of a given length."""
alphabet = string.ascii_letters + string.digits
return "".join(secrets.choice(alphabet) for _ in range(length))
def register_new_automation(
project_id: str,
model_id: str,
speckle_client: SpeckleClient,
automation_id: str,
automation_name: str,
automation_revision_id: str,
):
"""Register a new automation in the speckle server."""
query = gql(
"""
mutation CreateAutomation(
$projectId: String!
$modelId: String!
$automationName: String!
$automationId: String!
$automationRevisionId: String!
) {
automationMutations {
create(
input: {
projectId: $projectId
modelId: $modelId
automationName: $automationName
automationId: $automationId
automationRevisionId: $automationRevisionId
}
)
}
}
"""
)
params = {
"projectId": project_id,
"modelId": model_id,
"automationName": automation_name,
"automationId": automation_id,
"automationRevisionId": automation_revision_id,
}
speckle_client.httpclient.execute(query, params)
@pytest.fixture()
def speckle_token(user_dict: Dict[str, str]) -> str:
"""Provide a speckle token for the test suite."""
return user_dict["token"]
@pytest.fixture()
def speckle_server_url(host: str) -> str:
"""Provide a speckle server url for the test suite, default to localhost."""
return f"http://{host}"
@pytest.fixture()
def test_client(speckle_server_url: str, speckle_token: str) -> SpeckleClient:
"""Initialize a SpeckleClient for testing."""
test_client = SpeckleClient(speckle_server_url, use_ssl=False)
test_client.authenticate_with_token(speckle_token)
return test_client
@pytest.fixture()
def test_object() -> Base:
"""Create a Base model for testing."""
root_object = Base()
root_object.foo = "bar"
return root_object
@pytest.fixture()
def automation_run_data(
test_object: Base, test_client: SpeckleClient, speckle_server_url: str
) -> AutomationRunData:
"""Set up an automation context for testing."""
project_id = test_client.stream.create("Automate function e2e test")
branch_name = "main"
model = test_client.branch.get(project_id, branch_name, commits_limit=1)
model_id: str = model.id
root_obj_id = operations.send(
test_object, [ServerTransport(project_id, test_client)]
)
version_id = test_client.commit.create(project_id, root_obj_id)
automation_name = crypto_random_string(10)
automation_id = crypto_random_string(10)
automation_revision_id = crypto_random_string(10)
register_new_automation(
project_id,
model_id,
test_client,
automation_id,
automation_name,
automation_revision_id,
)
automation_run_id = crypto_random_string(10)
function_id = crypto_random_string(10)
function_revision = crypto_random_string(10)
return AutomationRunData(
project_id=project_id,
model_id=model_id,
branch_name=branch_name,
version_id=version_id,
speckle_server_url=speckle_server_url,
automation_id=automation_id,
automation_revision_id=automation_revision_id,
automation_run_id=automation_run_id,
function_id=function_id,
function_revision=function_revision,
)
def get_automation_status(
project_id: str,
model_id: str,
speckle_client: SpeckleClient,
):
query = gql(
"""
query AutomationRuns(
$projectId: String!
$modelId: String!
)
{
project(id: $projectId) {
model(id: $modelId) {
automationStatus {
id
status
statusMessage
automationRuns {
id
automationId
versionId
createdAt
updatedAt
status
functionRuns {
id
functionId
elapsed
status
contextView
statusMessage
results
resultVersions {
id
}
}
}
}
}
}
}
"""
)
params = {
"projectId": project_id,
"modelId": model_id,
}
response = speckle_client.httpclient.execute(query, params)
return response["project"]["model"]["automationStatus"]
class FunctionInputs(AutomateBase):
forbidden_speckle_type: str
def automate_function(
automate_context: AutomationContext,
function_inputs: FunctionInputs,
) -> None:
"""Hey, trying the automate sdk experience here."""
version_root_object = automate_context.receive_version()
count = 0
if version_root_object.speckle_type == function_inputs.forbidden_speckle_type:
if not version_root_object.id:
raise ValueError("Cannot operate on objects without their id's.")
automate_context.add_object_error(
version_root_object.id,
"This project should not contain the type: "
f"{function_inputs.forbidden_speckle_type}",
)
count += 1
if count > 0:
automate_context.mark_run_failed(
"Automation failed: "
f"Found {count} object that have a forbidden speckle type: "
f"{function_inputs.forbidden_speckle_type}"
)
else:
automate_context.mark_run_success("No forbidden types found.")
def test_function_run(automation_run_data: AutomationRunData, speckle_token: str):
"""Run an integration test for the automate function."""
automation_context = run_function(
automate_function,
automation_run_data,
speckle_token,
FunctionInputs(forbidden_speckle_type="Base"),
)
assert automation_context.run_status == AutomationStatus.FAILED
status = get_automation_status(
automation_run_data.project_id,
automation_run_data.model_id,
automation_context.speckle_client,
)
assert status["status"] == automation_context.run_status
status_message = status["automationRuns"][0]["functionRuns"][0]["statusMessage"]
assert status_message == automation_context._automation_result.status_message
def test_file_uploads(automation_run_data: AutomationRunData, speckle_token: str):
"""Test file store capabilities of the automate sdk."""
automation_context = AutomationContext.initialize(
automation_run_data, speckle_token
)
path = Path(f"./{crypto_random_string(10)}").resolve()
path.write_text("foobar")
automation_context.store_file_result(path)
os.remove(path)
assert len(automation_context._automation_result.blobs) == 1
@@ -1,4 +1,5 @@
import pytest
from specklepy.api.client import SpeckleClient
from specklepy.api.models import Activity, ActivityCollection, User
from specklepy.logging.exceptions import SpeckleException
@@ -38,7 +39,7 @@ class TestUser:
assert my_activity.totalCount
assert isinstance(their_activity, ActivityCollection)
older_activity = client.user.activity(before=my_activity.items[0].time)
older_activity = client.active_user.activity(before=my_activity.items[0].time)
assert isinstance(older_activity, ActivityCollection)
assert older_activity.totalCount
@@ -1,7 +1,8 @@
import pytest
from specklepy.api import operations
from specklepy.transports.server import ServerTransport
from specklepy.api.models import Branch, Commit, Stream
from specklepy.transports.server import ServerTransport
class TestBranch:

Some files were not shown because too many files have changed in this diff Show More