summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--python/vyos/configdiff.py2
-rw-r--r--python/vyos/util.py46
-rw-r--r--src/tests/test_util.py2
3 files changed, 2 insertions, 48 deletions
diff --git a/python/vyos/configdiff.py b/python/vyos/configdiff.py
index 5d30e9b66..0caa204c3 100644
--- a/python/vyos/configdiff.py
+++ b/python/vyos/configdiff.py
@@ -20,7 +20,7 @@ from vyos.configtree import DiffTree
from vyos.configdict import dict_merge
from vyos.configdict import list_diff
from vyos.utils.dict import get_sub_dict
-from vyos.util import mangle_dict_keys
+from vyos.utils.dict import mangle_dict_keys
from vyos.utils.dict import dict_search_args
from vyos.xml import defaults
diff --git a/python/vyos/util.py b/python/vyos/util.py
index 63539e897..a13d95201 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -13,49 +13,3 @@
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
-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
- with something acceptable.
-
- Args:
- data (dict): Original dict to mangle
-
- Returns: dict
- """
- import re
- from vyos.xml import is_tag
-
- new_dict = {}
-
- for key in data.keys():
- 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, 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)
diff --git a/src/tests/test_util.py b/src/tests/test_util.py
index 66c27e824..27ee648d4 100644
--- a/src/tests/test_util.py
+++ b/src/tests/test_util.py
@@ -18,7 +18,7 @@ from unittest import TestCase
class TestVyOSUtil(TestCase):
def test_key_mangling(self):
- from vyos.util import mangle_dict_keys
+ from vyos.utils.dict import mangle_dict_keys
data = {"foo-bar": {"baz-quux": None}}
expected_data = {"foo_bar": {"baz_quux": None}}
new_data = mangle_dict_keys(data, '-', '_')