summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2026-04-13 13:26:39 -0500
committerJohn Estabrook <jestabro@vyos.io>2026-05-11 09:03:23 -0500
commitc4fcf52f027d58987951299dc30b95affe52c3e0 (patch)
tree9631f194d140bda82a207c97290a6177eaefbdee /python
parentc02e59b9c655f8b79bfc490a3d1b3527545b99b2 (diff)
downloadvyos-1x-c4fcf52f027d58987951299dc30b95affe52c3e0.tar.gz
vyos-1x-c4fcf52f027d58987951299dc30b95affe52c3e0.zip
T8488: add utility to define exclusion mask from list of partial paths
A partial path is one that may or may not include intervening tag node values. For unspecified tag node values, the matching subtree is returned. For example ['interfaces', 'ethernet', 'address'] will return the subtree of the config for which ethernet tag values have defined address.
Diffstat (limited to 'python')
-rw-r--r--python/vyos/derivedtree.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/python/vyos/derivedtree.py b/python/vyos/derivedtree.py
new file mode 100644
index 000000000..0a57921b2
--- /dev/null
+++ b/python/vyos/derivedtree.py
@@ -0,0 +1,56 @@
+# Copyright (C) VyOS Inc.
+#
+# 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 vyos.referencetree import ReferenceTree
+from vyos.configtree import ConfigTree
+from vyos.configtree import ConfigTreeError
+from vyos.configtree import subtree_from_partial
+
+
+class DerivedTreeError(Exception):
+ """Error to be raised by functions of derivedtree"""
+
+
+def subtree_from_list_of_partial_paths(
+ ctree: ConfigTree, paths: list[list[str]], accumulator: ConfigTree = None
+):
+ """Return the union of subtrees of the ConfigTree argument matching each
+ of the 'partial' paths. A partial path is one that may or may not
+ contain intervening tag node values, in which case it will match for all
+ values that apply.
+
+ An existing subtree may be passed as the initial value of accumulator.
+ """
+ if accumulator:
+ if not isinstance(accumulator, ConfigTree):
+ raise TypeError("Argument 'accumulator' must be an instance of ConfigTree")
+ else:
+ accumulator = ConfigTree('')
+
+ rtree = ReferenceTree()
+
+ errors = []
+ for path in paths:
+ try:
+ accumulator = subtree_from_partial(ctree, path, rtree, accumulator)
+ except ConfigTreeError as e:
+ errors.append(str(e))
+ continue
+
+ if errors:
+ raise DerivedTreeError(f'Nonsensical paths: {errors}')
+
+ return accumulator