From e3347aec2dbb82598bf1336e740537117c75fafa Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Wed, 31 May 2023 18:47:48 +0300 Subject: [PATCH] use cached property instead of custom __getattr__ implementation --- CONTRIBUTING.md | 3 ++ package/share/ahriman/settings/ahriman.ini | 2 +- src/ahriman/application/handlers/validate.py | 2 +- src/ahriman/core/alpm/pacman.py | 36 ++++++------------- src/ahriman/core/log/lazy_logging.py | 33 ++++++----------- src/ahriman/core/triggers/trigger.py | 8 +++-- .../handlers/test_handler_validate.py | 3 ++ tests/testresources/core/ahriman.ini | 2 +- 8 files changed, 36 insertions(+), 53 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ff76eaf1..0db6c363 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -102,6 +102,9 @@ Again, the most checks can be performed by `make check` command, though some add @property def property(self) -> Any: ... + + @cached_property + def property_cached(self) -> Any: ... # cached property has to be treated as normal one @classmethod def class_method(cls) -> Self: ... diff --git a/package/share/ahriman/settings/ahriman.ini b/package/share/ahriman/settings/ahriman.ini index 0588bdc6..b43f5551 100644 --- a/package/share/ahriman/settings/ahriman.ini +++ b/package/share/ahriman/settings/ahriman.ini @@ -25,7 +25,7 @@ ignore_packages = makechrootpkg_flags = makepkg_flags = --nocolor --ignorearch triggers = ahriman.core.gitremote.RemotePullTrigger ahriman.core.report.ReportTrigger ahriman.core.upload.UploadTrigger ahriman.core.gitremote.RemotePushTrigger -triggers_known = ahriman.core.support.KeyringTrigger ahriman.core.support.MirrorlistTrigger +triggers_known = ahriman.core.gitremote.RemotePullTrigger ahriman.core.gitremote.RemotePushTrigger ahriman.core.report.ReportTrigger ahriman.core.upload.UploadTrigger ahriman.core.support.KeyringTrigger ahriman.core.support.MirrorlistTrigger vcs_allowed_age = 604800 [repository] diff --git a/src/ahriman/application/handlers/validate.py b/src/ahriman/application/handlers/validate.py index 857db3a5..8ba076c3 100644 --- a/src/ahriman/application/handlers/validate.py +++ b/src/ahriman/application/handlers/validate.py @@ -80,7 +80,7 @@ class Validate(Handler): loader = TriggerLoader() triggers = loader.selected_triggers(configuration) + loader.known_triggers(configuration) - for trigger in triggers: + for trigger in set(triggers): try: trigger_class = loader.load_trigger_class(trigger) except ExtensionError: diff --git a/src/ahriman/core/alpm/pacman.py b/src/ahriman/core/alpm/pacman.py index 03657b54..461a980e 100644 --- a/src/ahriman/core/alpm/pacman.py +++ b/src/ahriman/core/alpm/pacman.py @@ -20,9 +20,9 @@ import shutil from collections.abc import Callable, Generator +from functools import cached_property from pathlib import Path from pyalpm import DB, Handle, Package, SIG_PACKAGE, error as PyalpmError # type: ignore[import] -from typing import Any from ahriman.core.configuration import Configuration from ahriman.core.log import LazyLogging @@ -34,13 +34,8 @@ from ahriman.models.repository_paths import RepositoryPaths class Pacman(LazyLogging): """ alpm wrapper - - Attributes: - handle(Handle): pyalpm root ``Handle`` """ - handle: Handle - def __init__(self, architecture: str, configuration: Configuration, *, refresh_database: PacmanSynchronization) -> None: """ @@ -84,6 +79,16 @@ class Pacman(LazyLogging): return handle + @cached_property + def handle(self) -> Handle: + """ + pyalpm handle + + Returns: + Handle: generated pyalpm handle instance + """ + return self.__create_handle_fn() + def database_copy(self, handle: Handle, database: DB, pacman_root: Path, paths: RepositoryPaths, *, use_ahriman_cache: bool) -> None: """ @@ -184,22 +189,3 @@ class Pacman(LazyLogging): result.update(trim_package(provides) for provides in package.provides) return result - - def __getattr__(self, item: str) -> Any: - """ - pacman handle extractor - - Args: - item(str): property name - - Returns: - Any: attribute by its name - - Raises: - AttributeError: in case if no such attribute found - """ - if item == "handle": - handle = self.__create_handle_fn() - setattr(self, item, handle) - return handle - return super().__getattr__(item) # required for logging attribute diff --git a/src/ahriman/core/log/lazy_logging.py b/src/ahriman/core/log/lazy_logging.py index 1ad9461e..feeedb4f 100644 --- a/src/ahriman/core/log/lazy_logging.py +++ b/src/ahriman/core/log/lazy_logging.py @@ -21,18 +21,24 @@ import contextlib import logging from collections.abc import Generator +from functools import cached_property from typing import Any class LazyLogging: """ wrapper for the logger library inspired by scala lazy logging module - - Attributes: - logger(logging.Logger): class logger instance """ - logger: logging.Logger + @cached_property + def logger(self) -> logging.Logger: + """ + get class logger instance + + Returns: + logging.Logger: class logger instance + """ + return logging.getLogger(self.logger_name) @property def logger_name(self) -> str: @@ -89,22 +95,3 @@ class LazyLogging: yield finally: self._package_logger_reset() - - def __getattr__(self, item: str) -> Any: - """ - logger extractor - - Args: - item(str): property name - - Returns: - Any: attribute by its name - - Raises: - AttributeError: in case if no such attribute found - """ - if item == "logger": - logger = logging.getLogger(self.logger_name) - setattr(self, item, logger) - return logger - raise AttributeError(f"'{self.__class__.__qualname__}' object has no attribute '{item}'") diff --git a/src/ahriman/core/triggers/trigger.py b/src/ahriman/core/triggers/trigger.py index 6442097c..b443c513 100644 --- a/src/ahriman/core/triggers/trigger.py +++ b/src/ahriman/core/triggers/trigger.py @@ -17,6 +17,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . # +from collections.abc import Callable + from ahriman.core.configuration import Configuration from ahriman.core.configuration.schema import ConfigurationSchema from ahriman.core.log import LazyLogging @@ -128,8 +130,10 @@ class Trigger(LazyLogging): result(Result): build result packages(list[Package]): list of all available packages """ - if (run := getattr(self, "run", None)) is not None: - run(result, packages) # compatibility with old triggers + # compatibility with old triggers + run: Callable[[Result, list[Package]], None] | None = getattr(self, "run", None) + if run is not None: + run(result, packages) def on_start(self) -> None: """ diff --git a/tests/ahriman/application/handlers/test_handler_validate.py b/tests/ahriman/application/handlers/test_handler_validate.py index 3bfb7d0d..bcea1e64 100644 --- a/tests/ahriman/application/handlers/test_handler_validate.py +++ b/tests/ahriman/application/handlers/test_handler_validate.py @@ -61,11 +61,14 @@ def test_schema(configuration: Configuration) -> None: assert schema.pop("console") assert schema.pop("email") assert schema.pop("github") + assert schema.pop("gitremote") assert schema.pop("html") assert schema.pop("keyring") assert schema.pop("keyring_generator") assert schema.pop("mirrorlist") assert schema.pop("mirrorlist_generator") + assert schema.pop("remote-pull") + assert schema.pop("remote-push") assert schema.pop("report") assert schema.pop("rsync") assert schema.pop("s3") diff --git a/tests/testresources/core/ahriman.ini b/tests/testresources/core/ahriman.ini index ad35c8aa..57b632fa 100644 --- a/tests/testresources/core/ahriman.ini +++ b/tests/testresources/core/ahriman.ini @@ -25,7 +25,7 @@ ignore_packages = makechrootpkg_flags = makepkg_flags = --skippgpcheck triggers = ahriman.core.report.ReportTrigger ahriman.core.upload.UploadTrigger -triggers_known = ahriman.core.support.KeyringTrigger ahriman.core.support.MirrorlistTrigger +triggers_known = ahriman.core.gitremote.RemotePullTrigger ahriman.core.gitremote.RemotePushTrigger ahriman.core.report.ReportTrigger ahriman.core.upload.UploadTrigger ahriman.core.support.KeyringTrigger ahriman.core.support.MirrorlistTrigger [repository] name = aur-clone