diff options
author | John Estabrook <jestabro@vyos.io> | 2022-04-24 13:44:12 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-24 13:44:12 -0500 |
commit | fd9f82f96041b137885c99c526ac6efe2c4abc57 (patch) | |
tree | 9568568402107596cbcaf3fbb7bbeef76e91258c /python/vyos/config.py | |
parent | d7733328c767b838ceeaa75a1845c2629879d431 (diff) | |
parent | 63fee7ab0c082f3c614bb140de1e7482399dff2b (diff) | |
download | vyos-1x-fd9f82f96041b137885c99c526ac6efe2c4abc57.tar.gz vyos-1x-fd9f82f96041b137885c99c526ac6efe2c4abc57.zip |
Merge pull request #1295 from dmbaturin/T4361
T4361: refactor and simplify vyos.config.exists()
Diffstat (limited to 'python/vyos/config.py')
-rw-r--r-- | python/vyos/config.py | 73 |
1 files changed, 39 insertions, 34 deletions
diff --git a/python/vyos/config.py b/python/vyos/config.py index 858c7bdd7..287fd2ed1 100644 --- a/python/vyos/config.py +++ b/python/vyos/config.py @@ -156,26 +156,28 @@ class Config(object): """ if self._session_config is None: return False + + # Assume the path is a node path first if self._session_config.exists(self._make_path(path)): return True - # libvyosconfig exists() works only for _nodes_, not _values_ - # libvyattacfg also worked for values, so we emulate that case here - if isinstance(path, str): - path = re.split(r'\s+', path) - path_without_value = path[:-1] - path_str = " ".join(path_without_value) - try: - value = self._session_config.return_value(self._make_path(path_str)) - values = self._session_config.return_values(self._make_path(path_str)) - except vyos.configtree.ConfigTreeError: - # node/value doesn't exist - return False - if value and path[-1] == value: - return True - if isinstance(values, list) and path[-1] in values: - return True + else: + # If that check fails, it may mean the path has a value at the end. + # libvyosconfig exists() works only for _nodes_, not _values_ + # libvyattacfg also worked for values, so we emulate that case here + if isinstance(path, str): + path = re.split(r'\s+', path) + path_without_value = path[:-1] + try: + # return_values() is safe to use with single-value nodes, + # it simply returns a single-item list in that case. + values = self._session_config.return_values(self._make_path(path_without_value)) - return False + # If we got this far, the node does exist and has values, + # so we need to check if it has the value in question among its values. + return (path[-1] in values) + except vyos.configtree.ConfigTreeError: + # Even the parent node doesn't exist at all + return False def session_changed(self): """ @@ -402,26 +404,29 @@ class Config(object): """ if self._running_config is None: return False + + # Assume the path is a node path first if self._running_config.exists(self._make_path(path)): return True - # libvyosconfig exists() works only for _nodes_, not _values_ - # libvyattacfg also worked for values, so we emulate that case here - if isinstance(path, str): - path = re.split(r'\s+', path) - path_without_value = path[:-1] - path_str = " ".join(path_without_value) - try: - value = self._running_config.return_value(self._make_path(path_str)) - values = self._running_config.return_values(self._make_path(path_str)) - except vyos.configtree.ConfigTreeError: - # node/value doesn't exist - return False - if value and path[-1] == value: - return True - if isinstance(values, list) and path[-1] in values: - return True + else: + # If that check fails, it may mean the path has a value at the end. + # libvyosconfig exists() works only for _nodes_, not _values_ + # libvyattacfg also worked for values, so we emulate that case here + if isinstance(path, str): + path = re.split(r'\s+', path) + path_without_value = path[:-1] + try: + # return_values() is safe to use with single-value nodes, + # it simply returns a single-item list in that case. + values = self._running_config.return_values(self._make_path(path_without_value)) + + # If we got this far, the node does exist and has values, + # so we need to check if it has the value in question among its values. + return (path[-1] in values) + except vyos.configtree.ConfigTreeError: + # Even the parent node doesn't exist at all + return False - return False def return_effective_value(self, path, default=None): """ |