Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 209a95879f | |||
| 4f829d9908 | |||
| ac5345f528 | |||
| 1142481d89 | |||
| b4690f082f | |||
| 81a98ea938 | |||
| 9b387da77a | |||
| d0724c7d06 | |||
| 1414a3611b |
+18
-11
@@ -2,8 +2,8 @@ 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 (
|
||||
GraphQLException,
|
||||
SpeckleException,
|
||||
SpeckleWarning,
|
||||
)
|
||||
@@ -57,6 +57,7 @@ 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"
|
||||
|
||||
@@ -79,15 +80,17 @@ class SpeckleClient:
|
||||
|
||||
self._init_resources()
|
||||
|
||||
# Check compatibility with the server
|
||||
try:
|
||||
serverInfo = self.server.get()
|
||||
if isinstance(serverInfo, Exception):
|
||||
raise serverInfo
|
||||
if not isinstance(serverInfo, ServerInfo):
|
||||
raise Exception("Couldn't get ServerInfo")
|
||||
except Exception as ex:
|
||||
raise SpeckleException(f"{self.url} is not a compatible Speckle Server", ex)
|
||||
# ? 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} )"
|
||||
@@ -114,6 +117,7 @@ class SpeckleClient:
|
||||
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:
|
||||
@@ -123,9 +127,12 @@ class SpeckleClient:
|
||||
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",
|
||||
@@ -142,7 +149,7 @@ class SpeckleClient:
|
||||
|
||||
self._init_resources()
|
||||
|
||||
if isinstance(self.user.get(), GraphQLException):
|
||||
if self.user.get() is None:
|
||||
warn(
|
||||
SpeckleWarning(
|
||||
f"Possibly invalid token - could not authenticate Speckle Client for server {self.url}"
|
||||
|
||||
+34
-5
@@ -23,7 +23,7 @@ class Commit(BaseModel):
|
||||
authorId: Optional[str]
|
||||
authorAvatar: Optional[str]
|
||||
branchName: Optional[str]
|
||||
createdAt: Optional[str]
|
||||
createdAt: Optional[datetime]
|
||||
sourceApplication: Optional[str]
|
||||
referencedObject: Optional[str]
|
||||
totalChildrenCount: Optional[int]
|
||||
@@ -38,7 +38,7 @@ class Commit(BaseModel):
|
||||
|
||||
class Commits(BaseModel):
|
||||
totalCount: Optional[int]
|
||||
cursor: Optional[Any]
|
||||
cursor: Optional[datetime]
|
||||
items: List[Commit] = []
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ class Object(BaseModel):
|
||||
speckleType: Optional[str]
|
||||
applicationId: Optional[str]
|
||||
totalChildrenCount: Optional[int]
|
||||
createdAt: Optional[str]
|
||||
createdAt: Optional[datetime]
|
||||
|
||||
|
||||
class Branch(BaseModel):
|
||||
@@ -68,8 +68,8 @@ class Stream(BaseModel):
|
||||
name: Optional[str]
|
||||
description: Optional[str]
|
||||
isPublic: Optional[bool]
|
||||
createdAt: Optional[str]
|
||||
updatedAt: Optional[str]
|
||||
createdAt: Optional[datetime]
|
||||
updatedAt: Optional[datetime]
|
||||
collaborators: List[Collaborator] = []
|
||||
branches: Optional[Branches]
|
||||
commit: Optional[Commit]
|
||||
@@ -106,6 +106,35 @@ class User(BaseModel):
|
||||
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()} )"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
|
||||
|
||||
class ServerInfo(BaseModel):
|
||||
name: Optional[str]
|
||||
company: Optional[str]
|
||||
|
||||
@@ -72,7 +72,7 @@ def receive(
|
||||
|
||||
serializer = BaseObjectSerializer(read_transport=local_transport)
|
||||
|
||||
# try local transport first. if the parent is there, we assume all the children are there and continue wth deserialisation using the local transport
|
||||
# try local transport first. if the parent is there, we assume all the children are there and continue with deserialisation using the local transport
|
||||
obj_string = local_transport.get_object(obj_id)
|
||||
if obj_string:
|
||||
return serializer.read_json(obj_string=obj_string)
|
||||
|
||||
@@ -164,7 +164,7 @@ class Resource(ResourceBase):
|
||||
description {str} -- optional: the updated branch description
|
||||
|
||||
Returns:
|
||||
bool -- True if update is successfull
|
||||
bool -- True if update is successful
|
||||
"""
|
||||
metrics.track(metrics.BRANCH, self.account, {"name": "update"})
|
||||
query = gql(
|
||||
|
||||
@@ -2,6 +2,7 @@ from typing import Dict, List
|
||||
from gql import gql
|
||||
from specklepy.api.models import ServerInfo
|
||||
from specklepy.api.resource import ResourceBase
|
||||
from specklepy.logging import metrics
|
||||
|
||||
|
||||
NAME = "server"
|
||||
@@ -26,6 +27,7 @@ class Resource(ResourceBase):
|
||||
Returns:
|
||||
dict -- the server info in dictionary form
|
||||
"""
|
||||
metrics.track(metrics.SERVER, self.account, {"name": "get"})
|
||||
query = gql(
|
||||
"""
|
||||
query Server {
|
||||
@@ -65,6 +67,7 @@ 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 {
|
||||
@@ -98,6 +101,7 @@ 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!) {
|
||||
@@ -123,6 +127,7 @@ 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!) {
|
||||
|
||||
@@ -1,19 +1,14 @@
|
||||
from datetime import datetime, timezone
|
||||
from gql import gql
|
||||
from typing import List
|
||||
from specklepy.logging import metrics
|
||||
from specklepy.api.models import Stream
|
||||
from specklepy.api.models import ActivityCollection, Stream
|
||||
from specklepy.api.resource import ResourceBase
|
||||
from specklepy.logging.exceptions import SpeckleException
|
||||
|
||||
|
||||
NAME = "stream"
|
||||
METHODS = [
|
||||
"list",
|
||||
"create",
|
||||
"get",
|
||||
"update",
|
||||
"delete",
|
||||
"search",
|
||||
]
|
||||
METHODS = ["list", "create", "get", "update", "delete", "search", "activity"]
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
@@ -45,41 +40,41 @@ class Resource(ResourceBase):
|
||||
query = gql(
|
||||
"""
|
||||
query Stream($id: String!, $branch_limit: Int!, $commit_limit: Int!) {
|
||||
stream(id: $id) {
|
||||
id
|
||||
name
|
||||
description
|
||||
isPublic
|
||||
createdAt
|
||||
updatedAt
|
||||
collaborators {
|
||||
id
|
||||
name
|
||||
role
|
||||
avatar
|
||||
}
|
||||
branches(limit: $branch_limit) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
stream(id: $id) {
|
||||
id
|
||||
name
|
||||
description
|
||||
commits(limit: $commit_limit) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
isPublic
|
||||
createdAt
|
||||
updatedAt
|
||||
collaborators {
|
||||
id
|
||||
referencedObject
|
||||
message
|
||||
authorName
|
||||
authorId
|
||||
createdAt
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
@@ -101,34 +96,34 @@ class Resource(ResourceBase):
|
||||
query = gql(
|
||||
"""
|
||||
query User($stream_limit: Int!) {
|
||||
user {
|
||||
id
|
||||
email
|
||||
name
|
||||
bio
|
||||
company
|
||||
avatar
|
||||
verified
|
||||
profiles
|
||||
role
|
||||
streams(limit: $stream_limit) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
user {
|
||||
id
|
||||
email
|
||||
name
|
||||
description
|
||||
isPublic
|
||||
createdAt
|
||||
updatedAt
|
||||
collaborators {
|
||||
id
|
||||
name
|
||||
role
|
||||
}
|
||||
bio
|
||||
company
|
||||
avatar
|
||||
verified
|
||||
profiles
|
||||
role
|
||||
streams(limit: $stream_limit) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
id
|
||||
name
|
||||
description
|
||||
isPublic
|
||||
createdAt
|
||||
updatedAt
|
||||
collaborators {
|
||||
id
|
||||
name
|
||||
role
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
@@ -190,7 +185,7 @@ class Resource(ResourceBase):
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamUpdate($stream: StreamUpdateInput!) {
|
||||
streamUpdate(stream: $stream)
|
||||
streamUpdate(stream: $stream)
|
||||
}
|
||||
"""
|
||||
)
|
||||
@@ -221,9 +216,9 @@ class Resource(ResourceBase):
|
||||
query = gql(
|
||||
"""
|
||||
mutation StreamDelete($id: String!) {
|
||||
streamDelete(id: $id)
|
||||
streamDelete(id: $id)
|
||||
}
|
||||
"""
|
||||
"""
|
||||
)
|
||||
|
||||
params = {"id": id}
|
||||
@@ -254,43 +249,43 @@ class Resource(ResourceBase):
|
||||
query = gql(
|
||||
"""
|
||||
query StreamSearch($search_query: String!,$limit: Int!, $branch_limit:Int!, $commit_limit:Int!) {
|
||||
streams(query: $search_query, limit: $limit) {
|
||||
items {
|
||||
id
|
||||
name
|
||||
description
|
||||
isPublic
|
||||
createdAt
|
||||
updatedAt
|
||||
collaborators {
|
||||
id
|
||||
name
|
||||
role
|
||||
avatar
|
||||
}
|
||||
branches(limit: $branch_limit) {
|
||||
totalCount
|
||||
cursor
|
||||
streams(query: $search_query, limit: $limit) {
|
||||
items {
|
||||
id
|
||||
name
|
||||
description
|
||||
commits(limit: $commit_limit) {
|
||||
totalCount
|
||||
cursor
|
||||
items {
|
||||
id
|
||||
referencedObject
|
||||
message
|
||||
authorName
|
||||
authorId
|
||||
createdAt
|
||||
id
|
||||
name
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
)
|
||||
@@ -368,3 +363,72 @@ class Resource(ResourceBase):
|
||||
return_type="streamRevokePermission",
|
||||
parse_response=False,
|
||||
)
|
||||
|
||||
def activity(
|
||||
self,
|
||||
stream_id: str,
|
||||
action_type: str = None,
|
||||
limit: int = 20,
|
||||
before: datetime = None,
|
||||
after: datetime = None,
|
||||
cursor: 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,
|
||||
)
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
from datetime import datetime, timezone
|
||||
from specklepy.logging import metrics
|
||||
from specklepy.logging.exceptions import SpeckleException
|
||||
from typing import List
|
||||
from gql import gql
|
||||
from specklepy.api.resource import ResourceBase
|
||||
from specklepy.api.models import User
|
||||
from specklepy.api.models import ActivityCollection, User
|
||||
|
||||
NAME = "user"
|
||||
METHODS = ["get", "search", "update"]
|
||||
METHODS = ["get", "search", "update", "activity"]
|
||||
|
||||
|
||||
class Resource(ResourceBase):
|
||||
@@ -125,3 +126,65 @@ class Resource(ResourceBase):
|
||||
return self.make_request(
|
||||
query=query, params=params, return_type="userUpdate", parse_response=False
|
||||
)
|
||||
|
||||
def activity(
|
||||
self,
|
||||
user_id: str = None,
|
||||
limit: int = 20,
|
||||
action_type: str = None,
|
||||
before: datetime = None,
|
||||
after: datetime = None,
|
||||
cursor: 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,
|
||||
)
|
||||
|
||||
@@ -445,7 +445,7 @@ input ServerInfoUpdateInput {
|
||||
stream( id: String! ): Stream
|
||||
|
||||
"""
|
||||
All the streams of the current user, pass in the `query` parameter to seach by name, description or ID.
|
||||
All the streams of the current user, pass in the `query` parameter to search by name, description or ID.
|
||||
"""
|
||||
streams( query: String, limit: Int = 25, cursor: String ): StreamCollection
|
||||
@hasScope(scope: "streams:read")
|
||||
|
||||
@@ -98,6 +98,10 @@ class StreamWrapper:
|
||||
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)
|
||||
@@ -111,7 +115,7 @@ class StreamWrapper:
|
||||
)
|
||||
|
||||
if not self._account:
|
||||
self._account = get_account_from_token(token, self.host)
|
||||
self._account = get_account_from_token(token, self.server_url)
|
||||
|
||||
if self._client:
|
||||
self._client.authenticate_with_account(self._account)
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import json
|
||||
import os
|
||||
import socket
|
||||
import sys
|
||||
import queue
|
||||
@@ -14,6 +12,7 @@ This really helps us to deliver a better open source project and product!
|
||||
"""
|
||||
TRACK = True
|
||||
HOST_APP = "python"
|
||||
HOST_APP_VERSION = f"python {'.'.join(map(str, sys.version_info[:3]))}"
|
||||
PLATFORMS = {"win32": "Windows", "cygwin": "Windows", "darwin": "Mac OS X"}
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
@@ -27,6 +26,8 @@ PERMISSION = "Permission Action"
|
||||
COMMIT = "Commit Action"
|
||||
BRANCH = "Branch Action"
|
||||
USER = "User Action"
|
||||
SERVER = "Server Action"
|
||||
CLIENT = "Speckle Client"
|
||||
STREAM_WRAPPER = "Stream Wrapper"
|
||||
|
||||
ACCOUNTS = "Get Local Accounts"
|
||||
@@ -45,9 +46,10 @@ def enable():
|
||||
TRACK = True
|
||||
|
||||
|
||||
def set_host_app(host_app: str):
|
||||
global HOST_APP
|
||||
def set_host_app(host_app: str, host_app_version: str = None):
|
||||
global HOST_APP, HOST_APP_VERSION
|
||||
HOST_APP = host_app
|
||||
HOST_APP_VERSION = host_app_version or HOST_APP_VERSION
|
||||
|
||||
|
||||
def track(action: str, account: "Account" = None, custom_props: dict = None):
|
||||
@@ -62,6 +64,7 @@ def track(action: str, account: "Account" = None, custom_props: dict = None):
|
||||
"server_id": METRICS_TRACKER.last_server,
|
||||
"token": METRICS_TRACKER.analytics_token,
|
||||
"hostApp": HOST_APP,
|
||||
"hostAppVersion": HOST_APP_VERSION,
|
||||
"$os": METRICS_TRACKER.platform,
|
||||
"type": "action",
|
||||
},
|
||||
|
||||
@@ -9,6 +9,9 @@ from typing import (
|
||||
Type,
|
||||
get_type_hints,
|
||||
)
|
||||
|
||||
import contextlib
|
||||
from enum import EnumMeta
|
||||
from warnings import warn
|
||||
|
||||
from specklepy.logging.exceptions import SpeckleException
|
||||
@@ -248,12 +251,15 @@ class Base(_RegisteringBase):
|
||||
types = getattr(self, "_attr_types", {})
|
||||
t = types.get(name, None)
|
||||
|
||||
if t is None:
|
||||
if t is None or t is Any:
|
||||
return 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 = (
|
||||
@@ -275,13 +281,11 @@ class Base(_RegisteringBase):
|
||||
if isinstance(t, tuple):
|
||||
t = t[0]
|
||||
|
||||
try:
|
||||
with contextlib.suppress(ValueError):
|
||||
if t is float:
|
||||
return float(value)
|
||||
if t is str and value:
|
||||
return str(value)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
raise SpeckleException(
|
||||
f"Cannot set '{self.__class__.__name__}.{name}': it expects type '{t.__name__}', but received type '{type(value).__name__}'"
|
||||
|
||||
@@ -74,7 +74,7 @@ class ObjectArray:
|
||||
|
||||
index = 0
|
||||
while index < len(data):
|
||||
item_length = data[index]
|
||||
item_length = int(data[index])
|
||||
item_start = index + 1
|
||||
item_end = item_start + item_length
|
||||
item_data = data[item_start:item_end]
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from specklepy.objects.geometry import Point
|
||||
from enum import Enum
|
||||
from typing import List
|
||||
from specklepy.objects.geometry import Point
|
||||
|
||||
from .base import Base
|
||||
|
||||
@@ -19,11 +20,19 @@ class FakeGeo(Base, chunkable={"dots": 50}, detachable={"pointslist"}):
|
||||
dots: List[int] = None
|
||||
|
||||
|
||||
class FakeDirection(Enum):
|
||||
NORTH = 1
|
||||
EAST = 2
|
||||
SOUTH = 3
|
||||
WEST = 4
|
||||
|
||||
|
||||
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
|
||||
|
||||
@@ -293,9 +293,9 @@ class Curve(
|
||||
|
||||
@classmethod
|
||||
def from_list(cls, args: List[Any]) -> "Curve":
|
||||
point_count = args[7]
|
||||
weights_count = args[8]
|
||||
knots_count = args[9]
|
||||
point_count = int(args[7])
|
||||
weights_count = int(args[8])
|
||||
knots_count = int(args[9])
|
||||
|
||||
points_start = 10
|
||||
weights_start = 10 + point_count
|
||||
@@ -303,7 +303,7 @@ class Curve(
|
||||
knots_end = knots_start + knots_count
|
||||
|
||||
return cls(
|
||||
degree=args[1],
|
||||
degree=int(args[1]),
|
||||
periodic=bool(args[2]),
|
||||
rational=bool(args[3]),
|
||||
closed=bool(args[4]),
|
||||
@@ -632,7 +632,7 @@ class Brep(
|
||||
bbox: Box = None
|
||||
area: float = None
|
||||
volume: float = None
|
||||
displayValue: Mesh = None
|
||||
_displayValue: List[Mesh] = None
|
||||
Surfaces: List[Surface] = None
|
||||
Curve3D: List[Base] = None
|
||||
Curve2D: List[Base] = None
|
||||
@@ -648,6 +648,18 @@ class Brep(
|
||||
child._Brep = self
|
||||
return children
|
||||
|
||||
# set as prop for now for backwards compatibility
|
||||
@property
|
||||
def displayValue(self) -> List[Mesh]:
|
||||
return self._displayValue
|
||||
|
||||
@displayValue.setter
|
||||
def displayValue(self, value):
|
||||
if isinstance(value, Mesh):
|
||||
self._displayValue = [value]
|
||||
elif isinstance(value, list):
|
||||
self._displayValue = value
|
||||
|
||||
@property
|
||||
def Edges(self) -> List[BrepEdge]:
|
||||
return self._inject_self_into_children(self._Edges)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import List
|
||||
from typing import Any, List
|
||||
from specklepy.objects.geometry import Point, Vector
|
||||
from .base import Base
|
||||
|
||||
@@ -55,10 +55,11 @@ class Transform(
|
||||
def value(self, value: List[float]) -> None:
|
||||
try:
|
||||
value = [float(x) for x in value]
|
||||
except (ValueError, TypeError):
|
||||
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}"
|
||||
)
|
||||
) 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"
|
||||
@@ -196,4 +197,18 @@ class BlockInstance(
|
||||
Base, speckle_type=OTHER + "BlockInstance", detachable={"blockDefinition"}
|
||||
):
|
||||
blockDefinition: BlockDefinition = None
|
||||
transform: Transform = None
|
||||
transform: Transform = None
|
||||
|
||||
|
||||
# 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
|
||||
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
|
||||
)
|
||||
isShared: bool = False
|
||||
isReadOnly: bool = False
|
||||
isTypeParameter: bool = False
|
||||
|
||||
@@ -2,6 +2,7 @@ import ujson
|
||||
import hashlib
|
||||
import re
|
||||
from uuid import uuid4
|
||||
from enum import Enum
|
||||
from warnings import warn
|
||||
from typing import Any, Dict, List, Tuple
|
||||
from specklepy.objects.base import Base, DataChunk
|
||||
@@ -111,6 +112,11 @@ class BaseObjectSerializer:
|
||||
object_builder[prop] = value
|
||||
continue
|
||||
|
||||
# NOTE: for dynamic props, this won't be re-serialised as an enum but as an int
|
||||
if isinstance(value, Enum):
|
||||
object_builder[prop] = value.value
|
||||
continue
|
||||
|
||||
# 2. handle Base objects
|
||||
elif isinstance(value, Base):
|
||||
child_obj = self.traverse_value(value, detach=detach)
|
||||
@@ -182,6 +188,10 @@ class BaseObjectSerializer:
|
||||
if isinstance(obj, PRIMITIVES):
|
||||
return obj
|
||||
|
||||
# NOTE: for dynamic props, this won't be re-serialised as an enum but as an int
|
||||
if isinstance(obj, Enum):
|
||||
return obj.value
|
||||
|
||||
elif isinstance(obj, (list, tuple, set)):
|
||||
if not detach:
|
||||
return [self.traverse_value(o) for o in obj]
|
||||
@@ -215,7 +225,7 @@ class BaseObjectSerializer:
|
||||
except:
|
||||
warn(
|
||||
f"Failed to handle {type(obj)} in `BaseObjectSerializer.traverse_value`",
|
||||
SerializationException,
|
||||
SpeckleWarning,
|
||||
)
|
||||
|
||||
return str(obj)
|
||||
|
||||
+5
-2
@@ -6,7 +6,7 @@ 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 FakeMesh
|
||||
from specklepy.objects.fakemesh import FakeDirection, FakeMesh
|
||||
from specklepy.logging import metrics
|
||||
|
||||
metrics.disable()
|
||||
@@ -81,9 +81,10 @@ def mesh():
|
||||
mesh = FakeMesh()
|
||||
mesh.name = "my_mesh"
|
||||
mesh.vertices = [random.uniform(0, 10) for _ in range(1, 210)]
|
||||
mesh.faces = [i for i 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")
|
||||
@@ -104,6 +105,8 @@ def base():
|
||||
base.units = "millimetres"
|
||||
base.null_val = None
|
||||
base.null_dict = {"a null val": None}
|
||||
base.tuple = (1, 2, "3")
|
||||
base.set = {1, 2, "3"}
|
||||
base.vertices = [random.uniform(0, 10) for _ in range(1, 120)]
|
||||
base.test_bases = [Base(name=i) for i in range(1, 22)]
|
||||
base["@detach"] = Base(name="detached base")
|
||||
|
||||
+12
-1
@@ -1,5 +1,6 @@
|
||||
from contextlib import ExitStack as does_not_raise
|
||||
from enum import Enum
|
||||
from typing import Dict, List, Optional
|
||||
from contextlib import ExitStack as does_not_raise
|
||||
|
||||
import pytest
|
||||
from specklepy.api import operations
|
||||
@@ -95,6 +96,12 @@ def test_base_of_custom_speckle_type() -> None:
|
||||
assert b1.name == "Tweety's Crib"
|
||||
|
||||
|
||||
class DietaryRestrictions(Enum):
|
||||
VEGAN = 1
|
||||
GLUTEN_FREE = 2
|
||||
NUT_FREE = 3
|
||||
|
||||
|
||||
class FrozenYoghurt(Base):
|
||||
"""Testing type checking"""
|
||||
|
||||
@@ -103,6 +110,7 @@ class FrozenYoghurt(Base):
|
||||
customer: str
|
||||
add_ons: Optional[Dict[str, float]] # dict item types won't be checked
|
||||
price: float = 0.0
|
||||
dietary: DietaryRestrictions
|
||||
|
||||
|
||||
def test_type_checking() -> None:
|
||||
@@ -111,6 +119,7 @@ def test_type_checking() -> None:
|
||||
order.servings = 2
|
||||
order.price = "7" # will get converted
|
||||
order.customer = "izzy"
|
||||
order.dietary = DietaryRestrictions.VEGAN
|
||||
|
||||
with pytest.raises(SpeckleException):
|
||||
order.flavours = "not a list"
|
||||
@@ -118,6 +127,8 @@ def test_type_checking() -> None:
|
||||
order.servings = "five"
|
||||
with pytest.raises(SpeckleException):
|
||||
order.add_ons = ["sprinkles"]
|
||||
with pytest.raises(SpeckleException):
|
||||
order.dietary = "no nuts plz"
|
||||
|
||||
order.add_ons = {"sprinkles": 0.2, "chocolate": 1.0}
|
||||
order.flavours = ["strawberry", "lychee", "peach", "pineapple"]
|
||||
|
||||
+17
-2
@@ -1,5 +1,7 @@
|
||||
import pytest
|
||||
from specklepy.api.models import Stream
|
||||
from datetime import datetime
|
||||
from specklepy.api.models import ActivityCollection, Activity, Stream
|
||||
from specklepy.api.client import SpeckleClient
|
||||
from specklepy.logging.exceptions import GraphQLException
|
||||
|
||||
|
||||
@@ -87,9 +89,22 @@ class TestStream:
|
||||
|
||||
fetched_stream = client.stream.get(stream.id)
|
||||
|
||||
assert revoked == True
|
||||
assert revoked is True
|
||||
assert len(fetched_stream.collaborators) == 1
|
||||
|
||||
def test_stream_activity(self, client: SpeckleClient, stream: Stream):
|
||||
activity = client.stream.activity(stream.id)
|
||||
|
||||
older_activity = client.stream.activity(
|
||||
stream.id, before=activity.items[0].time
|
||||
)
|
||||
|
||||
assert isinstance(activity, ActivityCollection)
|
||||
assert isinstance(older_activity, ActivityCollection)
|
||||
assert older_activity.totalCount < activity.totalCount
|
||||
assert activity.items is not None
|
||||
assert isinstance(activity.items[0], Activity)
|
||||
|
||||
def test_stream_delete(self, client, stream):
|
||||
deleted = client.stream.delete(stream.id)
|
||||
|
||||
|
||||
+17
-2
@@ -1,6 +1,7 @@
|
||||
from specklepy.logging.exceptions import SpeckleException
|
||||
from specklepy.api.models import User
|
||||
import pytest
|
||||
from specklepy.api.client import SpeckleClient
|
||||
from specklepy.api.models import Activity, ActivityCollection, User
|
||||
from specklepy.logging.exceptions import SpeckleException
|
||||
|
||||
|
||||
@pytest.mark.run(order=1)
|
||||
@@ -43,3 +44,17 @@ class TestUser:
|
||||
assert isinstance(failed_update, SpeckleException)
|
||||
assert updated is True
|
||||
assert me.bio == bio
|
||||
|
||||
def test_user_activity(self, client: SpeckleClient, second_user_dict):
|
||||
my_activity = client.user.activity(limit=10)
|
||||
their_activity = client.user.activity(second_user_dict["id"])
|
||||
|
||||
assert isinstance(my_activity, ActivityCollection)
|
||||
assert isinstance(my_activity.items[0], Activity)
|
||||
assert my_activity.totalCount > 0
|
||||
assert isinstance(their_activity, ActivityCollection)
|
||||
|
||||
older_activity = client.user.activity(before=my_activity.items[0].time)
|
||||
|
||||
assert isinstance(older_activity, ActivityCollection)
|
||||
assert older_activity.totalCount < my_activity.totalCount
|
||||
|
||||
Reference in New Issue
Block a user