diff options
author | John Estabrook <jestabro@vyos.io> | 2021-08-04 08:10:34 -0500 |
---|---|---|
committer | John Estabrook <jestabro@vyos.io> | 2021-08-04 09:41:24 -0500 |
commit | 3a814957f412759b6ebc908ed78c7d299adedfb3 (patch) | |
tree | fb1b6b5ad353e7fa40164edbd2777bb4e8300e52 | |
parent | 62fd0c326173eeaccc751ad85969a5f0a93fb155 (diff) | |
download | vyos-1x-3a814957f412759b6ebc908ed78c7d299adedfb3.tar.gz vyos-1x-3a814957f412759b6ebc908ed78c7d299adedfb3.zip |
configquery: T3402: remove restriction of query to active config
-rw-r--r-- | python/vyos/configquery.py | 65 |
1 files changed, 10 insertions, 55 deletions
diff --git a/python/vyos/configquery.py b/python/vyos/configquery.py index 1ee66ca64..1cdcbcf39 100644 --- a/python/vyos/configquery.py +++ b/python/vyos/configquery.py @@ -25,7 +25,9 @@ from subprocess import STDOUT import vyos.util import vyos.xml +from vyos.config import Config from vyos.configtree import ConfigTree +from vyos.configsource import ConfigSourceSession class ConfigQueryError(Exception): pass @@ -75,15 +77,12 @@ class CliShellApiConfigQuery(GenericConfigQuery): raise ConfigQueryError('No values for given path') return out -class ConfigTreeActiveQuery(GenericConfigQuery): +class ConfigTreeQuery(GenericConfigQuery): def __init__(self): super().__init__() - with open('/config/config.boot') as f: - config_file = f.read() - self.configtree = ConfigTree(config_file) - - self._level = [] + config_source = ConfigSourceSession() + self.configtree = Config(config_source=config_source) def exists(self, path: list): return self.configtree.exists(path) @@ -97,57 +96,13 @@ class ConfigTreeActiveQuery(GenericConfigQuery): def list_nodes(self, path: list): return self.configtree.list_nodes(path) - def _make_path(self, path): - # Backwards-compatibility stuff: original implementation used string paths - # libvyosconfig paths are lists, but since node names cannot contain whitespace, - # splitting at whitespace is reasonably safe. - # It may cause problems with exists() when it's used for checking values, - # since values may contain whitespace. - if isinstance(path, str): - path = re.split(r'\s+', path) - elif isinstance(path, list): - pass - else: - raise TypeError("Path must be a whitespace-separated string or a list") - return (self._level + path) - - def get_config_dict(self, path=[], key_mangling=None, + def get_config_dict(self, path=[], effective=False, key_mangling=None, get_first_key=False, no_multi_convert=False, no_tag_node_value_mangle=False): - """ - Args: - path (str list): Configuration tree path, can be empty - key_mangling=None: mangle dict keys according to regex and replacement - get_first_key=False: if k = path[:-1], return sub-dict d[k] instead of {k: d[k]} - no_multi_convert=False: if convert, return single value of multi node as list - - Returns: a dict representation of the config under path - """ - lpath = self._make_path(path) - root_dict = json.loads(self.configtree.to_json()) - conf_dict = vyos.util.get_sub_dict(root_dict, lpath, get_first_key) - - if not key_mangling and no_multi_convert: - return deepcopy(conf_dict) - - xmlpath = lpath if get_first_key else lpath[:-1] - - if not key_mangling: - conf_dict = vyos.xml.multi_to_list(xmlpath, conf_dict) - return conf_dict - - if no_multi_convert is False: - conf_dict = vyos.xml.multi_to_list(xmlpath, conf_dict) - - 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") - - conf_dict = vyos.util.mangle_dict_keys(conf_dict, key_mangling[0], key_mangling[1], abs_path=xmlpath, no_tag_node_value_mangle=no_tag_node_value_mangle) - - return conf_dict + return self.configtree.get_config_dict(path, effective=effective, + key_mangling=key_mangling, get_first_key=get_first_key, + no_multi_convert=no_multi_convert, + no_tag_node_value_mangle=no_tag_node_value_mangle) class VbashOpRun(GenericOpRun): def __init__(self): |