config panels: attempt to improve the semantic of 'convert()'

This commit is contained in:
Alexandre Aubin 2021-09-23 05:02:27 +02:00
parent 9c3ff52d98
commit d5748b519a

View file

@ -201,20 +201,20 @@ class ConfigPanel:
# Transform toml format into internal format # Transform toml format into internal format
format_description = { format_description = {
"toml": { "root": {
"properties": ["version", "i18n"], "properties": ["version", "i18n"],
"default": {"version": 1.0}, "defaults": {"version": 1.0},
}, },
"panels": { "panels": {
"properties": ["name", "services", "actions", "help"], "properties": ["name", "services", "actions", "help"],
"default": { "defaults": {
"services": [], "services": [],
"actions": {"apply": {"en": "Apply"}}, "actions": {"apply": {"en": "Apply"}},
}, },
}, },
"sections": { "sections": {
"properties": ["name", "services", "optional", "help", "visible"], "properties": ["name", "services", "optional", "help", "visible"],
"default": { "defaults": {
"name": "", "name": "",
"services": [], "services": [],
"optional": True, "optional": True,
@ -244,11 +244,11 @@ class ConfigPanel:
"accept", "accept",
"redact", "redact",
], ],
"default": {}, "defaults": {},
}, },
} }
def convert(toml_node, node_type): def _build_internal_config_panel(raw_infos, level):
"""Convert TOML in internal format ('full' mode used by webadmin) """Convert TOML in internal format ('full' mode used by webadmin)
Here are some properties of 1.0 config panel in toml: Here are some properties of 1.0 config panel in toml:
- node properties and node children are mixed, - node properties and node children are mixed,
@ -256,48 +256,49 @@ class ConfigPanel:
- some properties have default values - some properties have default values
This function detects all children nodes and put them in a list This function detects all children nodes and put them in a list
""" """
# Prefill the node default keys if needed
default = format_description[node_type]["default"]
node = {key: toml_node.get(key, value) for key, value in default.items()}
properties = format_description[node_type]["properties"] defaults = format_description[level]["defaults"]
properties = format_description[level]["properties"]
# Define the filter_key part to use and the children type # Start building the ouput (merging the raw infos + defaults)
i = list(format_description).index(node_type) out = {key: raw_infos.get(key, value) for key, value in defaults.items()}
subnode_type = (
list(format_description)[i + 1] if node_type != "options" else None # Now fill the sublevels (+ apply filter_key)
i = list(format_description).index(level)
sublevel = (
list(format_description)[i + 1] if level != "options" else None
) )
search_key = filter_key[i] if len(filter_key) > i else False search_key = filter_key[i] if len(filter_key) > i else False
for key, value in toml_node.items(): for key, value in raw_infos.items():
# Key/value are a child node # Key/value are a child node
if ( if (
isinstance(value, OrderedDict) isinstance(value, OrderedDict)
and key not in properties and key not in properties
and subnode_type and sublevel
): ):
# We exclude all nodes not referenced by the filter_key # We exclude all nodes not referenced by the filter_key
if search_key and key != search_key: if search_key and key != search_key:
continue continue
subnode = convert(value, subnode_type) subnode = _build_internal_config_panel(value, sublevel)
subnode["id"] = key subnode["id"] = key
if node_type == "toml": if level == "root":
subnode.setdefault("name", {"en": key.capitalize()}) subnode.setdefault("name", {"en": key.capitalize()})
elif node_type == "sections": elif level == "sections":
subnode["name"] = key # legacy subnode["name"] = key # legacy
subnode.setdefault("optional", toml_node.get("optional", True)) subnode.setdefault("optional", raw_infos.get("optional", True))
node.setdefault(subnode_type, []).append(subnode) out.setdefault(sublevel, []).append(subnode)
# Key/value are a property # Key/value are a property
else: else:
if key not in properties: if key not in properties:
logger.warning(f"Unknown key '{key}' found in config toml") logger.warning(f"Unknown key '{key}' found in config panel")
# Todo search all i18n keys # Todo search all i18n keys
node[key] = ( out[key] = (
value if key not in ["ask", "help", "name"] else {"en": value} value if key not in ["ask", "help", "name"] else {"en": value}
) )
return node return out
self.config = convert(toml_config_panel, "toml") self.config = _build_internal_config_panel(toml_config_panel, "root")
try: try:
self.config["panels"][0]["sections"][0]["options"][0] self.config["panels"][0]["sections"][0]["options"][0]