From f47be6cab0b36e5afb835f6e8c6ffcc5725eed4b Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Tue, 18 Oct 2022 02:13:01 +0300 Subject: [PATCH] disallow no values in configuration This option could lead to missing warnings about missing or invalid configuration values because code usually expects that values are exists and not empty unless it is explicitly specified. However, pacman configuration still requires this option in order to be able to deal with boolean values --- src/ahriman/application/handlers/setup.py | 4 +++- src/ahriman/core/configuration.py | 8 ++++++-- src/ahriman/core/report/report_trigger.py | 2 +- src/ahriman/core/sign/gpg.py | 2 +- src/ahriman/core/upload/http_upload.py | 8 ++++---- src/ahriman/core/upload/upload_trigger.py | 2 +- 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/ahriman/application/handlers/setup.py b/src/ahriman/application/handlers/setup.py index e5d109db..6d2424b9 100644 --- a/src/ahriman/application/handlers/setup.py +++ b/src/ahriman/application/handlers/setup.py @@ -133,7 +133,9 @@ class Setup(Handler): repository(str): repository name paths(RepositoryPaths): repository paths instance """ - configuration = Configuration() + # allow_no_value=True is required because pacman uses boolean configuration in which just keys present + # (e.g. NoProgressBar) which will lead to exception + configuration = Configuration(allow_no_value=True) # preserve case # stupid mypy thinks that it is impossible configuration.optionxform = lambda key: key # type: ignore diff --git a/src/ahriman/core/configuration.py b/src/ahriman/core/configuration.py index 020c7cf6..8602ed9a 100644 --- a/src/ahriman/core/configuration.py +++ b/src/ahriman/core/configuration.py @@ -70,11 +70,15 @@ class Configuration(configparser.RawConfigParser): ARCHITECTURE_SPECIFIC_SECTIONS = ["build", "sign", "web"] SYSTEM_CONFIGURATION_PATH = Path(sys.prefix) / "share" / "ahriman" / "settings" / "ahriman.ini" - def __init__(self) -> None: + def __init__(self, allow_no_value: bool = False) -> None: """ default constructor. In the most cases must not be called directly + + Args: + allow_no_value(bool): copies ``configparser.RawConfigParser`` behaviour. In case if it is set to ``True``, + the keys without values will be allowed """ - configparser.RawConfigParser.__init__(self, allow_no_value=True, converters={ + configparser.RawConfigParser.__init__(self, allow_no_value=allow_no_value, converters={ "list": self.__convert_list, "path": self.__convert_path, }) diff --git a/src/ahriman/core/report/report_trigger.py b/src/ahriman/core/report/report_trigger.py index 35eadac5..984da075 100644 --- a/src/ahriman/core/report/report_trigger.py +++ b/src/ahriman/core/report/report_trigger.py @@ -43,7 +43,7 @@ class ReportTrigger(Trigger): configuration(Configuration): configuration instance """ Trigger.__init__(self, architecture, configuration) - self.targets = configuration.getlist("report", "target") + self.targets = configuration.getlist("report", "target", fallback=[]) def on_result(self, result: Result, packages: Iterable[Package]) -> None: """ diff --git a/src/ahriman/core/sign/gpg.py b/src/ahriman/core/sign/gpg.py index 398d751f..c9d534a6 100644 --- a/src/ahriman/core/sign/gpg.py +++ b/src/ahriman/core/sign/gpg.py @@ -97,7 +97,7 @@ class GPG(LazyLogging): Tuple[Set[SignSettings], Optional[str]]: tuple of sign targets and default PGP key """ targets: Set[SignSettings] = set() - for option in configuration.getlist("sign", "target"): + for option in configuration.getlist("sign", "target", fallback=[]): target = SignSettings.from_option(option) if target == SignSettings.Disabled: continue diff --git a/src/ahriman/core/upload/http_upload.py b/src/ahriman/core/upload/http_upload.py index 8ecb3cd6..9bb3dc35 100644 --- a/src/ahriman/core/upload/http_upload.py +++ b/src/ahriman/core/upload/http_upload.py @@ -33,7 +33,7 @@ class HttpUpload(Upload): helper for the http based uploads Attributes: - auth(Tuple[str, str]): HTTP auth object + auth(Optional[Tuple[str, str]]): HTTP auth object if set timeout(int): HTTP request timeout in seconds """ @@ -47,9 +47,9 @@ class HttpUpload(Upload): section(str): configuration section name """ Upload.__init__(self, architecture, configuration) - password = configuration.get(section, "password") - username = configuration.get(section, "username") - self.auth = (password, username) + password = configuration.get(section, "password", fallback=None) + username = configuration.get(section, "username", fallback=None) + self.auth = (password, username) if password and username else None self.timeout = configuration.getint(section, "timeout", fallback=30) @staticmethod diff --git a/src/ahriman/core/upload/upload_trigger.py b/src/ahriman/core/upload/upload_trigger.py index 1a306a5a..c993e4ac 100644 --- a/src/ahriman/core/upload/upload_trigger.py +++ b/src/ahriman/core/upload/upload_trigger.py @@ -43,7 +43,7 @@ class UploadTrigger(Trigger): configuration(Configuration): configuration instance """ Trigger.__init__(self, architecture, configuration) - self.targets = configuration.getlist("upload", "target") + self.targets = configuration.getlist("upload", "target", fallback=[]) def on_result(self, result: Result, packages: Iterable[Package]) -> None: """