Skip to content

Commit

Permalink
Make Configuration.load() an instance method
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra committed Jun 21, 2024
1 parent 18a0e5c commit b400c8a
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 306 deletions.
12 changes: 2 additions & 10 deletions betty/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,25 +151,17 @@ def update(self, other: Self) -> None:
self._dispatch_change()

@override
@classmethod
def load(
cls,
dump: Dump,
configuration: Self | None = None,
) -> Self:
if configuration is None:
configuration = cls()
def load(self, dump: Dump) -> None:
asserter = Asserter()
asserter.assert_record(
Fields(
OptionalField(
"locale",
AssertionChain(asserter.assert_str())
| asserter.assert_setattr(configuration, "locale"),
| asserter.assert_setattr(self, "locale"),
),
),
)(dump)
return configuration

@override
def dump(self) -> VoidableDump:
Expand Down
70 changes: 15 additions & 55 deletions betty/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from betty.serde.dump import Dumpable, Dump, minimize, VoidableDump, Void
from betty.serde.error import SerdeErrorCollection
from betty.serde.format import FormatRepository
from betty.serde.load import Asserter, Assertion
from betty.serde.load import Asserter

if TYPE_CHECKING:
from _weakref import ReferenceType
Expand Down Expand Up @@ -99,32 +99,11 @@ def update(self, other: Self) -> None:
"""
raise NotImplementedError(repr(self))

@classmethod
def load(
cls,
dump: Dump,
configuration: Self | None = None,
) -> Self:
"""
Load dumped configuration into a new configuration instance.
"""
raise NotImplementedError(repr(cls))

@classmethod
def assert_load(
cls: type[ConfigurationT], configuration: ConfigurationT | None = None
) -> Assertion[Dump, ConfigurationT]:
def load(self, dump: Dump) -> None:
"""
Assert that the dumped configuration can be loaded.
Load dumped configuration.
"""

def _assert_load(dump: Dump) -> ConfigurationT:
return cls.load(dump, configuration)

_assert_load.__qualname__ = (
f"{_assert_load.__qualname__} for {cls.__module__}.{cls.__qualname__}.load"
)
return _assert_load
raise NotImplementedError(repr(self))


ConfigurationT = TypeVar("ConfigurationT", bound=Configuration)
Expand Down Expand Up @@ -380,9 +359,8 @@ def to_keys(self, *indices: int | slice) -> Iterator[ConfigurationKeyT]:
for index in sorted(unique_indices):
yield self.to_key(index)

@classmethod
def _item_type(cls) -> type[ConfigurationT]:
raise NotImplementedError(repr(cls))
def _item_type(self) -> type[ConfigurationT]:
raise NotImplementedError(repr(self))

def keys(self) -> Iterator[ConfigurationKeyT]:
"""
Expand Down Expand Up @@ -495,20 +473,11 @@ def update(self, other: Self) -> None:
self.append(*other)

@override
@classmethod
def load(
cls,
dump: Dump,
configuration: Self | None = None,
) -> Self:
if configuration is None:
configuration = cls()
else:
configuration._clear_without_dispatch()
def load(self, dump: Dump) -> None:
self._clear_without_dispatch()
asserter = Asserter()
with SerdeErrorCollection().assert_valid():
configuration.append(*asserter.assert_sequence(cls._item_type().load)(dump))
return configuration
self.append(*asserter.assert_sequence(self._item_type().load)(dump))

@override
def dump(self) -> VoidableDump:
Expand Down Expand Up @@ -652,21 +621,13 @@ def replace(self, *values: ConfigurationT) -> None:
self.move_to_beginning(*other_keys)

