GeezerVison is showing github.com — context: adds find_xpath_atoms function · CESNET/libyang-python@b35336b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: cffi/cdefs.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -215,6 +215,7 @@ struct lys_module* ly_ctx_get_module_latest(const struct ly_ctx *, const char *) | 215 | 215 | LY_ERR ly_ctx_compile(struct ly_ctx *); | 216 | 216 |
| 217 | 217 | LY_ERR lys_find_xpath(const struct ly_ctx *, const struct lysc_node *, const char *, uint32_t, struct ly_set **); | | 218 | +LY_ERR lys_find_xpath_atoms(const struct ly_ctx *, const struct lysc_node *, const char *, uint32_t, struct ly_set **); | 218 | 219 | void ly_set_free(struct ly_set *, void(*)(void *obj)); | 219 | 220 |
| 220 | 221 | struct ly_set { | |
Collapse file Copy file name to clipboardExpand all lines: libyang/context.py +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -421,6 +421,38 @@ def find_path( | 421 | 421 | finally: | 422 | 422 | lib.ly_set_free(node_set, ffi.NULL) | 423 | 423 |
| | 424 | + def find_xpath_atoms( | | 425 | + self, | | 426 | + path: str, | | 427 | + output: bool = False, | | 428 | + root_node: Optional["libyang.SNode"] = None, | | 429 | + ) -> Iterator[SNode]: | | 430 | + if self.cdata is None: | | 431 | + raise RuntimeError("context already destroyed") | | 432 | + | | 433 | + if root_node is not None: | | 434 | + ctx_node = root_node.cdata | | 435 | + else: | | 436 | + ctx_node = ffi.NULL | | 437 | + | | 438 | + flags = lib.LYS_FIND_XP_OUTPUT if output else 0 | | 439 | + | | 440 | + node_set = ffi.new("struct ly_set **") | | 441 | + if ( | | 442 | + lib.lys_find_xpath_atoms(self.cdata, ctx_node, str2c(path), flags, node_set) | | 443 | + != lib.LY_SUCCESS | | 444 | + ): | | 445 | + raise self.error("cannot find path") | | 446 | + | | 447 | + node_set = node_set[0] | | 448 | + if node_set.count == 0: | | 449 | + raise self.error("cannot find path") | | 450 | + try: | | 451 | + for i in range(node_set.count): | | 452 | + yield SNode.new(self, node_set.snodes[i]) | | 453 | + finally: | | 454 | + lib.ly_set_free(node_set, ffi.NULL) | | 455 | + | 424 | 456 | def find_jsonpath( | 425 | 457 | self, | 426 | 458 | path: str, | |
Collapse file Copy file name to clipboardExpand all lines: tests/test_context.py +17-1Lines changed: 17 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | import os | 5 | 5 | import unittest | 6 | 6 |
| 7 | | -from libyang import Context, LibyangError, Module, SLeaf, SLeafList | | 7 | +from libyang import Context, LibyangError, Module, SContainer, SLeaf, SLeafList | 8 | 8 | from libyang.util import c2str | 9 | 9 |
| 10 | 10 |
| @@ -95,6 +95,22 @@ def test_ctx_find_path(self): | 95 | 95 | node2 = next(ctx.find_path("../number", root_node=node)) | 96 | 96 | self.assertIsInstance(node2, SLeafList) | 97 | 97 |
| | 98 | + def test_ctx_find_xpath_atoms(self): | | 99 | + with Context(YANG_DIR) as ctx: | | 100 | + ctx.load_module("yolo-system") | | 101 | + node_iter = ctx.find_xpath_atoms("/yolo-system:conf/offline") | | 102 | + node = next(node_iter) | | 103 | + self.assertIsInstance(node, SContainer) | | 104 | + node = next(node_iter) | | 105 | + self.assertIsInstance(node, SLeaf) | | 106 | + node_iter = ctx.find_xpath_atoms("../number", root_node=node) | | 107 | + node = next(node_iter) | | 108 | + self.assertIsInstance(node, SLeaf) | | 109 | + node = next(node_iter) | | 110 | + self.assertIsInstance(node, SContainer) | | 111 | + node = next(node_iter) | | 112 | + self.assertIsInstance(node, SLeafList) | | 113 | + | 98 | 114 | def test_ctx_iter_modules(self): | 99 | 115 | with Context(YANG_DIR) as ctx: | 100 | 116 | ctx.load_module("yolo-system") | |
|