summaryrefslogtreecommitdiff
path: root/python/vyos/util.py
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2021-04-16 09:04:22 -0500
committerJohn Estabrook <jestabro@vyos.io>2021-04-18 09:11:49 -0500
commitff7b2b0e62510ef8de28c9c4bfa34badeabec775 (patch)
tree9ef353972047eb616b0ea1412d6f8c2fa979568f /python/vyos/util.py
parent0646a2b3be10bde0d98d0f61af737d80c4645a52 (diff)
downloadvyos-1x-ff7b2b0e62510ef8de28c9c4bfa34badeabec775.tar.gz
vyos-1x-ff7b2b0e62510ef8de28c9c4bfa34badeabec775.zip
config: T3481: add switch to prevent mangling of tag node values
(cherry picked from commit e96932cbd99f508f9f5c24b207b5650aa8817e45)
Diffstat (limited to 'python/vyos/util.py')
-rw-r--r--python/vyos/util.py29
1 files changed, 26 insertions, 3 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py
index 17a7dda91..13d0f03be 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -364,7 +364,7 @@ def colon_separated_to_dict(data_string, uniquekeys=False):
return data
-def mangle_dict_keys(data, regex, replacement):
+def _mangle_dict_keys(data, regex, replacement, abs_path=[], no_tag_node_value_mangle=False, mod=0):
""" Mangles dict keys according to a regex and replacement character.
Some libraries like Jinja2 do not like certain characters in dict keys.
This function can be used for replacing all offending characters
@@ -375,18 +375,41 @@ def mangle_dict_keys(data, regex, replacement):
Returns: dict
"""
+ from vyos.xml import is_tag
+
new_dict = {}
+
for key in data.keys():
- new_key = re.sub(regex, replacement, key)
+ save_mod = mod
+ save_path = abs_path[:]
+
+ abs_path.append(key)
+
+ if not is_tag(abs_path):
+ new_key = re.sub(regex, replacement, key)
+ else:
+ if mod%2:
+ new_key = key
+ else:
+ new_key = re.sub(regex, replacement, key)
+ if no_tag_node_value_mangle:
+ mod += 1
value = data[key]
+
if isinstance(value, dict):
- new_dict[new_key] = mangle_dict_keys(value, regex, replacement)
+ new_dict[new_key] = _mangle_dict_keys(value, regex, replacement, abs_path=abs_path, mod=mod, no_tag_node_value_mangle=no_tag_node_value_mangle)
else:
new_dict[new_key] = value
+ mod = save_mod
+ abs_path = save_path[:]
+
return new_dict
+def mangle_dict_keys(data, regex, replacement, abs_path=[], no_tag_node_value_mangle=False):
+ return _mangle_dict_keys(data, regex, replacement, abs_path=abs_path, no_tag_node_value_mangle=no_tag_node_value_mangle, mod=0)
+
def _get_sub_dict(d, lpath):
k = lpath[0]
if k not in d.keys():