Skip to content

Commit

Permalink
Reintroduce Configuration.update() (#2147)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra authored Oct 19, 2024
1 parent 01c80be commit 1213805
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
8 changes: 6 additions & 2 deletions betty/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from collections.abc import Callable
from contextlib import chdir
from typing import Generic, TypeVar, TypeAlias, TYPE_CHECKING
from typing import Generic, TypeVar, TypeAlias, TYPE_CHECKING, Self

import aiofiles
from aiofiles.os import makedirs
Expand All @@ -31,7 +31,11 @@ class Configuration(Loadable, Dumpable):
Any configuration object.
"""

pass
def update(self, other: Self) -> None:
"""
Update this configuration with the values from ``other``.
"""
self.load(other.dump())


_ConfigurationT = TypeVar("_ConfigurationT", bound=Configuration)
Expand Down
10 changes: 5 additions & 5 deletions betty/project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,11 @@ async def _init_extensions(self) -> ProjectExtensions:
isinstance(extension, ConfigurableExtension)
and extension_type in self.configuration.extensions
):
# This is a hack because we do not yet have a way to inject configuration into extensions **when
# initializing them**.
extension._configuration = self.configuration.extensions[
extension_type
].extension_configuration
extension.configuration.update(
self.configuration.extensions[
extension_type
].extension_configuration
)
if isinstance(extension, Theme):
theme_count += 1
extensions_batch.append(extension)
Expand Down
2 changes: 1 addition & 1 deletion betty/project/extension/gramps/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class FamilyTreeConfigurationSequence(ConfigurationSequence[FamilyTreeConfigurat
@override
def load_item(self, dump: Dump) -> FamilyTreeConfiguration:
# Use a dummy path to satisfy initializer arguments.
# It will be overridden when loading the fump.
# It will be overridden when loading the dump.
item = FamilyTreeConfiguration(Path())
item.load(dump)
return item
Expand Down
23 changes: 23 additions & 0 deletions betty/tests/config/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,40 @@
import pytest
from typing_extensions import override

from betty.assertion import assert_int
from betty.assertion.error import AssertionFailed
from betty.config import (
Configurable,
assert_configuration_file,
write_configuration_file,
Configuration,
)
from betty.error import FileNotFound
from betty.serde.dump import Dump
from betty.test_utils.config import DummyConfiguration


class TestConfiguration:
class _DummyConfiguration(Configuration):
def __init__(self, value: int):
self.value = value

@override
def load(self, dump: Dump) -> None:
self.value = assert_int()(dump)

@override
def dump(self) -> Dump:
return self.value

def test_update(self) -> None:
sut = self._DummyConfiguration(123)
value = 456
other = self._DummyConfiguration(value)
sut.update(other)
assert sut.value == value


class TestConfigurable:
class _DummyConfigurable(Configurable[DummyConfiguration]):
def __init__(self, configuration: DummyConfiguration | None = None):
Expand Down
3 changes: 0 additions & 3 deletions betty/tests/coverage/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ class MissingReason(Enum):
"__aexit__": MissingReason.SHOULD_BE_COVERED,
},
},
"betty/config/__init__.py": {
"Configuration": MissingReason.ABSTRACT,
},
"betty/config/collections/__init__.py": MissingReason.ABSTRACT,
"betty/contextlib.py": {
"SynchronizedContextManager": {
Expand Down

0 comments on commit 1213805

Please sign in to comment.