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