diff --git a/libyang/schema.py b/libyang/schema.py index c6d822c2..6b0c803b 100644 --- a/libyang/schema.py +++ b/libyang/schema.py @@ -1034,6 +1034,10 @@ class SNode: ANYDATA: "anydata", } + PATH_LOG = lib.LYSC_PATH_LOG + PATH_DATA = lib.LYSC_PATH_DATA + PATH_DATA_PATTERN = lib.LYSC_PATH_DATA_PATTERN + def __init__(self, context: "libyang.Context", cdata): self.context = context self.cdata = cdata # C type: "struct lysc_node *" @@ -1079,22 +1083,19 @@ def status(self) -> str: def module(self) -> Module: return Module(self.context, self.cdata.module) - def schema_path(self) -> str: + def schema_path(self, path_type: int = PATH_LOG) -> str: try: - s = lib.lysc_path(self.cdata, lib.LYSC_PATH_LOG, ffi.NULL, 0) + s = lib.lysc_path(self.cdata, path_type, ffi.NULL, 0) return c2str(s) finally: lib.free(s) def data_path(self, key_placeholder: str = "'%s'") -> str: - try: - s = lib.lysc_path(self.cdata, lib.LYSC_PATH_DATA_PATTERN, ffi.NULL, 0) - val = c2str(s) - if key_placeholder != "'%s'": - val = val.replace("'%s'", key_placeholder) - return val - finally: - lib.free(s) + val = self.schema_path(self.PATH_DATA_PATTERN) + + if key_placeholder != "'%s'": + val = val.replace("'%s'", key_placeholder) + return val def extensions(self) -> Iterator[ExtensionCompiled]: ext = ffi.cast("struct lysc_ext_instance *", self.cdata.exts) diff --git a/tests/test_schema.py b/tests/test_schema.py index b88f0092..f493ba14 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -282,13 +282,16 @@ def test_iter_tree(self): # ------------------------------------------------------------------------------------- class ListTest(unittest.TestCase): - SCHEMA_PATH = "/yolo-system:conf/url" - DATA_PATH = "/yolo-system:conf/url[host='%s'][proto='%s']" + PATH = { + "LOG": "/yolo-system:conf/url", + "DATA": "/yolo-system:conf/url", + "DATA_PATTERN": "/yolo-system:conf/url[host='%s'][proto='%s']", + } def setUp(self): self.ctx = Context(YANG_DIR) self.ctx.load_module("yolo-system") - self.list = next(self.ctx.find_path(self.SCHEMA_PATH)) + self.list = next(self.ctx.find_path(self.PATH["LOG"])) def tearDown(self): self.list = None @@ -300,9 +303,11 @@ def test_list_attrs(self): self.assertEqual(self.list.nodetype(), SNode.LIST) self.assertEqual(self.list.keyword(), "list") - self.assertEqual(self.list.schema_path(), self.SCHEMA_PATH) + self.assertEqual(self.list.schema_path(), self.PATH["LOG"]) - self.assertEqual(self.list.data_path(), self.DATA_PATH) + self.assertEqual(self.list.schema_path(SNode.PATH_DATA), self.PATH["DATA"]) + + self.assertEqual(self.list.data_path(), self.PATH["DATA_PATTERN"]) self.assertFalse(self.list.ordered()) def test_list_keys(self):