mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-04-24 15:27:17 +00:00
feat: add __call__ method to printers
This commit is contained in:
parent
9fe760efdf
commit
8ff567cac3
@ -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]:
|
||||
"""
|
||||
|
@ -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=" = ")
|
||||
|
@ -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:
|
||||
|
@ -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)
|
||||
|
@ -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]:
|
||||
|
@ -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)
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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:
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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]:
|
||||
|
@ -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"]
|
||||
|
@ -17,6 +17,8 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
||||
|
||||
|
@ -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=": ")
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
|
||||
|
||||
|
@ -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:
|
||||
|
@ -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,
|
||||
|
@ -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=": "),
|
||||
])
|
||||
|
||||
|
||||
|
@ -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:
|
||||
|
@ -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:
|
||||
|
@ -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:
|
||||
|
@ -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=": ")
|
||||
|
@ -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=": "),
|
||||
])
|
||||
|
Loading…
Reference in New Issue
Block a user