GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — context: adds find_xpath_atoms function · CESNET/libyang-python@b35336b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • cffi
    • cdefs.h
  • libyang
    • context.py
  • tests
    • test_context.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎cffi/cdefs.h‎

Copy file name to clipboardExpand all lines: cffi/cdefs.h
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ struct lys_module* ly_ctx_get_module_latest(const struct ly_ctx *, const char *)
215215
LY_ERR ly_ctx_compile(struct ly_ctx *);
216216

217217
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 **);
218219
void ly_set_free(struct ly_set *, void(*)(void *obj));
219220

220221
struct ly_set {
Collapse file

‎libyang/context.py‎

Copy file name to clipboardExpand all lines: libyang/context.py
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,38 @@ def find_path(
421421
finally:
422422
lib.ly_set_free(node_set, ffi.NULL)
423423

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+
424456
def find_jsonpath(
425457
self,
426458
path: str,
Collapse file

‎tests/test_context.py‎

Copy file name to clipboardExpand all lines: tests/test_context.py
+17-1Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55
import unittest
66

7-
from libyang import Context, LibyangError, Module, SLeaf, SLeafList
7+
from libyang import Context, LibyangError, Module, SContainer, SLeaf, SLeafList
88
from libyang.util import c2str
99

1010

@@ -95,6 +95,22 @@ def test_ctx_find_path(self):
9595
node2 = next(ctx.find_path("../number", root_node=node))
9696
self.assertIsInstance(node2, SLeafList)
9797

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+
98114
def test_ctx_iter_modules(self):
99115
with Context(YANG_DIR) as ctx:
100116
ctx.load_module("yolo-system")

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.