Skip to content

Commit

Permalink
Merge pull request #228 from specklesystems/gergo/noDiskAccess
Browse files Browse the repository at this point in the history
make sure specklepy send works completely without disk write access
  • Loading branch information
gjedlicska authored Nov 1, 2022
2 parents aec94f8 + 8824835 commit b50e658
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 35 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "specklepy"
version = "2.4.0"
version = "2.9.1"
description = "The Python SDK for Speckle 2.0"
readme = "README.md"
authors = ["Speckle Systems <devops@speckle.systems>"]
Expand Down
45 changes: 29 additions & 16 deletions src/specklepy/api/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
from specklepy.api.models import ServerInfo
from specklepy.transports.sqlite import SQLiteTransport
from specklepy.logging.exceptions import SpeckleException
from specklepy import paths


class UserInfo(BaseModel):
name: Optional[str]
email: Optional[str]
company: Optional[str]
id: Optional[str]
name: Optional[str] = None
email: Optional[str] = None
company: Optional[str] = None
id: Optional[str] = None


class Account(BaseModel):
Expand All @@ -35,7 +36,7 @@ def from_token(cls, token: str, server_url: str = None):
return acct


def get_local_accounts(base_path: str = None) -> List[Account]:
def get_local_accounts(base_path: Optional[str] = None) -> List[Account]:
"""Gets all the accounts present in this environment
Arguments:
Expand All @@ -44,18 +45,30 @@ def get_local_accounts(base_path: str = None) -> List[Account]:
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)
# pylint: disable=protected-access
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: List[Account] = []
res = account_storage.get_all_objects()
account_storage.close()
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 res:
accounts.extend(Account.parse_raw(r[1]) for r in res)
if json_acct_files:
try:
accounts.extend(
Expand All @@ -79,7 +92,7 @@ def get_local_accounts(base_path: str = None) -> List[Account]:
return accounts


def get_default_account(base_path: str = None) -> Account:
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
Expand Down
20 changes: 10 additions & 10 deletions src/specklepy/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ def __str__(self) -> str:


class ServerInfo(BaseModel):
name: Optional[str]
company: Optional[str]
url: Optional[str]
description: Optional[str]
adminContact: Optional[str]
canonicalUrl: Optional[str]
roles: Optional[List[dict]]
scopes: Optional[List[dict]]
authStrategies: Optional[List[dict]]
version: Optional[str]
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
2 changes: 1 addition & 1 deletion src/specklepy/transports/abstract_transport.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC, abstractmethod
from typing import Optional, List, Dict
from pydantic import BaseModel
from pydantic.main import Extra
from pydantic.config import Extra

# __________________
# | |
Expand Down
10 changes: 3 additions & 7 deletions src/specklepy/transports/sqlite.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import os
import sys
import time
import sched
import sqlite3
from typing import Any, List, Dict, Tuple
from appdirs import user_data_dir
from typing import Any, List, Dict, Optional, Tuple
from contextlib import closing
from specklepy.transports.abstract_transport import AbstractTransport
from specklepy.logging.exceptions import SpeckleException
Expand All @@ -25,8 +21,8 @@ class SQLiteTransport(AbstractTransport):

def __init__(
self,
base_path: str = None,
app_name: str = None,
base_path: Optional[str] = None,
app_name: Optional[str] = None,
scope: str = None,
max_batch_size_mb: float = 10.0,
**data: Any,
Expand Down

0 comments on commit b50e658

Please sign in to comment.