From f2f6b963b755ca5da3321e84738bfec1d08fb1ea Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Fri, 2 Jun 2023 23:29:47 -0500 Subject: xml: T5218: fix error and simplify logic in recursive option --- python/vyos/xml_ref/__init__.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'python/vyos/xml_ref/__init__.py') diff --git a/python/vyos/xml_ref/__init__.py b/python/vyos/xml_ref/__init__.py index ae5184746..d3fb4ab07 100644 --- a/python/vyos/xml_ref/__init__.py +++ b/python/vyos/xml_ref/__init__.py @@ -62,5 +62,8 @@ def get_config_defaults(rpath: list, conf: dict, get_first_key=False, get_first_key=get_first_key, recursive=recursive) -def merge_defaults(path: list, conf: dict) -> dict: - return load_reference().merge_defaults(path, conf) +def merge_defaults(path: list, conf: dict, get_first_key=False, + recursive=False) -> dict: + return load_reference().merge_defaults(path, conf, + get_first_key=get_first_key, + recursive=recursive) -- cgit v1.2.3 From d19e7e5ce633f42f2a084ebd17e8c1e3dfef6b03 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Fri, 2 Jun 2023 23:35:00 -0500 Subject: config: T5228: add get_config_defaults options to match get_config_dict For those cases not covered by automatic merging of defaults in get_config_dict(..., with_defaults=True), get_config_defaults should take arguments consistent with those of get_config_dict, for ease of merging results. --- python/vyos/config.py | 27 ++++++++++++++++++++++++++- python/vyos/xml_ref/__init__.py | 6 +++--- 2 files changed, 29 insertions(+), 4 deletions(-) (limited to 'python/vyos/xml_ref/__init__.py') diff --git a/python/vyos/config.py b/python/vyos/config.py index 1b9e61651..c3bb68373 100644 --- a/python/vyos/config.py +++ b/python/vyos/config.py @@ -68,7 +68,7 @@ import json from copy import deepcopy import vyos.configtree -from vyos.xml_ref import multi_to_list, merge_defaults +from vyos.xml_ref import multi_to_list, merge_defaults, relative_defaults from vyos.utils.dict import get_sub_dict, mangle_dict_keys from vyos.configsource import ConfigSource, ConfigSourceSession @@ -267,6 +267,31 @@ class Config(object): return conf_dict + def get_config_defaults(self, path=[], effective=False, key_mangling=None, + no_tag_node_value_mangle=False, get_first_key=False, + recursive=False) -> dict: + lpath = self._make_path(path) + root_dict = self.get_cached_root_dict(effective) + conf_dict = get_sub_dict(root_dict, lpath, get_first_key) + + defaults = relative_defaults(lpath, conf_dict, + get_first_key=get_first_key, + recursive=recursive) + if key_mangling is None: + return defaults + + rpath = lpath if get_first_key else lpath[:-1] + + if not (isinstance(key_mangling, tuple) and \ + (len(key_mangling) == 2) and \ + isinstance(key_mangling[0], str) and \ + isinstance(key_mangling[1], str)): + raise ValueError("key_mangling must be a tuple of two strings") + + defaults = mangle_dict_keys(defaults, key_mangling[0], key_mangling[1], abs_path=rpath, no_tag_node_value_mangle=no_tag_node_value_mangle) + + return defaults + def is_multi(self, path): """ Args: diff --git a/python/vyos/xml_ref/__init__.py b/python/vyos/xml_ref/__init__.py index d3fb4ab07..53ca6ed98 100644 --- a/python/vyos/xml_ref/__init__.py +++ b/python/vyos/xml_ref/__init__.py @@ -55,10 +55,10 @@ def get_defaults(path: list, get_first_key=False, recursive=False) -> dict: return load_reference().get_defaults(path, get_first_key=get_first_key, recursive=recursive) -def get_config_defaults(rpath: list, conf: dict, get_first_key=False, - recursive=False) -> dict: +def relative_defaults(rpath: list, conf: dict, get_first_key=False, + recursive=False) -> dict: - return load_reference().relative_defaults(rpath, conf=conf, + return load_reference().relative_defaults(rpath, conf, get_first_key=get_first_key, recursive=recursive) -- cgit v1.2.3