summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2021-04-16 16:23:35 -0500
committerGitHub <noreply@github.com>2021-04-16 16:23:35 -0500
commit6a08795adb9ab60d1ff26946fdff9a5442b0be6f (patch)
treedd48a551b07e067d8fe4d1766a455485d53b02d2
parentd6f9e2432c00a13d248f513add5660c3b99d4c76 (diff)
parente96932cbd99f508f9f5c24b207b5650aa8817e45 (diff)
downloadvyos-1x-6a08795adb9ab60d1ff26946fdff9a5442b0be6f.tar.gz
vyos-1x-6a08795adb9ab60d1ff26946fdff9a5442b0be6f.zip
Merge pull request #811 from jestabro/no-tag-node-value-mangle
config: T3481: add switch to prevent mangling of tag node values
-rw-r--r--python/vyos/config.py5
-rw-r--r--python/vyos/util.py29
2 files changed, 29 insertions, 5 deletions
diff --git a/python/vyos/config.py b/python/vyos/config.py
index de79a3654..a5c1ad122 100644
--- a/python/vyos/config.py
+++ b/python/vyos/config.py
@@ -214,7 +214,8 @@ class Config(object):
return config_dict
def get_config_dict(self, path=[], effective=False, key_mangling=None,
- get_first_key=False, no_multi_convert=False):
+ get_first_key=False, no_multi_convert=False,
+ no_tag_node_value_mangle=False):
"""
Args:
path (str list): Configuration tree path, can be empty
@@ -247,7 +248,7 @@ class Config(object):
isinstance(key_mangling[1], str)):
raise ValueError("key_mangling must be a tuple of two strings")
- conf_dict = vyos.util.mangle_dict_keys(conf_dict, key_mangling[0], key_mangling[1])
+ conf_dict = vyos.util.mangle_dict_keys(conf_dict, key_mangling[0], key_mangling[1], abs_path=xmlpath, no_tag_node_value_mangle=no_tag_node_value_mangle)
return conf_dict
diff --git a/python/vyos/util.py b/python/vyos/util.py
index e2f4b8fc4..2a3f6a228 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():