Skip to content

Commit 097412c

Browse files
MatthieuTdO-6WINDrjarry
authored andcommitted
schema: enable getting node data path without list key
There is no way to get a node data path (path without choice/case) without list keys. Adds the path_type parameter to the SNode.schema_path() method. This parameter can takes 3 values: - SNode.PATH_LOG: returns the path with schema-only nodes (choice, case) included, the default - SNode.PATH_DATA: returns the path without schema-only nodes - SNode.PATH_DATA_PATTERN: similar to PATH_DATA with list keys added (the one used by data_path()) The SNode.PATH_LOG is set by default to not change the original behavior. The SNode.data_path() method now calls SNode.schema_path() with self.PATH_DATA_PATTERN instead of lib.lysc_path(). Here is an example of the output difference between schema_path(), data_path(), and schema_path(path_type=SNode.PATH_DATA) with a node included in a choice and a list: node.schema_path(): /ietf-keystore:keystore/asymmetric-keys/asymmetric-key/private-key-type/private-key/private-key node.data_path() or node.schema_path(SNode.PATH_DATA_PATTERN): /ietf-keystore:keystore/asymmetric-keys/asymmetric-key[name='%s']/private-key node.schema_path(SNode.PATH_DATA): /ietf-keystore:keystore/asymmetric-keys/asymmetric-key/private-key Tests have been updated accordingly. Signed-off-by: Matthieu Ternisien d'Ouville <matthieu.tdo@6wind.com>
1 parent a2159c8 commit 097412c

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

libyang/schema.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,10 @@ class SNode:
10341034
ANYDATA: "anydata",
10351035
}
10361036

1037+
PATH_LOG = lib.LYSC_PATH_LOG
1038+
PATH_DATA = lib.LYSC_PATH_DATA
1039+
PATH_DATA_PATTERN = lib.LYSC_PATH_DATA_PATTERN
1040+
10371041
def __init__(self, context: "libyang.Context", cdata):
10381042
self.context = context
10391043
self.cdata = cdata # C type: "struct lysc_node *"
@@ -1079,22 +1083,19 @@ def status(self) -> str:
10791083
def module(self) -> Module:
10801084
return Module(self.context, self.cdata.module)
10811085

1082-
def schema_path(self) -> str:
1086+
def schema_path(self, path_type: int = PATH_LOG) -> str:
10831087
try:
1084-
s = lib.lysc_path(self.cdata, lib.LYSC_PATH_LOG, ffi.NULL, 0)
1088+
s = lib.lysc_path(self.cdata, path_type, ffi.NULL, 0)
10851089
return c2str(s)
10861090
finally:
10871091
lib.free(s)
10881092

10891093
def data_path(self, key_placeholder: str = "'%s'") -> str:
1090-
try:
1091-
s = lib.lysc_path(self.cdata, lib.LYSC_PATH_DATA_PATTERN, ffi.NULL, 0)
1092-
val = c2str(s)
1093-
if key_placeholder != "'%s'":
1094-
val = val.replace("'%s'", key_placeholder)
1095-
return val
1096-
finally:
1097-
lib.free(s)
1094+
val = self.schema_path(self.PATH_DATA_PATTERN)
1095+
1096+
if key_placeholder != "'%s'":
1097+
val = val.replace("'%s'", key_placeholder)
1098+
return val
10981099

10991100
def extensions(self) -> Iterator[ExtensionCompiled]:
11001101
ext = ffi.cast("struct lysc_ext_instance *", self.cdata.exts)

tests/test_schema.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,13 +282,16 @@ def test_iter_tree(self):
282282

283283
# -------------------------------------------------------------------------------------
284284
class ListTest(unittest.TestCase):
285-
SCHEMA_PATH = "/yolo-system:conf/url"
286-
DATA_PATH = "/yolo-system:conf/url[host='%s'][proto='%s']"
285+
PATH = {
286+
"LOG": "/yolo-system:conf/url",
287+
"DATA": "/yolo-system:conf/url",
288+
"DATA_PATTERN": "/yolo-system:conf/url[host='%s'][proto='%s']",
289+
}
287290

288291
def setUp(self):
289292
self.ctx = Context(YANG_DIR)
290293
self.ctx.load_module("yolo-system")
291-
self.list = next(self.ctx.find_path(self.SCHEMA_PATH))
294+
self.list = next(self.ctx.find_path(self.PATH["LOG"]))
292295

293296
def tearDown(self):
294297
self.list = None
@@ -300,9 +303,11 @@ def test_list_attrs(self):
300303
self.assertEqual(self.list.nodetype(), SNode.LIST)
301304
self.assertEqual(self.list.keyword(), "list")
302305

303-
self.assertEqual(self.list.schema_path(), self.SCHEMA_PATH)
306+
self.assertEqual(self.list.schema_path(), self.PATH["LOG"])
304307

305-
self.assertEqual(self.list.data_path(), self.DATA_PATH)
308+
self.assertEqual(self.list.schema_path(SNode.PATH_DATA), self.PATH["DATA"])
309+
310+
self.assertEqual(self.list.data_path(), self.PATH["DATA_PATTERN"])
306311
self.assertFalse(self.list.ordered())
307312

308313
def test_list_keys(self):

0 commit comments

Comments
 (0)