From 2c10e3766d882e0ad02db9f3ef7d7b4cb0d3ae62 Mon Sep 17 00:00:00 2001 From: hygienic-books Date: Tue, 5 Jul 2022 04:46:35 +0200 Subject: [PATCH] feat(config): Define settings that are allowed empty --- update-firewall-source.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/update-firewall-source.py b/update-firewall-source.py index 6ed1850..4872eb3 100644 --- a/update-firewall-source.py +++ b/update-firewall-source.py @@ -112,6 +112,7 @@ class ConfigParser( ini_defaults = [] internal_defaults = {default["key"]: default["value"] for default in CONST.CFG_KNOWN_DEFAULTS} internal_globals = [default["key"] for default in CONST.CFG_KNOWN_DEFAULTS if default["is_global"]] +internal_empty_ok = [default["key"] for default in CONST.CFG_KNOWN_DEFAULTS if default["empty_ok"]] config = ConfigParser(defaults=internal_defaults, converters={'list': lambda x: [i.strip() for i in x.split(',') if len(x) > 0]}) config.read(CONST.CFG_DEFAULT_ABS_PATH) @@ -172,10 +173,38 @@ def is_same_as_default( return config_kv_pair in ini_defaults +def we_have_unset_options( + config_obj: configparser.ConfigParser(), + section_name: str) -> list: + + options_must_be_non_empty = [] + + for option in config_obj.options(section_name): + if not config_obj.get(section_name, option): + if option not in internal_empty_ok: + log.warning(f"In section '[{section_name}]' option '{option}' is empty, it mustn't be.") + options_must_be_non_empty.append(option) + + return options_must_be_non_empty + + def validate_config_sections( config_obj: configparser.ConfigParser()) -> None: for this_section in config_obj.sections(): log.debug(print_section_header(this_section)) + + unset_options = we_have_unset_options(config_obj, this_section) + if unset_options: + log.error(f"""{p.plural("Option", len(unset_options))} {unset_options} """ + f"""{p.plural("is", len(unset_options))} unset. """ + f"""{p.singular_noun("They", len(unset_options))} """ + f"must have a non-null value. " + f"""{p.plural("Default", len(unset_options))} {p.plural("is", len(unset_options))}:""") + for unset_option in unset_options: + log.error(f"{unset_option} = {internal_defaults[unset_option]}") + log.error(f"Exiting 7 ...") + sys.exit(7) + if not set(CONST.CFG_MANDATORY).issubset(config_obj.options(this_section, no_defaults=True)): log.warning(f"Config section '[{this_section}]' does not have all mandatory options " f"{CONST.CFG_MANDATORY} set, skipping section ...")