mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-07-16 23:39:56 +00:00
refactor: even further improvements for Handler.check_status method
This commit is contained in:
@ -27,6 +27,7 @@ from ahriman.application.lock import Lock
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.core.exceptions import ExitCode, MissingArchitectureError, MultipleArchitecturesError
|
||||
from ahriman.core.log.log_loader import LogLoader
|
||||
from ahriman.core.types import ExplicitBool
|
||||
from ahriman.models.repository_id import RepositoryId
|
||||
from ahriman.models.repository_paths import RepositoryPaths
|
||||
|
||||
@ -124,13 +125,14 @@ class Handler:
|
||||
raise NotImplementedError
|
||||
|
||||
@staticmethod
|
||||
def check_status(enabled: bool, status: bool | Callable[[], bool]) -> None:
|
||||
def check_status(enabled: bool, status: ExplicitBool | Callable[[], ExplicitBool]) -> 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
|
||||
status(bool | Callable[[], bool]): return status or function to check. ``True`` means success and vice versa
|
||||
status(ExplicitBool | Callable[[], ExplicitBool]): return status or function to check.
|
||||
``True`` means success and vice versa
|
||||
|
||||
Raises:
|
||||
ExitCode: if result is empty and check is enabled
|
||||
@ -138,12 +140,9 @@ class Handler:
|
||||
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
|
||||
status = status() if callable(status) else status
|
||||
if not status:
|
||||
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_status(exit_code, bool(patches))
|
||||
Patch.check_status(exit_code, patches)
|
||||
|
||||
PatchPrinter(package_base, patches)(verbose=True, separator=" = ")
|
||||
|
||||
|
@ -51,7 +51,7 @@ class Rebuild(Handler):
|
||||
packages = Rebuild.extract_packages(application, args.status, from_database=args.from_database)
|
||||
packages = application.repository.packages_depend_on(packages, args.depends_on)
|
||||
|
||||
Rebuild.check_status(args.exit_code, bool(packages))
|
||||
Rebuild.check_status(args.exit_code, packages)
|
||||
if args.dry_run:
|
||||
application.print_updates(packages, log_fn=print)
|
||||
return
|
||||
|
@ -61,7 +61,7 @@ class Status(Handler):
|
||||
else:
|
||||
packages = client.package_get(None)
|
||||
|
||||
Status.check_status(args.exit_code, bool(packages))
|
||||
Status.check_status(args.exit_code, packages)
|
||||
|
||||
comparator: Callable[[tuple[Package, BuildStatus]], str] = lambda item: item[0].base
|
||||
filter_fn: Callable[[tuple[Package, BuildStatus]], bool] =\
|
||||
|
@ -54,7 +54,7 @@ class Update(Handler):
|
||||
application.changes(packages)
|
||||
|
||||
if args.dry_run: # exit from application if no build requested
|
||||
Update.check_status(args.exit_code, bool(packages)) # status code check
|
||||
Update.check_status(args.exit_code, packages) # status code check
|
||||
return
|
||||
|
||||
packages = application.with_dependencies(packages, process_dependencies=args.dependencies)
|
||||
|
@ -61,7 +61,7 @@ class Users(Handler):
|
||||
users = database.user_list(args.username, args.role)
|
||||
for user in users:
|
||||
UserPrinter(user)(verbose=True)
|
||||
Users.check_status(args.exit_code, bool(users))
|
||||
Users.check_status(args.exit_code, users)
|
||||
case Action.Remove:
|
||||
database.user_remove(args.username)
|
||||
|
||||
|
39
src/ahriman/core/types.py
Normal file
39
src/ahriman/core/types.py
Normal file
@ -0,0 +1,39 @@
|
||||
#
|
||||
# Copyright (c) 2021-2024 ahriman team.
|
||||
#
|
||||
# This file is part of ahriman
|
||||
# (see https://github.com/arcan1s/ahriman).
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
from typing import Protocol
|
||||
|
||||
|
||||
class HasBool(Protocol):
|
||||
"""
|
||||
class which defines :func:`bool()` method
|
||||
"""
|
||||
|
||||
def __bool__(self) -> bool: ...
|
||||
|
||||
|
||||
class HasLength(Protocol):
|
||||
"""
|
||||
class which defines :func:`len()` method
|
||||
"""
|
||||
|
||||
def __len__(self) -> int: ...
|
||||
|
||||
|
||||
ExplicitBool = HasBool | HasLength | int
|
Reference in New Issue
Block a user