From 63fee7ab0c082f3c614bb140de1e7482399dff2b Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Thu, 21 Apr 2022 14:17:14 -0400 Subject: T4361: refactor and simplify vyos.config.exists() --- python/vyos/config.py | 73 +++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 34 deletions(-) (limited to 'python') 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): """ -- cgit v1.2.3