From 648ab001b6563443c86eb4959f1592c1a8543b63 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Mon, 14 Jul 2025 17:00:25 +0900 Subject: [PATCH 1/2] Settings loader fix for not set range check entries If we have a range or length check and the value is not set, skip, and do not convert either Not set is None --- src/corelibs/config_handling/settings_loader.py | 3 +++ test-run/config_handling/config/settings.ini | 1 + test-run/config_handling/settings_loader.py | 3 +++ 3 files changed, 7 insertions(+) diff --git a/src/corelibs/config_handling/settings_loader.py b/src/corelibs/config_handling/settings_loader.py index d8194c5..f6c0b36 100644 --- a/src/corelibs/config_handling/settings_loader.py +++ b/src/corelibs/config_handling/settings_loader.py @@ -331,6 +331,9 @@ class SettingsLoader: (__from, __to, __equal) = check valid = True for value_raw in convert_to_list(values): + # skip no tset values for range check + if not value_raw: + continue value = 0 error_mark = '' if check_type == 'length': diff --git a/test-run/config_handling/config/settings.ini b/test-run/config_handling/config/settings.ini index 63d97f1..88897b8 100644 --- a/test-run/config_handling/config/settings.ini +++ b/test-run/config_handling/config/settings.ini @@ -9,6 +9,7 @@ other_list=a|b|c|d| third_list=xy|ab|df|fg str_length=foobar int_range=20 +int_range_not_set= # match_target=foo match_target_list=foo,bar,baz diff --git a/test-run/config_handling/settings_loader.py b/test-run/config_handling/settings_loader.py index d53c572..422fdfa 100644 --- a/test-run/config_handling/settings_loader.py +++ b/test-run/config_handling/settings_loader.py @@ -63,6 +63,9 @@ def main(): "int_range": [ "range:2-50" ], + "int_range_not_set": [ + "range:2-50" + ], "match_target": ["matching:foo"], "match_target_list": ["split:,", "matching:foo|bar|baz",], "match_source_a": ["in:match_target"], From c69076f517c0b57de1854c3762d16da64768b52f Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Mon, 14 Jul 2025 17:19:33 +0900 Subject: [PATCH 2/2] Add set default if empty/not set in settings With new empty: block if just like this set to None if not set (empty), can also be any value, if list, skip setting default --- .../config_handling/settings_loader.py | 31 +++++++++++++++++-- test-run/config_handling/config/settings.ini | 1 + test-run/config_handling/settings_loader.py | 3 ++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/corelibs/config_handling/settings_loader.py b/src/corelibs/config_handling/settings_loader.py index f6c0b36..c796fbf 100644 --- a/src/corelibs/config_handling/settings_loader.py +++ b/src/corelibs/config_handling/settings_loader.py @@ -51,11 +51,14 @@ class SettingsLoader: self.always_print = always_print # entries that have to be split self.entry_split_char: dict[str, str] = {} + # entries that should be converted self.entry_convert: dict[str, str] = {} + # default set entries + self.entry_set_empty: dict[str, str | None] = {} # config parser, load config file first self.config_parser: configparser.ConfigParser | None = self.__load_config_file() # all settings - self.settings: dict[str, dict[str, Any]] | None = None + self.settings: dict[str, dict[str, None | str | int | float | bool]] | None = None # remove file name and get base path and check if not self.config_file.parent.is_dir(): raise ValueError(f"Cannot find the config folder: {self.config_file.parent}") @@ -79,6 +82,7 @@ class SettingsLoader: - in: the right side is another KEY value from the settings where this value must be inside - split: character to split entries, if set check:list+ must be set if checks are needed - convert: convert to int, float -> if element is number convert, else leave as is + - empty: convert empty to, if nothing set on the right side then convert to None type Args: config_id (str): what block to load @@ -115,6 +119,8 @@ class SettingsLoader: 'CRITICAL', raise_exception=True ) + sys.exit(1) + self.entry_convert[key] = convert_to except ValueError as e: self.__print( f"[!] In [{config_id}] the convert type setup for entry failed: {check}: {e}", @@ -122,6 +128,20 @@ class SettingsLoader: raise_exception=True ) sys.exit(1) + if check.startswith('empty:'): + try: + [_, empty_set] = check.split(":") + if not empty_set: + empty_set = None + self.entry_set_empty[key] = empty_set + except ValueError as e: + print(f"VALUE ERROR: {key}") + self.__print( + f"[!] In [{config_id}] the empty set type for entry failed: {check}: {e}", + 'CRITICAL', + raise_exception=True + ) + sys.exit(1) # split char, also check to not set it twice, first one only if check.startswith("split:") and not self.entry_split_char.get(key): try: @@ -243,8 +263,14 @@ class SettingsLoader: if error is True: self.__print("[!] Missing or incorrect settings data. Cannot proceed", 'CRITICAL', raise_exception=True) sys.exit(1) + # set empty + for [entry, empty_set] in self.entry_set_empty.items(): + # if set, skip, else set to empty value + if settings[config_id].get(entry) or isinstance(settings[config_id].get(entry), list): + continue + settings[config_id][entry] = empty_set # Convert input - for [entry, convert_type] in self.entry_convert: + for [entry, convert_type] in self.entry_convert.items(): if convert_type in ["int", "any"] and is_int(settings[config_id][entry]): settings[config_id][entry] = int(settings[config_id][entry]) elif convert_type in ["float", "any"] and is_float(settings[config_id][entry]): @@ -263,6 +289,7 @@ class SettingsLoader: 'ERROR' ) # string is always string + # TODO: empty and int/float/bool: set to none? return settings[config_id] diff --git a/test-run/config_handling/config/settings.ini b/test-run/config_handling/config/settings.ini index 88897b8..b951f8b 100644 --- a/test-run/config_handling/config/settings.ini +++ b/test-run/config_handling/config/settings.ini @@ -10,6 +10,7 @@ third_list=xy|ab|df|fg str_length=foobar int_range=20 int_range_not_set= +int_range_not_set_empty_set=5 # match_target=foo match_target_list=foo,bar,baz diff --git a/test-run/config_handling/settings_loader.py b/test-run/config_handling/settings_loader.py index 422fdfa..c320c3d 100644 --- a/test-run/config_handling/settings_loader.py +++ b/test-run/config_handling/settings_loader.py @@ -66,6 +66,9 @@ def main(): "int_range_not_set": [ "range:2-50" ], + "int_range_not_set_empty_set": [ + "empty:" + ], "match_target": ["matching:foo"], "match_target_list": ["split:,", "matching:foo|bar|baz",], "match_source_a": ["in:match_target"],