summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2026-04-12 16:23:55 -0500
committerJohn Estabrook <jestabro@vyos.io>2026-05-11 09:03:23 -0500
commitc02e59b9c655f8b79bfc490a3d1b3527545b99b2 (patch)
tree7aac1a50524fe54b60a725603b41d481a824c4fd /python
parent50e09bdf1ab2344c119d2e6c50375f44faaf4a6c (diff)
downloadvyos-1x-c02e59b9c655f8b79bfc490a3d1b3527545b99b2.tar.gz
vyos-1x-c02e59b9c655f8b79bfc490a3d1b3527545b99b2.zip
T8488: add wrapper for subtree_from_partial
Diffstat (limited to 'python')
-rw-r--r--python/vyos/configtree.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/python/vyos/configtree.py b/python/vyos/configtree.py
index 2876130cf..f93ec668b 100644
--- a/python/vyos/configtree.py
+++ b/python/vyos/configtree.py
@@ -18,6 +18,12 @@ import json
import logging
from ctypes import cdll, c_char_p, c_void_p, c_int, c_bool
+from typing import TYPE_CHECKING
+
+# https://peps.python.org/pep-0484/#forward-references
+# for type 'ConfigDict'
+if TYPE_CHECKING:
+ from vyos.referencetree import ReferenceTree
BUILD_PATH = '/tmp/libvyosconfig/_build/libvyosconfig.so'
INSTALL_PATH = '/usr/lib/libvyosconfig.so.0'
@@ -627,6 +633,49 @@ def mask_exclusive(left, right, libpath=LIBPATH):
return tree
+def subtree_from_partial(
+ config_tree: ConfigTree,
+ path: list[str],
+ reference_tree: 'ReferenceTree',
+ start: ConfigTree = None,
+ libpath=LIBPATH,
+):
+ if start:
+ if not isinstance(start, ConfigTree):
+ raise TypeError("Argument 'start' must be an instance of ConfigTree")
+ else:
+ start = ConfigTree('')
+
+ check_path(path)
+ path_str = ' '.join(map(str, path)).encode()
+
+ try:
+ __lib = cdll.LoadLibrary(libpath)
+ __subtree_from_partial = __lib.subtree_from_partial
+ __subtree_from_partial.argtypes = [c_void_p, c_void_p, c_void_p, c_char_p]
+ __subtree_from_partial.restype = c_void_p
+ __get_error = __lib.get_error
+ __get_error.argtypes = []
+ __get_error.restype = c_char_p
+
+ res = __subtree_from_partial(
+ reference_tree.get_tree(),
+ config_tree.get_tree(),
+ start.get_tree(),
+ path_str,
+ )
+ except Exception as e:
+ raise ConfigTreeError(e)
+ if not res:
+ msg = __get_error().decode()
+ # msg is space separated list of path
+ raise ConfigTreeError(msg.split())
+
+ tree = ConfigTree(address=res)
+
+ return tree
+
+
def reference_tree_to_json(from_dir, to_file, internal_cache='', libpath=LIBPATH):
try:
__lib = cdll.LoadLibrary(libpath)