summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2022-10-21 13:35:48 -0500
committerGitHub <noreply@github.com>2022-10-21 13:35:48 -0500
commitf157aea8c7dddd159c9daecc528250370d560db2 (patch)
treede3cd73f58d8890a0bc8bdb4328f7af5d270ad58
parenteeb7810f4240a2c1d1da0bcc6825ae9c75ca693e (diff)
parentb6d2e0a4b08c81814cb2d9b5b611cbc3fc31dbeb (diff)
downloadvyos-1x-f157aea8c7dddd159c9daecc528250370d560db2.tar.gz
vyos-1x-f157aea8c7dddd159c9daecc528250370d560db2.zip
Merge pull request #1611 from dmbaturin/field-normalization-2
T4765: support list and primitives in op mode output normalization
-rw-r--r--python/vyos/opmode.py14
-rw-r--r--src/tests/test_op_mode.py25
2 files changed, 33 insertions, 6 deletions
diff --git a/python/vyos/opmode.py b/python/vyos/opmode.py
index 5f9c2c1ce..c9827d634 100644
--- a/python/vyos/opmode.py
+++ b/python/vyos/opmode.py
@@ -120,12 +120,12 @@ def _normalize_field_name(name):
return name
-def _normalize_field_names(old_dict):
+def _normalize_dict_field_names(old_dict):
new_dict = {}
for key in old_dict:
new_key = _normalize_field_name(key)
- new_dict[new_key] = old_dict[key]
+ new_dict[new_key] = _normalize_field_names(old_dict[key])
# Sanity check
if len(old_dict) != len(new_dict):
@@ -133,6 +133,14 @@ def _normalize_field_names(old_dict):
else:
return new_dict
+def _normalize_field_names(value):
+ if isinstance(value, dict):
+ return _normalize_dict_field_names(value)
+ elif isinstance(value, list):
+ return list(map(lambda v: _normalize_field_names(v), value))
+ else:
+ return value
+
def run(module):
from argparse import ArgumentParser
@@ -188,8 +196,6 @@ def run(module):
if not args["raw"]:
return res
else:
- if not isinstance(res, dict):
- raise InternalError("'raw' output of 'show_*' command must be a dict")
res = _normalize_field_names(res)
from json import dumps
return dumps(res, indent=4)
diff --git a/src/tests/test_op_mode.py b/src/tests/test_op_mode.py
index 4786357c5..90963b3c5 100644
--- a/src/tests/test_op_mode.py
+++ b/src/tests/test_op_mode.py
@@ -37,8 +37,29 @@ class TestVyOSOpMode(TestCase):
with self.assertRaises(vyos.opmode.InternalError):
_normalize_field_names(data)
- def test_dict_fields_normalization(self):
+ def test_dict_fields_normalization_simple_dict(self):
from vyos.opmode import _normalize_field_names
- data = {"foo bar": True, "bar-baz": False}
+ data = {"foo bar": True, "Bar-Baz": False}
self.assertEqual(_normalize_field_names(data), {"foo_bar": True, "bar_baz": False})
+
+ def test_dict_fields_normalization_nested_dict(self):
+ from vyos.opmode import _normalize_field_names
+
+ data = {"foo bar": True, "bar-baz": {"baz-quux": {"quux-xyzzy": False}}}
+ self.assertEqual(_normalize_field_names(data),
+ {"foo_bar": True, "bar_baz": {"baz_quux": {"quux_xyzzy": False}}})
+
+ def test_dict_fields_normalization_mixed(self):
+ from vyos.opmode import _normalize_field_names
+
+ data = [{"foo bar": True, "bar-baz": [{"baz-quux": {"quux-xyzzy": [False]}}]}]
+ self.assertEqual(_normalize_field_names(data),
+ [{"foo_bar": True, "bar_baz": [{"baz_quux": {"quux_xyzzy": [False]}}]}])
+
+ def test_dict_fields_normalization_primitive(self):
+ from vyos.opmode import _normalize_field_names
+
+ data = [1, False, "foo"]
+ self.assertEqual(_normalize_field_names(data), [1, False, "foo"])
+