diff --git a/pyproject.toml b/pyproject.toml index 4c3ad1b..3c08a38 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,8 @@ name = "py-scopes" version = "3.0.1" description = "Implementation of the unknown 'scopes' paradigm in Python" readme = "README.md" -license = {text = "MIT"} +license = "MIT" +license-files = ["LICENSE"] keywords = ["scopes"] authors = [{name = "Helmut Merz", email = "helmutm@cy55.de"}] @@ -19,7 +20,7 @@ postgres = [ "transaction", "zope.sqlalchemy", ] -app = [ # TODO: rename to zope +zope = [ "python-dotenv", "waitress", "zope.authentication", @@ -28,8 +29,8 @@ app = [ # TODO: rename to zope "zope.traversing", ] api = ["fastapi[standard]"] -auth = ["pyjwt[crypto]", "cryptography", "requests"] -test = ["zope.testrunner"] # TODO: rename to zopetest, or merge with apitest +auth = ["authlib", "pyjwt[crypto]", "cryptography", "requests"] +zopetest = ["zope.testrunner"] apitest = ["pytest", "httpx2"] [tool.setuptools] diff --git a/scopes/api/auth/user.py b/scopes/api/auth/user.py index 989eedc..597636f 100644 --- a/scopes/api/auth/user.py +++ b/scopes/api/auth/user.py @@ -23,6 +23,6 @@ router = APIRouter() @router.get('/users', response_model=list[User]) async def get_users(storage: deps.Storage): users = storage.create(st_user.Users) - result = [tr.toModel(User) for tr in users.query()] + result = [tr.toObject(User) for tr in users.query()] return result diff --git a/scopes/shape.py b/scopes/shape.py index cf60c52..0c66295 100644 --- a/scopes/shape.py +++ b/scopes/shape.py @@ -47,7 +47,11 @@ class Record: def __repr__(self): return f'<{self.__class__.__name__}: {self.headValues}, {self.data}>' - def asDict(self): + def asDict(self, flatten=False): + if flatten: + return self.head | self.data return dict(head=self.head, data=self.data) + def toObject(self, cls): + return cls(**self.asDict(flatten=True)) diff --git a/scopes/storage/tracking.py b/scopes/storage/tracking.py index 07852bd..544df9c 100644 --- a/scopes/storage/tracking.py +++ b/scopes/storage/tracking.py @@ -25,16 +25,12 @@ class Record(shape.Record): self.trackId = trackId self.container = container - def asDict(self): - return dict(uid=self.uid, head=self.head, data=self.data, - timeStamp=str(self.timeStamp)[:19]) - - def toModel(self, cls): - data = dict(uid=self.uid) - data.update(self.head) - for k, v in self.data.items(): - data[k] = v - return cls(**data) + def asDict(self, flatten=False): + ts = str(self.timeStamp)[:19] + base = dict(uid=self.uid, timeStamp=ts) + if flatten: + return base | self.head | self.data + return base | super().asDict() @property def uid(self): diff --git a/tests/test_core.py b/tests/test_core.py index 697cd53..de8f298 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -57,6 +57,12 @@ def test_message(): assert msg.head['class'] is k.person assert msg.addr() == (k.test, k.person, '4711') assert msg.categ() == (k.test, k.person) + assert msg.asDict() == {'head': {'domain': k.test, 'action': k.data, + 'class': k.person, 'item': '4711'}, + 'data': {'firstName': 'Bob', 'lastName': 'Miller'}} + assert msg.asDict(flatten=True) == { + 'domain': k.test, 'action': k.data, 'class': k.person, 'item': '4711', + 'firstName': 'Bob', 'lastName': 'Miller'} @pytest.fixture