From 25e22c993f851e7432d81c3de50f783ab57bc00e Mon Sep 17 00:00:00 2001 From: dw-0 Date: Mon, 14 Apr 2025 21:15:12 +0200 Subject: [PATCH] chore(scp): update SimpleConfigParser Signed-off-by: Dominik Willner --- .../simple_config_parser.py | 46 +++++++++++-------- .../tests/public_api/test_options_api.py | 4 +- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/kiauh/core/submodules/simple_config_parser/src/simple_config_parser/simple_config_parser.py b/kiauh/core/submodules/simple_config_parser/src/simple_config_parser/simple_config_parser.py index 842d7bd..01d9374 100644 --- a/kiauh/core/submodules/simple_config_parser/src/simple_config_parser/simple_config_parser.py +++ b/kiauh/core/submodules/simple_config_parser/src/simple_config_parser/simple_config_parser.py @@ -314,9 +314,7 @@ class SimpleConfigParser: elements.pop(i) break - def getval( - self, section: str, option: str, fallback: str | _UNSET = _UNSET - ) -> str | List[str]: + def getval(self, section: str, option: str, fallback: str | _UNSET = _UNSET) -> str: """ Return the value of the given option in the given section @@ -329,22 +327,34 @@ class SimpleConfigParser: if option not in self.get_options(section): raise NoOptionError(option, section) - # Find the option in the elements list for element in self.config[section]["elements"]: - if element["type"] in [LineType.OPTION.value, LineType.OPTION_BLOCK.value] and element["name"] == option: - raw_value = element["value"] - if isinstance(raw_value, str) and raw_value.endswith("\n"): - return raw_value[:-1].strip() - elif isinstance(raw_value, list): - values: List[str] = [] - for i, val in enumerate(raw_value): - val = val.strip().strip("\n") - if len(val) < 1: - continue - values.append(val.strip()) - return values - return str(raw_value) - raise NoOptionError(option, section) + if element["type"] is LineType.OPTION.value and element["name"] == option: + return str(element["value"].strip().replace("\n", "")) + return "" + + except (NoSectionError, NoOptionError): + if fallback is _UNSET: + raise + return fallback + + def getvals(self, section: str, option: str, fallback: List[str] | _UNSET = _UNSET) -> List[str]: + """ + Return the values of the given multi-line option in the given section + + If the key is not found and 'fallback' is provided, it is used as + a fallback value. + """ + try: + if section not in self.get_sections(): + raise NoSectionError(section) + if option not in self.get_options(section): + raise NoOptionError(option, section) + + for element in self.config[section]["elements"]: + if element["type"] is LineType.OPTION_BLOCK.value and element["name"] == option: + return [val.strip() for val in element["value"] if val.strip()] + return [] + except (NoSectionError, NoOptionError): if fallback is _UNSET: raise diff --git a/kiauh/core/submodules/simple_config_parser/tests/public_api/test_options_api.py b/kiauh/core/submodules/simple_config_parser/tests/public_api/test_options_api.py index a423745..1e6f5fe 100644 --- a/kiauh/core/submodules/simple_config_parser/tests/public_api/test_options_api.py +++ b/kiauh/core/submodules/simple_config_parser/tests/public_api/test_options_api.py @@ -51,7 +51,7 @@ def test_getval(parser): assert parser.getval("section_2", "option_2") == "value_2" # test multiline option values - ml_val = parser.getval("section number 5", "multi_option") + ml_val = parser.getvals("section number 5", "multi_option") assert isinstance(ml_val, list) assert len(ml_val) > 0 @@ -164,7 +164,7 @@ def test_set_new_option(parser): assert parser.getval("new_section", "very_new_option") == "very_new_value" parser.set_option("section_2", "array_option", ["value_1", "value_2", "value_3"]) - assert parser.getval("section_2", "array_option") == [ + assert parser.getvals("section_2", "array_option") == [ "value_1", "value_2", "value_3",