Skip to content

Commit

Permalink
refactor: replace DataViewMapping with DataModelMapping
Browse files Browse the repository at this point in the history
Signed-off-by: Oz Tiram <oz.tiram@gmail.com>
  • Loading branch information
oz123 committed Apr 19, 2024
1 parent 2903248 commit da1c4b6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/plette/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
__all__ = [
"DataView", "DataViewCollection", "DataViewMapping", "DataModelSequence",
"validate", "ValidationError",
"DataView", "DataModelCollection", "DataModelMapping", "DataModelSequence",
"validate", "DataValidationError",
"Hash", "Package", "Requires", "Source", "Script",
"Meta", "PackageCollection", "ScriptCollection", "SourceCollection",
]

from .base import (
DataView, DataViewCollection, DataViewMapping, DataModelSequence,
validate, ValidationError,
DataView, DataModelCollection, DataModelMapping, DataModelSequence,
validate, DataValidationError,
)

from .hashes import Hash
Expand Down
26 changes: 26 additions & 0 deletions src/plette/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def items(self):
return [(k, self[k]) for k in self._data]



class DataViewSequence(DataViewCollection):
"""A sequence of data views.
Expand Down Expand Up @@ -268,3 +269,28 @@ def append(self, value):
if isinstance(value, DataView):
value = value._data
self._data.append(value)


class DataModelMapping(DataModelCollection):
"""A mapping of data views.
The keys are primitive values, while values are instances of `item_class`.
"""

@classmethod
def validate(cls, data):
for d in data.values():
cls.item_class.validate(d)

def __iter__(self):
return iter(self._data)

def keys(self):
return self._data.keys()

def values(self):
return [self[k] for k in self._data]

def items(self):
return [(k, self[k]) for k in self._data]

1 change: 0 additions & 1 deletion src/plette/models/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,3 @@ def __setattr__(self, key, value):
self._data = value
else:
self._data[key] = value

6 changes: 3 additions & 3 deletions src/plette/models/sections.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from .base import DataModel, DataViewMapping, DataModelSequence
from .base import DataModel, DataModelSequence, DataModelMapping
from .hashes import Hash
from .packages import Package
from .scripts import Script
from .sources import Source


class PackageCollection(DataViewMapping):
class PackageCollection(DataModelMapping):
item_class = Package


class ScriptCollection(DataViewMapping):
class ScriptCollection(DataModelMapping):
item_class = Script


Expand Down

0 comments on commit da1c4b6

Please sign in to comment.