Skip to content

Commit

Permalink
Merge pull request bartfeenstra#803 from bartfeenstra/reactives-0.2
Browse files Browse the repository at this point in the history
Upgrade to Reactives 0.2
  • Loading branch information
bartfeenstra authored Feb 17, 2022
2 parents 4c4f72a + 7bb830c commit 02fcb4b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
26 changes: 17 additions & 9 deletions betty/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import aiohttp
from jinja2 import Environment
from reactives import reactive, Scope
from reactives import reactive, scope

from betty.concurrent import ExceptionRaisingExecutor
from betty.dispatch import Dispatcher
Expand All @@ -35,20 +35,20 @@ class _AppExtensions(Extensions):
def __init__(self):
self._extensions = []

@Scope.register_self
@scope.register_self
def __getitem__(self, extension_type: Type[Extension]) -> Extension:
for extension in self.flatten():
if type(extension) == extension_type:
return extension
raise KeyError(f'Unknown extension of type "{extension_type}"')

@Scope.register_self
@scope.register_self
def __iter__(self) -> Sequence[Sequence[Extension]]:
# Use a generator so we discourage calling code from storing the result.
for batch in self._extensions:
yield (extension for extension in batch)

@Scope.register_self
@scope.register_self
def __contains__(self, extension_type: Type[Extension]) -> bool:
for extension in self.flatten():
if type(extension) == extension_type:
Expand Down Expand Up @@ -181,7 +181,7 @@ def _update_extensions(self) -> None:
extensions.append(extensions_batch)
self._extensions._update(extensions)

@reactive(on_trigger=(lambda app: app._assets.paths.clear(),))
@reactive
@property
def assets(self) -> FileSystem:
if len(self._assets.paths) == 0:
Expand All @@ -194,6 +194,10 @@ def assets(self) -> FileSystem:

return self._assets

@assets.deleter
def assets(self) -> None:
self._assets.paths.clear()

@property
def dispatcher(self) -> Dispatcher:
if self._dispatcher is None:
Expand All @@ -211,7 +215,7 @@ def localized_url_generator(self) -> LocalizedUrlGenerator:
def static_url_generator(self) -> StaticUrlGenerator:
return self._static_url_generator

@reactive(on_trigger=(lambda app: app._translations.clear(),))
@reactive
@property
def translations(self) -> Dict[str, NullTranslations]:
if len(self._translations) == 0:
Expand All @@ -225,7 +229,11 @@ def translations(self) -> Dict[str, NullTranslations]:

return self._translations

@reactive(on_trigger=(lambda app: delattr(app, '_jinja2_environment'),))
@translations.deleter
def translations(self) -> None:
self._translations.clear()

@reactive
@property
def jinja2_environment(self) -> Environment:
if not self._jinja2_environment:
Expand All @@ -238,7 +246,7 @@ def jinja2_environment(self) -> Environment:
def jinja2_environment(self) -> None:
self._jinja2_environment = None

@reactive(on_trigger=(lambda app: delattr(app, '_renderer'),))
@reactive
@property
def renderer(self) -> Renderer:
if not self._renderer:
Expand Down Expand Up @@ -270,7 +278,7 @@ def executor(self) -> None:
def locks(self) -> Locks:
return self._locks

@reactive(on_trigger=(lambda app: delattr(app, 'http_client'),))
@reactive
@property
def http_client(self) -> aiohttp.ClientSession:
if self._http_client is None:
Expand Down
26 changes: 13 additions & 13 deletions betty/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import yaml
from babel import parse_locale, Locale
from reactives import reactive, Scope, isreactive
from reactives import reactive, scope, is_reactive
from voluptuous import Schema, All, Required, Invalid, IsDir, Any, Range

from betty import fs
Expand Down Expand Up @@ -59,7 +59,7 @@ def __init__(self, configurations: Optional[Sequence[LocaleConfiguration]] = Non
self._configurations = OrderedDict()
self.replace(configurations)

@Scope.register_self
@scope.register_self
def __getitem__(self, locale: str) -> LocaleConfiguration:
return self._configurations[locale]

Expand All @@ -69,25 +69,25 @@ def __delitem__(self, locale: str) -> None:
del self._configurations[locale]
self.react.trigger()

@Scope.register_self
@scope.register_self
def __iter__(self) -> Iterable[LocaleConfiguration]:
return (configuration for configuration in self._configurations.values())

@Scope.register_self
@scope.register_self
def __len__(self) -> int:
return len(self._configurations)

@Scope.register_self
@scope.register_self
def __eq__(self, other):
if not isinstance(other, LocalesConfiguration):
return NotImplemented
return self._configurations == other._configurations

@Scope.register_self
@scope.register_self
def __contains__(self, item):
return item in self._configurations

@Scope.register_self
@scope.register_self
def __repr__(self):
return '<%s %s>' % (self.__class__.__name__, repr(list(self._configurations.values())))

Expand Down Expand Up @@ -170,14 +170,14 @@ def __init__(self, configurations: Optional[Iterable[ExtensionConfiguration]] =
self.add(configuration)

def _wire(self, value) -> None:
if isreactive(value):
if is_reactive(value):
value.react(self)

def _unwire(self, value) -> None:
if isreactive(value):
if is_reactive(value):
value.react.shutdown(self)

@Scope.register_self
@scope.register_self
def __getitem__(self, extension_type: Type[Extension]) -> ExtensionConfiguration:
return self._configurations[extension_type]

Expand All @@ -187,15 +187,15 @@ def __delitem__(self, extension_type: Type[Extension]) -> None:
del self._configurations[extension_type]
self.react.trigger()

@Scope.register_self
@scope.register_self
def __iter__(self) -> Iterable[ExtensionConfiguration]:
return (configuration for configuration in self._configurations.values())

@Scope.register_self
@scope.register_self
def __len__(self) -> int:
return len(self._configurations)

@Scope.register_self
@scope.register_self
def __eq__(self, other):
if not isinstance(other, ExtensionsConfiguration):
return NotImplemented
Expand Down
12 changes: 10 additions & 2 deletions betty/extension/wikipedia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,28 @@ def __init__(self, *args, **kwargs):
async def post_load(self) -> None:
await self._populator.populate()

@reactive(on_trigger=(lambda wikipedia: setattr(wikipedia, '__retriever', None),))
@reactive
@property
def _retriever(self) -> _Retriever:
if self.__retriever is None:
self.__retriever = _Retriever(self._app.http_client, self._app.configuration.cache_directory_path / self.name())
return self.__retriever

@reactive(on_trigger=(lambda wikipedia: setattr(wikipedia, '__populator', None),))
@_retriever.deleter
def _retriever(self) -> None:
self.__retriever = None

@reactive
@property
def _populator(self) -> _Populator:
if self.__populator is None:
self.__populator = _Populator(self._app, self._retriever)
return self.__populator

@_populator.deleter
def _populator(self) -> None:
self.__populator = None

@property
def filters(self) -> Dict[str, Callable]:
return {
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
'PyQt5 ~= 5.15.6',
'python-resize-image ~= 1.1.20',
'pyyaml ~= 6.0.0',
'reactives ~= 0.1.1',
'reactives ~= 0.2.0',
'voluptuous ~= 0.12.2',
],
'extras_require': {
Expand Down

0 comments on commit 02fcb4b

Please sign in to comment.