@override
@classmethod
def load(
cls,
dump: Dump,
configuration: Self | None = None,
) -> Self:
if configuration is None:
configuration = cls()
def load(self, dump: Dump) -> None:
asserter = Asserter()
dict_dump = asserter.assert_dict()(dump)
mapping = asserter.assert_mapping(cls._item_type().load)(
{key: cls._load_key(value, key) for key, value in dict_dump.items()}
mapping = asserter.assert_mapping(self._item_type().load)(
{key: self._load_key(value, key) for key, value in dict_dump.items()}
)
configuration.replace(*mapping.values())
return configuration
self.replace(*mapping.values())

@override
def dump(self) -> VoidableDump:
Expand Down Expand Up @@ -758,13 +719,12 @@ def _move_by_offset(
def _get_key(self, configuration: ConfigurationT) -> ConfigurationKeyT:
raise NotImplementedError(repr(self))

@classmethod
def _load_key(
cls,
self,
item_dump: Dump,
key_dump: str,
) -> Dump:
raise NotImplementedError(repr(cls))
raise NotImplementedError(repr(self))

def _dump_key(self, item_dump: VoidableDump) -> tuple[VoidableDump, str]:
raise NotImplementedError(repr(self))
Expand Down
46 changes: 9 additions & 37 deletions betty/extension/cotton_candy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,9 @@ def update(self, other: Self) -> None:
self.hex = other.hex

@override
@classmethod
def load(
cls,
dump: Dump,
configuration: Self | None = None,
) -> Self:
def load(self, dump: Dump) -> None:
asserter = Asserter()
hex_value = asserter.assert_str()(dump)
if configuration is None:
configuration = cls(hex_value)
else:
configuration.hex = hex_value
return configuration
self.hex = asserter.assert_str()(dump)

@override
def dump(self) -> VoidableDump:
Expand Down Expand Up @@ -193,55 +183,37 @@ def logo(self, logo: Path | None) -> None:
self._dispatch_change()

@override
@classmethod
def load(
cls,
dump: Dump,
configuration: Self | None = None,
) -> Self:
if configuration is None:
configuration = cls()
def load(self, dump: Dump) -> None:
asserter = Asserter()
asserter.assert_record(
Fields(
OptionalField(
"featured_entities",
configuration._featured_entities.assert_load(
configuration._featured_entities
),
self.featured_entities.load,
),
OptionalField(
"primary_inactive_color",
configuration._primary_inactive_color.assert_load(
configuration._primary_inactive_color
),
self._primary_inactive_color.load,
),
OptionalField(
"primary_active_color",
configuration._primary_active_color.assert_load(
configuration._primary_active_color
),
self._primary_active_color.load,
),
OptionalField(
"link_inactive_color",
configuration._link_inactive_color.assert_load(
configuration._link_inactive_color
),
self._link_inactive_color.load,
),
OptionalField(
"link_active_color",
configuration._link_active_color.assert_load(
configuration._link_active_color
),
self._link_active_color.load,
),
OptionalField(
"logo",
AssertionChain(asserter.assert_path())
| asserter.assert_setattr(configuration, "logo"),
| asserter.assert_setattr(self, "logo"),
),
)
)(dump)
return configuration

@override
def dump(self) -> VoidableDump:
Expand Down
30 changes: 5 additions & 25 deletions betty/extension/gramps/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,17 @@ def file_path(self, file_path: Path | None) -> None:
self._file_path = file_path

@override
@classmethod
def load(
cls,
dump: Dump,
configuration: Self | None = None,
) -> Self:
if configuration is None:
configuration = cls()
def load(self, dump: Dump) -> None:
asserter = Asserter()
asserter.assert_record(
Fields(
RequiredField(
"file",
AssertionChain(asserter.assert_path())
| asserter.assert_setattr(configuration, "file_path"),
| asserter.assert_setattr(self, "file_path"),
),
)
)(dump)
return configuration

@override
def dump(self) -> VoidableDump:
Expand All @@ -89,8 +81,7 @@ class FamilyTreeConfigurationSequence(ConfigurationSequence[FamilyTreeConfigurat
"""

@override
@classmethod
def _item_type(cls) -> type[FamilyTreeConfiguration]:
def _item_type(self) -> type[FamilyTreeConfiguration]:
return FamilyTreeConfiguration


Expand Down Expand Up @@ -120,24 +111,13 @@ def update(self, other: Self) -> None:
self._family_trees.update(other._family_trees)

@override
@classmethod
def load(
cls,
dump: Dump,
configuration: Self | None = None,
) -> Self:
if configuration is None:
configuration = cls()
def load(self, dump: Dump) -> None:
asserter = Asserter()
asserter.assert_record(
Fields(
OptionalField(
"family_trees",
configuration._family_trees.assert_load(configuration.family_trees),
),
OptionalField("family_trees", self.family_trees.load),
)
)(dump)
return configuration

@override
def dump(self) -> VoidableDump:
Expand Down
14 changes: 3 additions & 11 deletions betty/extension/nginx/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,7 @@ def update(self, other: Self) -> None:
self._dispatch_change()

@override
@classmethod
def load(
cls,
dump: Dump,
configuration: Self | None = None,
) -> Self:
if configuration is None:
configuration = cls()
def load(self, dump: Dump) -> None:
asserter = Asserter()
asserter.assert_record(
Fields(
Expand All @@ -76,16 +69,15 @@ def load(
asserter.assert_bool(), asserter.assert_none()
)
)
| asserter.assert_setattr(configuration, "https"),
| asserter.assert_setattr(self, "https"),
),
OptionalField(
"www_directory_path",
AssertionChain(asserter.assert_str())
| asserter.assert_setattr(configuration, "www_directory_path"),
| asserter.assert_setattr(self, "www_directory_path"),
),
)
)(dump)
return configuration

@override
def dump(self) -> VoidableDump:
Expand Down
12 changes: 2 additions & 10 deletions betty/extension/wikipedia/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,17 @@ def update(self, other: Self) -> None:
self._dispatch_change()

@override
@classmethod
def load(
cls,
dump: Dump,
configuration: Self | None = None,
) -> Self:
if configuration is None:
configuration = cls()
def load(self, dump: Dump) -> None:
asserter = Asserter()
asserter.assert_record(
Fields(
OptionalField(
"populate_images",
AssertionChain(asserter.assert_bool())
| asserter.assert_setattr(configuration, "populate_images"),
| asserter.assert_setattr(self, "populate_images"),
),
)
)(dump)
return configuration

@override
def dump(self) -> VoidableDump:
Expand Down
Loading

0 comments on commit b400c8a

Please sign in to comment.