Compare commits

..

7 Commits

Author SHA1 Message Date
izzy lyseggen 409ac68df0 Merge pull request #101 from specklesystems/izzy/json-acct-tweak
Izzy/json acct tweak
2021-06-04 17:37:25 +01:00
izzy lyseggen 81051a87c1 chore: bump version 2021-06-04 17:36:02 +01:00
izzy lyseggen fca386706b fix(credentials): tweak default base path
align with docs
2021-06-04 17:35:25 +01:00
izzy lyseggen 80036b0b98 Merge pull request #100 from specklesystems/izzy/json-accts
feat(credentials): read json files from local accts
2021-06-04 11:42:59 +01:00
izzy lyseggen 92c9a0882e chore: revert yml change 2021-06-04 10:30:20 +01:00
izzy lyseggen 239c466264 chore: bump version 2021-06-04 10:24:09 +01:00
izzy lyseggen 7dd490b24f feat(accounts): read json files from local 2021-06-04 10:23:54 +01:00
3 changed files with 38 additions and 10 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[tool.poetry]
name = "specklepy"
version = "2.2.0"
version = "2.2.2"
description = "The Python SDK for Speckle 2.0"
readme = "README.md"
authors = ["Speckle Systems <devops@speckle.systems>"]
+31 -6
View File
@@ -1,9 +1,9 @@
import os
from typing import List, Optional
from pydantic import BaseModel
from specklepy.api.models import ServerInfo
from specklepy.transports.sqlite import SQLiteTransport
account_storage = SQLiteTransport(scope="Accounts")
from specklepy.logging.exceptions import SpeckleException
class UserInfo(BaseModel):
@@ -28,23 +28,48 @@ class Account(BaseModel):
return self.__repr__()
def get_local_accounts() -> List[Account]:
def get_local_accounts(base_path: 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
"""
account_storage = SQLiteTransport(scope="Accounts", base_path=base_path)
json_path = os.path.join(account_storage._base_path, "Accounts")
os.makedirs(json_path, exist_ok=True)
json_acct_files = [file for file in os.listdir(json_path) if file.endswith(".json")]
accounts = []
res = account_storage.get_all_objects()
return [Account.parse_raw(r[1]) for r in res] if res else []
if res:
accounts.extend(Account.parse_raw(r[1]) for r in res)
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,
)
return accounts
def get_default_account() -> Account:
def get_default_account(base_path: str = None) -> 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()
accounts = get_local_accounts(base_path=base_path)
if not accounts:
return None
+6 -3
View File
@@ -13,6 +13,7 @@ from specklepy.logging.exceptions import SpeckleException
class SQLiteTransport(AbstractTransport):
_name = "SQLite"
_base_path: str = None
_root_path: str = None
_is_writing: bool = False
_scheduler = sched.scheduler(time.time, time.sleep)
@@ -33,11 +34,13 @@ class SQLiteTransport(AbstractTransport):
super().__init__(**data)
self.app_name = app_name or "Speckle"
self.scope = scope or "Objects"
base_path = base_path or self.__get_base_path()
self._base_path = base_path or self.__get_base_path()
os.makedirs(base_path, exist_ok=True)
os.makedirs(self._base_path, exist_ok=True)
self._root_path = os.path.join(os.path.join(base_path, f"{self.scope}.db"))
self._root_path = os.path.join(
os.path.join(self._base_path, f"{self.scope}.db")
)
self.__initialise()
def __repr__(self) -> str: