mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-07-29 13:49:57 +00:00
refactor: rename Handler.check_if_empty to check_status
This commit is contained in:
@ -65,4 +65,4 @@ class Add(Handler):
|
||||
|
||||
application.print_updates(packages, log_fn=application.logger.info)
|
||||
result = application.update(packages, packagers, bump_pkgrel=args.increment)
|
||||
Add.check_if_empty(args.exit_code, result.is_empty)
|
||||
Add.check_status(args.exit_code, not result.is_empty)
|
||||
|
@ -54,6 +54,6 @@ class Change(Handler):
|
||||
case Action.List:
|
||||
changes = client.package_changes_get(args.package)
|
||||
ChangesPrinter(changes)(verbose=True, separator="")
|
||||
Change.check_if_empty(args.exit_code, changes.is_empty)
|
||||
Change.check_status(args.exit_code, not changes.is_empty)
|
||||
case Action.Remove:
|
||||
client.package_changes_update(args.package, Changes())
|
||||
|
@ -20,7 +20,7 @@
|
||||
import argparse
|
||||
import logging
|
||||
|
||||
from collections.abc import Iterable
|
||||
from collections.abc import Callable, Iterable
|
||||
from multiprocessing import Pool
|
||||
|
||||
from ahriman.application.lock import Lock
|
||||
@ -124,19 +124,26 @@ class Handler:
|
||||
raise NotImplementedError
|
||||
|
||||
@staticmethod
|
||||
def check_if_empty(enabled: bool, predicate: bool) -> None:
|
||||
def check_status(enabled: bool, status: bool | Callable[[], bool]) -> None:
|
||||
"""
|
||||
check condition and flag and raise ExitCode exception in case if it is enabled and condition match
|
||||
|
||||
Args:
|
||||
enabled(bool): if ``False`` no check will be performed
|
||||
predicate(bool): indicates condition on which exception should be thrown
|
||||
status(bool | Callable[[], bool]): return status or function to check. ``True`` means success and vice versa
|
||||
|
||||
Raises:
|
||||
ExitCode: if result is empty and check is enabled
|
||||
"""
|
||||
if enabled and predicate:
|
||||
raise ExitCode
|
||||
if not enabled:
|
||||
return
|
||||
|
||||
match status:
|
||||
case False:
|
||||
raise ExitCode
|
||||
# https://github.com/python/mypy/issues/14014
|
||||
case Callable() if not status(): # type: ignore[misc]
|
||||
raise ExitCode
|
||||
|
||||
@staticmethod
|
||||
def repositories_extract(args: argparse.Namespace) -> list[RepositoryId]:
|
||||
|
@ -136,7 +136,7 @@ class Patch(Handler):
|
||||
for patch in application.reporter.package_patches_get(package_base, None)
|
||||
if variables is None or patch.key in variables
|
||||
]
|
||||
Patch.check_if_empty(exit_code, not patches)
|
||||
Patch.check_status(exit_code, bool(patches))
|
||||
|
||||
PatchPrinter(package_base, patches)(verbose=True, separator=" = ")
|
||||
|
||||
|
@ -49,15 +49,15 @@ class Rebuild(Handler):
|
||||
application.on_start()
|
||||
|
||||
packages = Rebuild.extract_packages(application, args.status, from_database=args.from_database)
|
||||
updates = application.repository.packages_depend_on(packages, args.depends_on)
|
||||
packages = application.repository.packages_depend_on(packages, args.depends_on)
|
||||
|
||||
Rebuild.check_if_empty(args.exit_code, not updates)
|
||||
Rebuild.check_status(args.exit_code, bool(packages))
|
||||
if args.dry_run:
|
||||
application.print_updates(updates, log_fn=print)
|
||||
application.print_updates(packages, log_fn=print)
|
||||
return
|
||||
|
||||
result = application.update(updates, Packagers(args.username), bump_pkgrel=args.increment)
|
||||
Rebuild.check_if_empty(args.exit_code, result.is_empty)
|
||||
result = application.update(packages, Packagers(args.username), bump_pkgrel=args.increment)
|
||||
Rebuild.check_status(args.exit_code, not result.is_empty)
|
||||
|
||||
@staticmethod
|
||||
def extract_packages(application: Application, status: BuildStatusEnum | None, *,
|
||||
|
@ -47,7 +47,7 @@ class Run(Handler):
|
||||
parser = args.parser()
|
||||
for command in args.command:
|
||||
status = Run.run_command(shlex.split(command), parser)
|
||||
Run.check_if_empty(True, not status)
|
||||
Run.check_status(True, status)
|
||||
|
||||
@staticmethod
|
||||
def run_command(command: list[str], parser: argparse.ArgumentParser) -> bool:
|
||||
|
@ -60,7 +60,8 @@ class Search(Handler):
|
||||
"""
|
||||
official_packages_list = Official.multisearch(*args.search)
|
||||
aur_packages_list = AUR.multisearch(*args.search)
|
||||
Search.check_if_empty(args.exit_code, not official_packages_list and not aur_packages_list)
|
||||
non_empty = bool(official_packages_list or aur_packages_list)
|
||||
Search.check_status(args.exit_code, non_empty)
|
||||
|
||||
for packages_list in (official_packages_list, aur_packages_list):
|
||||
# keep sorting by packages source
|
||||
|
@ -57,4 +57,4 @@ class ServiceUpdates(Handler):
|
||||
return
|
||||
|
||||
UpdatePrinter(remote, local_version)(verbose=True, separator=" -> ")
|
||||
ServiceUpdates.check_if_empty(args.exit_code, not same_version)
|
||||
ServiceUpdates.check_status(args.exit_code, same_version)
|
||||
|
@ -61,7 +61,7 @@ class Status(Handler):
|
||||
else:
|
||||
packages = client.package_get(None)
|
||||
|
||||
Status.check_if_empty(args.exit_code, not packages)
|
||||
Status.check_status(args.exit_code, bool(packages))
|
||||
|
||||
comparator: Callable[[tuple[Package, BuildStatus]], str] = lambda item: item[0].base
|
||||
filter_fn: Callable[[tuple[Package, BuildStatus]], bool] =\
|
||||
|
@ -63,7 +63,7 @@ class UnsafeCommands(Handler):
|
||||
parser(argparse.ArgumentParser): generated argument parser
|
||||
"""
|
||||
args = parser.parse_args(command)
|
||||
UnsafeCommands.check_if_empty(True, args.command in unsafe_commands)
|
||||
UnsafeCommands.check_status(True, args.command not in unsafe_commands)
|
||||
|
||||
@staticmethod
|
||||
def get_unsafe_commands(parser: argparse.ArgumentParser) -> list[str]:
|
||||
|
@ -54,7 +54,7 @@ class Update(Handler):
|
||||
application.changes(packages)
|
||||
|
||||
if args.dry_run: # exit from application if no build requested
|
||||
Update.check_if_empty(args.exit_code, not packages) # status code check
|
||||
Update.check_status(args.exit_code, bool(packages)) # status code check
|
||||
return
|
||||
|
||||
packages = application.with_dependencies(packages, process_dependencies=args.dependencies)
|
||||
@ -62,7 +62,7 @@ class Update(Handler):
|
||||
|
||||
application.print_updates(packages, log_fn=application.logger.info)
|
||||
result = application.update(packages, packagers, bump_pkgrel=args.increment)
|
||||
Update.check_if_empty(args.exit_code, result.is_empty)
|
||||
Update.check_status(args.exit_code, not result.is_empty)
|
||||
|
||||
@staticmethod
|
||||
def log_fn(application: Application, dry_run: bool) -> Callable[[str], None]:
|
||||
|
@ -59,9 +59,9 @@ class Users(Handler):
|
||||
database.user_update(user.hash_password(salt))
|
||||
case Action.List:
|
||||
users = database.user_list(args.username, args.role)
|
||||
Users.check_if_empty(args.exit_code, not users)
|
||||
for user in users:
|
||||
UserPrinter(user)(verbose=True)
|
||||
Users.check_status(args.exit_code, bool(users))
|
||||
case Action.Remove:
|
||||
database.user_remove(args.username)
|
||||
|
||||
|
@ -61,7 +61,7 @@ class Validate(Handler):
|
||||
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)
|
||||
Validate.check_status(args.exit_code, False)
|
||||
|
||||
@staticmethod
|
||||
def schema(repository_id: RepositoryId, configuration: Configuration) -> ConfigurationSchema:
|
||||
|
Reference in New Issue
Block a user