From 5379a132a0d8973f1bd206cdc2a7910a6ca1832d Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Tue, 3 Feb 2026 20:29:54 -0600 Subject: T8232: internal name change for consistency with ReferenceTree class --- python/vyos/configtree.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'python') diff --git a/python/vyos/configtree.py b/python/vyos/configtree.py index e57cd5205..03aac72be 100644 --- a/python/vyos/configtree.py +++ b/python/vyos/configtree.py @@ -224,20 +224,20 @@ class ConfigTree(object): def __eq__(self, other): if isinstance(other, ConfigTree): - return self.__equal(self._get_config(), other._get_config()) + return self.__equal(self.get_tree(), other.get_tree()) return False def __str__(self): return self.to_string() - def _get_config(self): + def get_tree(self): return self.__config def get_version_string(self): return self.__version def write_cache(self, file_name): - self.__write_internal(self._get_config(), file_name.encode()) + self.__write_internal(self.get_tree(), file_name.encode()) def to_string(self, ordered_values=False, no_version=False): config_string = self.__to_string(self.__config, ordered_values).decode() @@ -495,7 +495,7 @@ def diff_compare(left, right, path=[], commands=False, libpath=LIBPATH): __get_error.argtypes = [] __get_error.restype = c_char_p - res = __diff_compare(commands, path_str, left._get_config(), right._get_config()) + res = __diff_compare(commands, path_str, left.get_tree(), right.get_tree()) res = res.decode() if res == '#1@': msg = __get_error().decode() @@ -521,7 +521,7 @@ def union(left, right, libpath=LIBPATH): __get_error.argtypes = [] __get_error.restype = c_char_p - res = __tree_union(left._get_config(), right._get_config()) + res = __tree_union(left.get_tree(), right.get_tree()) tree = ConfigTree(address=res) return tree @@ -543,7 +543,7 @@ def merge(left, right, destructive=False, libpath=LIBPATH): __get_error.argtypes = [] __get_error.restype = c_char_p - res = __tree_merge(destructive, left._get_config(), right._get_config()) + res = __tree_merge(destructive, left.get_tree(), right.get_tree()) tree = ConfigTree(address=res) return tree @@ -562,7 +562,7 @@ def mask_inclusive(left, right, libpath=LIBPATH): __get_error.argtypes = [] __get_error.restype = c_char_p - res = __mask_tree(left._get_config(), right._get_config()) + res = __mask_tree(left.get_tree(), right.get_tree()) except Exception as e: raise ConfigTreeError(e) if not res: @@ -657,7 +657,7 @@ def validate_tree_filter( __get_error.argtypes = [] __get_error.restype = c_char_p res = __validate_tree_filter( - config_tree._get_config(), cache_path.encode(), validator_dir.encode() + config_tree.get_tree(), cache_path.encode(), validator_dir.encode() ) except Exception as e: raise ConfigTreeError(e) @@ -706,7 +706,7 @@ class DiffTree: check_path(path) path_str = ' '.join(map(str, path)).encode() - res = self.__diff_tree(path_str, left._get_config(), right._get_config()) + res = self.__diff_tree(path_str, left.get_tree(), right.get_tree()) # full diff config_tree and python dict representation self.full = ConfigTree(address=res) -- cgit v1.2.3 From 324badc9f9d8c972ec6da71873d6f47e71279c39 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Tue, 3 Feb 2026 20:47:30 -0600 Subject: T8232: add default reference tree cache file location --- python/vyos/defaults.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'python') diff --git a/python/vyos/defaults.py b/python/vyos/defaults.py index 833ac1fbf..fbaf18919 100644 --- a/python/vyos/defaults.py +++ b/python/vyos/defaults.py @@ -99,3 +99,5 @@ SSH_DSA_DEPRECATION_WARNING: str = \ 'Support for SSH-DSA keys is deprecated and will be removed in VyOS 1.6. ' \ 'Please update affected keys to a supported algorithm (e.g., RSA, ECDSA or ' \ 'ED25519) to avoid authentication failures after the upgrade.' + +reference_tree_cache = '/usr/share/vyos/reftree.cache' -- cgit v1.2.3 From 713ab5ded6a5544c1f19dd7f1d25cdd673e7f52d Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Tue, 3 Feb 2026 20:39:31 -0600 Subject: T8232: add reference tree class As a needed argument to the internal config_dict function, reify for caching under vyos-configd or other running process. --- python/vyos/referencetree.py | 81 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 python/vyos/referencetree.py (limited to 'python') diff --git a/python/vyos/referencetree.py b/python/vyos/referencetree.py new file mode 100644 index 000000000..87ed310b4 --- /dev/null +++ b/python/vyos/referencetree.py @@ -0,0 +1,81 @@ +# Copyright VyOS maintainers and contributors +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this library. If not, see . + +from ctypes import cdll, c_char_p, c_void_p, c_bool + +from vyos.defaults import reference_tree_cache + +LIBPATH = '/usr/lib/libvyosconfig.so.0' + + +class ReferenceTreeError(Exception): + pass + + +class ReferenceTree: + # pylint: disable=too-many-instance-attributes + def __init__(self, cache_file=reference_tree_cache, libpath=LIBPATH): + self.__pointer = None + self.__lib = cdll.LoadLibrary(libpath) + + # Import functions + self.__get_error = self.__lib.get_error + self.__get_error.argtypes = [] + self.__get_error.restype = c_char_p + + self.__read_internal = self.__lib.read_internal_reference_tree + self.__read_internal.argtypes = [c_char_p] + self.__read_internal.restype = c_void_p + + self.__write_internal = self.__lib.write_internal_reference_tree + self.__write_internal.argtypes = [c_void_p, c_char_p] + + self.__to_json = self.__lib.to_json_reference_tree + self.__to_json.argtypes = [c_void_p] + self.__to_json.restype = c_char_p + + self.__destroy = self.__lib.destroy + self.__destroy.argtypes = [c_void_p] + + self.__equal = self.__lib.equal + self.__equal.argtypes = [c_void_p, c_void_p] + self.__equal.restype = c_bool + + pointer = self.__read_internal(cache_file.encode()) + if pointer is None: + msg = self.__get_error().decode() + raise ValueError(f'Failed to read internal rep: {msg}') + self.__pointer = pointer + + def __del__(self): + if self.__pointer is not None: + self.__destroy(self.__pointer) + + def __eq__(self, other): + if isinstance(other, ReferenceTree): + return self.__equal(self.get_tree(), other.get_tree()) + return False + + def __str__(self): + return self.to_json() + + def get_tree(self): + return self.__pointer + + def write_cache(self, file_name): + self.__write_internal(self.get_tree(), file_name.encode()) + + def to_json(self): + return self.__to_json(self.__pointer).decode() -- cgit v1.2.3 From d907ca940d33a4607110e51d3333c9c7068b11f9 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Tue, 3 Feb 2026 20:51:13 -0600 Subject: T8232: add config_dict wrapper --- python/vyos/configtree.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'python') diff --git a/python/vyos/configtree.py b/python/vyos/configtree.py index 03aac72be..afe1d7fe4 100644 --- a/python/vyos/configtree.py +++ b/python/vyos/configtree.py @@ -188,6 +188,17 @@ class ConfigTree(object): self.__equal.argtypes = [c_void_p, c_void_p] self.__equal.restype = c_bool + self.__config_dict = self.__lib.config_dict + self.__config_dict.argtypes = [ + c_void_p, + c_void_p, + c_void_p, + c_char_p, + c_bool, + c_bool, + ] + self.__config_dict.restype = c_char_p + if address is not None: self.__config = address self.__version = '' @@ -472,6 +483,23 @@ class ConfigTree(object): subt = ConfigTree(address=res) return subt + def config_dict( + self, ref_tree, path, mask, get_first_key=False, with_defaults=False + ): + check_path(path) + path_str = ' '.join(map(str, path)).encode() + + res_json = self.__config_dict( + self.__config, + ref_tree.get_tree(), + mask.get_tree(), + path_str, + get_first_key, + with_defaults, + ).decode() + res = json.loads(res_json) + return res + def diff_compare(left, right, path=[], commands=False, libpath=LIBPATH): if left is None: -- cgit v1.2.3