summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2026-02-03 20:39:31 -0600
committerJohn Estabrook <jestabro@vyos.io>2026-02-12 08:37:48 -0600
commit713ab5ded6a5544c1f19dd7f1d25cdd673e7f52d (patch)
tree4b6f4acfcda029d842b2963e394e095b5b18ce58 /python
parent324badc9f9d8c972ec6da71873d6f47e71279c39 (diff)
downloadvyos-1x-713ab5ded6a5544c1f19dd7f1d25cdd673e7f52d.tar.gz
vyos-1x-713ab5ded6a5544c1f19dd7f1d25cdd673e7f52d.zip
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.
Diffstat (limited to 'python')
-rw-r--r--python/vyos/referencetree.py81
1 files changed, 81 insertions, 0 deletions
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 <maintainers@vyos.io>
+#
+# 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 <http://www.gnu.org/licenses/>.
+
+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()