diff options
author | John Estabrook <jestabro@vyos.io> | 2023-07-04 19:06:28 -0500 |
---|---|---|
committer | John Estabrook <jestabro@vyos.io> | 2023-07-07 08:33:51 -0500 |
commit | 38a8bcfc407fefea357dfbb5f337562593a265b5 (patch) | |
tree | 59c825fc90e83308488fd732f088d077565d45c7 /python/vyos | |
parent | b5e9b61671d32b3bf540d6ce3638adf33adbd4c7 (diff) | |
download | vyos-1x-38a8bcfc407fefea357dfbb5f337562593a265b5.tar.gz vyos-1x-38a8bcfc407fefea357dfbb5f337562593a265b5.zip |
config: T5330: add subclass ConfigDict to preserve merge data
Diffstat (limited to 'python/vyos')
-rw-r--r-- | python/vyos/config.py | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/python/vyos/config.py b/python/vyos/config.py index c3bb68373..b0dbc5c2a 100644 --- a/python/vyos/config.py +++ b/python/vyos/config.py @@ -68,10 +68,16 @@ import json from copy import deepcopy import vyos.configtree -from vyos.xml_ref import multi_to_list, merge_defaults, relative_defaults +from vyos.xml_ref import multi_to_list, from_source +from vyos.xml_ref import merge_defaults, relative_defaults from vyos.utils.dict import get_sub_dict, mangle_dict_keys from vyos.configsource import ConfigSource, ConfigSourceSession +class ConfigDict(dict): + _from_defaults = {} + def from_defaults(self, path: list[str]): + return from_source(self._from_defaults, path) + class Config(object): """ The class of config access objects. @@ -250,6 +256,7 @@ class Config(object): conf_dict = multi_to_list(rpath, conf_dict) if with_defaults or with_recursive_defaults: + conf_dict = ConfigDict(conf_dict) conf_dict = merge_defaults(lpath, conf_dict, get_first_key=get_first_key, recursive=with_recursive_defaults) @@ -263,7 +270,17 @@ class Config(object): isinstance(key_mangling[1], str)): raise ValueError("key_mangling must be a tuple of two strings") - conf_dict = mangle_dict_keys(conf_dict, key_mangling[0], key_mangling[1], abs_path=rpath, no_tag_node_value_mangle=no_tag_node_value_mangle) + def mangle(obj): + return mangle_dict_keys(obj, key_mangling[0], key_mangling[1], + abs_path=rpath, + no_tag_node_value_mangle=no_tag_node_value_mangle) + + if isinstance(conf_dict, ConfigDict): + from_defaults = mangle(conf_dict._from_defaults) + conf_dict = mangle(conf_dict) + conf_dict._from_defaults = from_defaults + else: + conf_dict = mangle(conf_dict) return conf_dict |