From b667636dcb868acd37fad15d06d05ed3955b242b Mon Sep 17 00:00:00 2001 From: Spitap Date: Mon, 25 Sep 2023 12:09:38 +0200 Subject: [PATCH] Added possibility of if directive in each entry --- modoboa_installer/config_dict_template.py | 3 ++- modoboa_installer/utils.py | 26 +++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/modoboa_installer/config_dict_template.py b/modoboa_installer/config_dict_template.py index 2147a8c..7a2c38d 100644 --- a/modoboa_installer/config_dict_template.py +++ b/modoboa_installer/config_dict_template.py @@ -42,7 +42,8 @@ ConfigDictTemplate = [ "default": "rspamd", "customizable": True, "question": "Please select your antispam utility", - "values": ["rspamd", "amavis"] + "values": ["rspamd", "amavis"], + "if": ["antispam.enabled=true"] } ] }, diff --git a/modoboa_installer/utils.py b/modoboa_installer/utils.py index d8ca3ee..7ca9c62 100644 --- a/modoboa_installer/utils.py +++ b/modoboa_installer/utils.py @@ -293,6 +293,16 @@ def random_key(l=16): return key +def check_if_condition(config, entry): + """Check if the "if" directive is present and computes it""" + section_if = True + for condition in entry: + config_key, value = condition.split("=") + section_name, option = config_key.split(".") + section_if = config.get(section_name, option) == value + return section_if + + def validate(value, config_entry): if value is None: return False @@ -362,15 +372,19 @@ def load_config_template(interactive): for section in tpl_dict: interactive_section = interactive if "if" in section: - for condition in section.get("if"): - config_key, value = condition.split("=") - section_name, option = config_key.split(".") - interactive_section = interactive_section and ( - config.get(section_name, option) == value) + condition = check_if_condition(config, section["if"]) + interactive_section = condition and interactive config.add_section(section["name"]) for config_entry in section["values"]: - value = get_entry_value(config_entry, interactive_section) + if config_entry.get("if") is not None: + interactive_section = (interactive_section and + check_if_condition( + config, config_entry["if"] + ) + ) + value = get_entry_value(config_entry, + interactive_section) config.set(section["name"], config_entry["option"], value) return config