summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Mangin <thomas.mangin@exa.net.uk>2020-06-27 13:47:52 +0100
committerThomas Mangin <thomas.mangin@exa.net.uk>2020-06-27 13:47:52 +0100
commit3977fe705b0155ef380e3522201b812fa2149f76 (patch)
tree2dcfa977bdf69ee0144ac432d100b4b94ac1ece8
parent1d97d52120eaa33a3c2edb39e966002dab7f9576 (diff)
downloadvyos-1x-3977fe705b0155ef380e3522201b812fa2149f76.tar.gz
vyos-1x-3977fe705b0155ef380e3522201b812fa2149f76.zip
xml: T2656: option to not flatten the default dict
-rw-r--r--python/vyos/xml/__init__.py9
-rw-r--r--python/vyos/xml/definition.py16
2 files changed, 17 insertions, 8 deletions
diff --git a/python/vyos/xml/__init__.py b/python/vyos/xml/__init__.py
index 52f5bfb38..bda8c96c5 100644
--- a/python/vyos/xml/__init__.py
+++ b/python/vyos/xml/__init__.py
@@ -35,5 +35,10 @@ def load_configuration(cache=[]):
return xml
-def defaults(lpath):
- return load_configuration().defaults(lpath)
+def defaults(lpath, flat=True):
+ return load_configuration().defaults(lpath, flat)
+
+
+if __name__ == '__main__':
+ print(defaults(['service'], flat=True))
+ print(defaults(['service'], flat=False))
diff --git a/python/vyos/xml/definition.py b/python/vyos/xml/definition.py
index a66170a18..e493c54c1 100644
--- a/python/vyos/xml/definition.py
+++ b/python/vyos/xml/definition.py
@@ -11,6 +11,7 @@
# You should have received a copy of the GNU Lesser General Public License along with this library;
# if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+from copy import deepcopy
from vyos.xml import kw
@@ -246,28 +247,31 @@ class XML(dict):
# @lru_cache(maxsize=100)
# XXX: need to use cachetool instead - for later
- def defaults(self, lpath):
+ def defaults(self, lpath, flat):
d = self[kw.default]
for k in lpath:
d = d[k]
- r = {}
- def _flatten(inside, index, d, r):
+ if not flat:
+ return deepcopy(d)
+
+ def _flatten(inside, index, d):
+ r = {}
local = inside[index:]
prefix = '_'.join(_.replace('-','_') for _ in local) + '_' if local else ''
for k in d:
under = prefix + k.replace('-','_')
level = inside + [k]
if isinstance(d[k],dict):
- _flatten(level, index, d[k], r)
+ r.update(_flatten(level, index, d[k]))
continue
if self.is_multi(level, with_tag=False):
r[under] = [_.strip() for _ in d[k].split(',')]
continue
r[under] = d[k]
+ return r
- _flatten(lpath, len(lpath), d, r)
- return r
+ return _flatten(lpath, len(lpath), d)
# from functools import lru_cache
# @lru_cache(maxsize=100)