diff --git a/src/ahriman/application/application/application.py b/src/ahriman/application/application/application.py index 2a2baed8..ff5c4ef0 100644 --- a/src/ahriman/application/application/application.py +++ b/src/ahriman/application/application/application.py @@ -105,8 +105,7 @@ class Application(ApplicationPackages, ApplicationRepository): tree = Tree.resolve(packages) for level in tree: for package in level: - UpdatePrinter(package, local_versions.get(package.base)).print( - verbose=True, log_fn=log_fn, separator=" -> ") + UpdatePrinter(package, local_versions.get(package.base))(verbose=True, log_fn=log_fn, separator=" -> ") def with_dependencies(self, packages: list[Package], *, process_dependencies: bool) -> list[Package]: """ diff --git a/src/ahriman/application/handlers/dump.py b/src/ahriman/application/handlers/dump.py index 15533059..9b455bd7 100644 --- a/src/ahriman/application/handlers/dump.py +++ b/src/ahriman/application/handlers/dump.py @@ -45,11 +45,11 @@ class Dump(Handler): report(bool): force enable or disable reporting """ root, _ = configuration.check_loaded() - ConfigurationPathsPrinter(root, configuration.includes).print(verbose=True, separator=" = ") + ConfigurationPathsPrinter(root, configuration.includes)(verbose=True, separator=" = ") # empty line - StringPrinter("").print(verbose=False) + StringPrinter("")(verbose=False) dump = configuration.dump() for section, values in sorted(dump.items()): - ConfigurationPrinter(section, values).print(verbose=not args.secure, separator=" = ") + ConfigurationPrinter(section, values)(verbose=not args.secure, separator=" = ") diff --git a/src/ahriman/application/handlers/patch.py b/src/ahriman/application/handlers/patch.py index 9121a236..1cdba013 100644 --- a/src/ahriman/application/handlers/patch.py +++ b/src/ahriman/application/handlers/patch.py @@ -133,7 +133,7 @@ class Patch(Handler): Patch.check_if_empty(exit_code, not patches) for base, patch in patches.items(): - PatchPrinter(base, patch).print(verbose=True, separator=" = ") + PatchPrinter(base, patch)(verbose=True, separator=" = ") @staticmethod def patch_set_remove(application: Application, package_base: str, variables: list[str] | None) -> None: diff --git a/src/ahriman/application/handlers/remove_unknown.py b/src/ahriman/application/handlers/remove_unknown.py index f6f01f36..ea249f55 100644 --- a/src/ahriman/application/handlers/remove_unknown.py +++ b/src/ahriman/application/handlers/remove_unknown.py @@ -49,7 +49,7 @@ class RemoveUnknown(Handler): if args.dry_run: for package in sorted(unknown_packages): - StringPrinter(package).print(verbose=False) + StringPrinter(package)(verbose=False) return application.remove(unknown_packages) diff --git a/src/ahriman/application/handlers/search.py b/src/ahriman/application/handlers/search.py index fd1df635..e8f8baab 100644 --- a/src/ahriman/application/handlers/search.py +++ b/src/ahriman/application/handlers/search.py @@ -68,7 +68,7 @@ class Search(Handler): for packages_list in (official_packages_list, aur_packages_list): # keep sorting by packages source for package in Search.sort(packages_list, args.sort_by): - AurPrinter(package).print(verbose=args.info) + AurPrinter(package)(verbose=args.info) @staticmethod def sort(packages: Iterable[AURPackage], sort_by: str) -> list[AURPackage]: diff --git a/src/ahriman/application/handlers/service_updates.py b/src/ahriman/application/handlers/service_updates.py index f45b7599..0c7bf7d5 100644 --- a/src/ahriman/application/handlers/service_updates.py +++ b/src/ahriman/application/handlers/service_updates.py @@ -59,5 +59,5 @@ class ServiceUpdates(Handler): if same_version: return - UpdatePrinter(remote, local_version).print(verbose=True, separator=" -> ") + UpdatePrinter(remote, local_version)(verbose=True, separator=" -> ") ServiceUpdates.check_if_empty(args.exit_code, not same_version) diff --git a/src/ahriman/application/handlers/shell.py b/src/ahriman/application/handlers/shell.py index d3d026fb..69f242d4 100644 --- a/src/ahriman/application/handlers/shell.py +++ b/src/ahriman/application/handlers/shell.py @@ -51,7 +51,7 @@ class Shell(Handler): if args.verbose: # licensed by https://creativecommons.org/licenses/by-sa/3.0 path = Path(sys.prefix) / "share" / "ahriman" / "templates" / "shell" - StringPrinter(path.read_text(encoding="utf8")).print(verbose=False) + StringPrinter(path.read_text(encoding="utf8"))(verbose=False) local_variables = { "architecture": repository_id.architecture, diff --git a/src/ahriman/application/handlers/status.py b/src/ahriman/application/handlers/status.py index 6700ca36..7b8b0927 100644 --- a/src/ahriman/application/handlers/status.py +++ b/src/ahriman/application/handlers/status.py @@ -53,7 +53,7 @@ class Status(Handler): client = Application(repository_id, configuration, report=True).repository.reporter if args.ahriman: service_status = client.status_get() - StatusPrinter(service_status.status).print(verbose=args.info) + StatusPrinter(service_status.status)(verbose=args.info) if args.package: packages: list[tuple[Package, BuildStatus]] = sum( (client.package_get(base) for base in args.package), @@ -67,4 +67,4 @@ class Status(Handler): filter_fn: Callable[[tuple[Package, BuildStatus]], bool] =\ lambda item: args.status is None or item[1].status == args.status for package, package_status in sorted(filter(filter_fn, packages), key=comparator): - PackagePrinter(package, package_status).print(verbose=args.info) + PackagePrinter(package, package_status)(verbose=args.info) diff --git a/src/ahriman/application/handlers/structure.py b/src/ahriman/application/handlers/structure.py index cc7d5bb4..23c3424e 100644 --- a/src/ahriman/application/handlers/structure.py +++ b/src/ahriman/application/handlers/structure.py @@ -50,11 +50,11 @@ class Structure(Handler): partitions = Tree.partition(application.repository.packages(), count=args.partitions) for partition_id, partition in enumerate(partitions): - StringPrinter(f"partition #{partition_id}").print(verbose=False) + StringPrinter(f"partition #{partition_id}")(verbose=False) tree = Tree.resolve(partition) for num, level in enumerate(tree): - TreePrinter(num, level).print(verbose=True, separator=" ") + TreePrinter(num, level)(verbose=True, separator=" ") # empty line - StringPrinter("").print(verbose=False) + StringPrinter("")(verbose=False) diff --git a/src/ahriman/application/handlers/unsafe_commands.py b/src/ahriman/application/handlers/unsafe_commands.py index 68c85c85..fe87bc47 100644 --- a/src/ahriman/application/handlers/unsafe_commands.py +++ b/src/ahriman/application/handlers/unsafe_commands.py @@ -50,7 +50,7 @@ class UnsafeCommands(Handler): UnsafeCommands.check_unsafe(args.command, unsafe_commands, parser) else: for command in unsafe_commands: - StringPrinter(command).print(verbose=True) + StringPrinter(command)(verbose=True) @staticmethod def check_unsafe(command: list[str], unsafe_commands: list[str], parser: argparse.ArgumentParser) -> None: diff --git a/src/ahriman/application/handlers/users.py b/src/ahriman/application/handlers/users.py index 17ef9876..4407712e 100644 --- a/src/ahriman/application/handlers/users.py +++ b/src/ahriman/application/handlers/users.py @@ -61,7 +61,7 @@ class Users(Handler): users = database.user_list(args.username, args.role) Users.check_if_empty(args.exit_code, not users) for user in users: - UserPrinter(user).print(verbose=True) + UserPrinter(user)(verbose=True) case Action.Remove: database.user_remove(args.username) diff --git a/src/ahriman/application/handlers/validate.py b/src/ahriman/application/handlers/validate.py index bc983bcd..11baa985 100644 --- a/src/ahriman/application/handlers/validate.py +++ b/src/ahriman/application/handlers/validate.py @@ -57,7 +57,7 @@ class Validate(Handler): if validator.validate(configuration.dump()): return # no errors found for node, errors in validator.errors.items(): - ValidationPrinter(node, errors).print(verbose=True) + ValidationPrinter(node, errors)(verbose=True) # as we reach this part it means that we always have errors Validate.check_if_empty(args.exit_code, True) diff --git a/src/ahriman/application/handlers/versions.py b/src/ahriman/application/handlers/versions.py index 3c1d8157..8dfc9c0a 100644 --- a/src/ahriman/application/handlers/versions.py +++ b/src/ahriman/application/handlers/versions.py @@ -55,9 +55,9 @@ class Versions(Handler): report(bool): force enable or disable reporting """ VersionPrinter(f"Module version {__version__}", - {"Python": sys.version}).print(verbose=False, separator=" ") + {"Python": sys.version})(verbose=False, separator=" ") packages = Versions.package_dependencies("ahriman") - VersionPrinter("Installed packages", dict(packages)).print(verbose=False, separator=" ") + VersionPrinter("Installed packages", dict(packages))(verbose=False, separator=" ") @staticmethod def package_dependencies(root: str) -> Generator[tuple[str, str], None, None]: diff --git a/src/ahriman/core/configuration/validator.py b/src/ahriman/core/configuration/validator.py index c10cbfc7..2ba5d25b 100644 --- a/src/ahriman/core/configuration/validator.py +++ b/src/ahriman/core/configuration/validator.py @@ -45,7 +45,7 @@ class Validator(RootValidator): Args: configuration(Configuration): configuration instance used for extraction *args(Any): positional arguments to be passed to base validator - **kwargs(): keyword arguments to be passed to base validator + **kwargs(Any): keyword arguments to be passed to base validator """ RootValidator.__init__(self, *args, **kwargs) self.configuration: Configuration = kwargs["configuration"] diff --git a/src/ahriman/core/formatters/printer.py b/src/ahriman/core/formatters/printer.py index 844b322e..4d147b05 100644 --- a/src/ahriman/core/formatters/printer.py +++ b/src/ahriman/core/formatters/printer.py @@ -17,6 +17,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . # +import builtins + from collections.abc import Callable from ahriman.models.property import Property @@ -27,7 +29,9 @@ class Printer: base class for formatters """ - def print(self, *, verbose: bool, log_fn: Callable[[str], None] = print, separator: str = ": ") -> None: + _print = builtins.print # do not shadow with method + + def print(self, *, verbose: bool, log_fn: Callable[[str], None] = _print, separator: str = ": ") -> None: """ print content @@ -60,3 +64,15 @@ class Printer: Returns: str | None: content title if it can be generated and None otherwise """ + return None + + def __call__(self, *, verbose: bool, log_fn: Callable[[str], None] = _print, separator: str = ": ") -> None: + """ + print content. Shortcut for ``Printer.print`` + + Args: + verbose(bool): print all fields + log_fn(Callable[[str], None], optional): logger function to log data (Default value = print) + separator(str, optional): separator for property name and property value (Default value = ": ") + """ + self.print(verbose=verbose, log_fn=log_fn, separator=separator) diff --git a/src/ahriman/core/report/console.py b/src/ahriman/core/report/console.py index d8cd4b4e..ac30ef47 100644 --- a/src/ahriman/core/report/console.py +++ b/src/ahriman/core/report/console.py @@ -54,6 +54,6 @@ class Console(Report): result(Result): build result """ for package in result.success: - BuildPrinter(package, is_success=True, use_utf=self.use_utf).print(verbose=True) + BuildPrinter(package, is_success=True, use_utf=self.use_utf)(verbose=True) for package in result.failed: - BuildPrinter(package, is_success=False, use_utf=self.use_utf).print(verbose=True) + BuildPrinter(package, is_success=False, use_utf=self.use_utf)(verbose=True) diff --git a/tests/ahriman/application/handlers/test_handler_patch.py b/tests/ahriman/application/handlers/test_handler_patch.py index 42e3ee50..054aedb9 100644 --- a/tests/ahriman/application/handlers/test_handler_patch.py +++ b/tests/ahriman/application/handlers/test_handler_patch.py @@ -159,7 +159,7 @@ def test_patch_set_list(application: Application, mocker: MockerFixture) -> None Patch.patch_set_list(application, "ahriman", ["version"], False) get_mock.assert_called_once_with("ahriman", ["version"]) - print_mock.assert_called_once_with(verbose=True, separator=" = ") + print_mock.assert_called_once_with(verbose=True, log_fn=pytest.helpers.anyvar(int), separator=" = ") check_mock.assert_called_once_with(False, False) diff --git a/tests/ahriman/application/handlers/test_handler_remove_unknown.py b/tests/ahriman/application/handlers/test_handler_remove_unknown.py index d0f25819..0bb9910e 100644 --- a/tests/ahriman/application/handlers/test_handler_remove_unknown.py +++ b/tests/ahriman/application/handlers/test_handler_remove_unknown.py @@ -1,4 +1,5 @@ import argparse +import pytest from pytest_mock import MockerFixture @@ -58,4 +59,4 @@ def test_run_dry_run(args: argparse.Namespace, configuration: Configuration, rep RemoveUnknown.run(args, repository_id, configuration, report=False) application_mock.assert_called_once_with() remove_mock.assert_not_called() - print_mock.assert_called_once_with(verbose=False) + print_mock.assert_called_once_with(verbose=False, log_fn=pytest.helpers.anyvar(int), separator=": ") diff --git a/tests/ahriman/application/handlers/test_handler_search.py b/tests/ahriman/application/handlers/test_handler_search.py index f2c08181..bf4fe07c 100644 --- a/tests/ahriman/application/handlers/test_handler_search.py +++ b/tests/ahriman/application/handlers/test_handler_search.py @@ -47,7 +47,10 @@ def test_run(args: argparse.Namespace, configuration: Configuration, repository: aur_search_mock.assert_called_once_with("ahriman", pacman=pytest.helpers.anyvar(int)) official_search_mock.assert_called_once_with("ahriman", pacman=pytest.helpers.anyvar(int)) check_mock.assert_called_once_with(False, False) - print_mock.assert_has_calls([MockCall(verbose=False), MockCall(verbose=False)]) + print_mock.assert_has_calls([ + MockCall(verbose=False, log_fn=pytest.helpers.anyvar(int), separator=": "), + MockCall(verbose=False, log_fn=pytest.helpers.anyvar(int), separator=": "), + ]) def test_run_empty_exception(args: argparse.Namespace, configuration: Configuration, repository: Repository, diff --git a/tests/ahriman/application/handlers/test_handler_service_updates.py b/tests/ahriman/application/handlers/test_handler_service_updates.py index 0f04128a..694c2697 100644 --- a/tests/ahriman/application/handlers/test_handler_service_updates.py +++ b/tests/ahriman/application/handlers/test_handler_service_updates.py @@ -1,4 +1,5 @@ import argparse +import pytest from pytest_mock import MockerFixture @@ -38,7 +39,7 @@ def test_run(args: argparse.Namespace, configuration: Configuration, repository: _, repository_id = configuration.check_loaded() ServiceUpdates.run(args, repository_id, configuration, report=False) package_mock.assert_called_once_with(package_ahriman.base, repository.pacman, None) - application_mock.assert_called_once_with(verbose=True, separator=" -> ") + application_mock.assert_called_once_with(verbose=True, log_fn=pytest.helpers.anyvar(int), separator=" -> ") check_mock.assert_called_once_with(args.exit_code, True) diff --git a/tests/ahriman/application/handlers/test_handler_shell.py b/tests/ahriman/application/handlers/test_handler_shell.py index d5104afb..af46976a 100644 --- a/tests/ahriman/application/handlers/test_handler_shell.py +++ b/tests/ahriman/application/handlers/test_handler_shell.py @@ -68,7 +68,7 @@ def test_run_verbose(args: argparse.Namespace, configuration: Configuration, rep Shell.run(args, repository_id, configuration, report=False) application_mock.assert_called_once_with(local=pytest.helpers.anyvar(int)) read_mock.assert_called_once_with(encoding="utf8") - print_mock.assert_called_once_with(verbose=False) + print_mock.assert_called_once_with(verbose=False, log_fn=pytest.helpers.anyvar(int), separator=": ") def test_disallow_multi_architecture_run() -> None: diff --git a/tests/ahriman/application/handlers/test_handler_status.py b/tests/ahriman/application/handlers/test_handler_status.py index 3988aa0b..44909243 100644 --- a/tests/ahriman/application/handlers/test_handler_status.py +++ b/tests/ahriman/application/handlers/test_handler_status.py @@ -1,4 +1,5 @@ import argparse +import pytest from pytest_mock import MockerFixture from unittest.mock import call as MockCall @@ -48,7 +49,10 @@ def test_run(args: argparse.Namespace, configuration: Configuration, repository: application_mock.assert_called_once_with() packages_mock.assert_called_once_with(None) check_mock.assert_called_once_with(False, False) - print_mock.assert_has_calls([MockCall(verbose=False) for _ in range(3)]) + print_mock.assert_has_calls([ + MockCall(verbose=False, log_fn=pytest.helpers.anyvar(int), separator=": ") + for _ in range(3) + ]) def test_run_empty_exception(args: argparse.Namespace, configuration: Configuration, repository: Repository, @@ -82,7 +86,10 @@ def test_run_verbose(args: argparse.Namespace, configuration: Configuration, rep _, repository_id = configuration.check_loaded() Status.run(args, repository_id, configuration, report=False) - print_mock.assert_has_calls([MockCall(verbose=True) for _ in range(2)]) + print_mock.assert_has_calls([ + MockCall(verbose=True, log_fn=pytest.helpers.anyvar(int), separator=": ") + for _ in range(2) + ]) def test_run_with_package_filter(args: argparse.Namespace, configuration: Configuration, repository: Repository, @@ -116,7 +123,10 @@ def test_run_by_status(args: argparse.Namespace, configuration: Configuration, r _, repository_id = configuration.check_loaded() Status.run(args, repository_id, configuration, report=False) - print_mock.assert_has_calls([MockCall(verbose=False) for _ in range(2)]) + print_mock.assert_has_calls([ + MockCall(verbose=False, log_fn=pytest.helpers.anyvar(int), separator=": ") + for _ in range(2) + ]) def test_imply_with_report(args: argparse.Namespace, configuration: Configuration, database: SQLite, diff --git a/tests/ahriman/application/handlers/test_handler_structure.py b/tests/ahriman/application/handlers/test_handler_structure.py index bde85b0c..5a09e9f3 100644 --- a/tests/ahriman/application/handlers/test_handler_structure.py +++ b/tests/ahriman/application/handlers/test_handler_structure.py @@ -1,4 +1,5 @@ import argparse +import pytest from pytest_mock import MockerFixture from unittest.mock import call as MockCall @@ -40,9 +41,9 @@ def test_run(args: argparse.Namespace, configuration: Configuration, repository: packages_mock.assert_called_once_with([package_ahriman], count=args.partitions) application_mock.assert_called_once_with([package_ahriman]) print_mock.assert_has_calls([ - MockCall(verbose=False), - MockCall(verbose=True, separator=" "), - MockCall(verbose=False), + MockCall(verbose=False, log_fn=pytest.helpers.anyvar(int), separator=": "), + MockCall(verbose=True, log_fn=pytest.helpers.anyvar(int), separator=" "), + MockCall(verbose=False, log_fn=pytest.helpers.anyvar(int), separator=": "), ]) diff --git a/tests/ahriman/application/handlers/test_handler_unsafe_commands.py b/tests/ahriman/application/handlers/test_handler_unsafe_commands.py index 01fcc5d9..f6ca4e8a 100644 --- a/tests/ahriman/application/handlers/test_handler_unsafe_commands.py +++ b/tests/ahriman/application/handlers/test_handler_unsafe_commands.py @@ -35,7 +35,7 @@ def test_run(args: argparse.Namespace, configuration: Configuration, mocker: Moc _, repository_id = configuration.check_loaded() UnsafeCommands.run(args, repository_id, configuration, report=False) commands_mock.assert_called_once_with(pytest.helpers.anyvar(int)) - print_mock.assert_called_once_with(verbose=True) + print_mock.assert_called_once_with(verbose=True, log_fn=pytest.helpers.anyvar(int), separator=": ") def test_run_check(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None: diff --git a/tests/ahriman/application/handlers/test_handler_validate.py b/tests/ahriman/application/handlers/test_handler_validate.py index ad89cea6..73af3fed 100644 --- a/tests/ahriman/application/handlers/test_handler_validate.py +++ b/tests/ahriman/application/handlers/test_handler_validate.py @@ -1,5 +1,6 @@ import argparse import json +import pytest from pytest_mock import MockerFixture @@ -36,7 +37,7 @@ def test_run(args: argparse.Namespace, configuration: Configuration, mocker: Moc _, repository_id = configuration.check_loaded() Validate.run(args, repository_id, configuration, report=False) application_mock.assert_called_once_with(configuration.dump()) - print_mock.assert_called_once_with(verbose=True) + print_mock.assert_called_once_with(verbose=True, log_fn=pytest.helpers.anyvar(int), separator=": ") def test_run_skip(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None: diff --git a/tests/ahriman/application/handlers/test_handler_versions.py b/tests/ahriman/application/handlers/test_handler_versions.py index 25d37dbe..73602a42 100644 --- a/tests/ahriman/application/handlers/test_handler_versions.py +++ b/tests/ahriman/application/handlers/test_handler_versions.py @@ -1,4 +1,5 @@ import argparse +import pytest from pytest_mock import MockerFixture from unittest.mock import call as MockCall @@ -17,7 +18,10 @@ def test_run(args: argparse.Namespace, configuration: Configuration, mocker: Moc _, repository_id = configuration.check_loaded() Versions.run(args, repository_id, configuration, report=False) application_mock.assert_called_once_with("ahriman") - print_mock.assert_has_calls([MockCall(verbose=False, separator=" "), MockCall(verbose=False, separator=" ")]) + print_mock.assert_has_calls([ + MockCall(verbose=False, log_fn=pytest.helpers.anyvar(int), separator=" "), + MockCall(verbose=False, log_fn=pytest.helpers.anyvar(int), separator=" "), + ]) def test_package_dependencies() -> None: diff --git a/tests/ahriman/core/formatters/test_printer.py b/tests/ahriman/core/formatters/test_printer.py index ee1bed0b..456b8558 100644 --- a/tests/ahriman/core/formatters/test_printer.py +++ b/tests/ahriman/core/formatters/test_printer.py @@ -1,3 +1,5 @@ +import pytest + from pytest_mock import MockerFixture from unittest.mock import MagicMock, call as MockCall @@ -63,3 +65,12 @@ def test_title() -> None: must return empty title """ assert Printer().title() is None + + +def test_call(mocker: MockerFixture) -> None: + """ + must perform print call + """ + print_mock = mocker.patch("ahriman.core.formatters.Printer.print") + Printer()(verbose=False) + print_mock.assert_called_once_with(verbose=False, log_fn=pytest.helpers.anyvar(int), separator=": ") diff --git a/tests/ahriman/core/report/test_console.py b/tests/ahriman/core/report/test_console.py index 24edb6bb..9518447c 100644 --- a/tests/ahriman/core/report/test_console.py +++ b/tests/ahriman/core/report/test_console.py @@ -1,3 +1,5 @@ +import pytest + from pytest_mock import MockerFixture from unittest.mock import call as MockCall @@ -18,4 +20,7 @@ def test_generate(configuration: Configuration, result: Result, package_python_s report = Console(repository_id, configuration, "console") report.generate([], result) - print_mock.assert_has_calls([MockCall(verbose=True), MockCall(verbose=True)]) + print_mock.assert_has_calls([ + MockCall(verbose=True, log_fn=pytest.helpers.anyvar(int), separator=": "), + MockCall(verbose=True, log_fn=pytest.helpers.anyvar(int), separator=": "), + ])