Skip to content

Commit fd7a990

Browse files
samuel-gauthierrjarry
authored andcommitted
schema: add get_module_from_prefix to module class
This new method from the Module class gives a module given the prefix defined in the import section. Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com> Acked-by: Robin Jarry <robin@jarry.cc>
1 parent fdfabf9 commit fd7a990

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

libyang/schema.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ def imports(self) -> Iterator["Import"]:
116116
for i in ly_array_iter(self.cdata.parsed.imports):
117117
yield Import(self.context, i, self)
118118

119+
def get_module_from_prefix(self, prefix: str) -> Optional["Module"]:
120+
for i in self.imports():
121+
if i.prefix() != prefix:
122+
continue
123+
return self.context.get_module(i.name())
124+
return None
125+
119126
def __iter__(self) -> Iterator["SNode"]:
120127
return self.children()
121128

tests/test_schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ def test_leaf_type_derived(self):
379379
mod = t.module()
380380
self.assertIsNot(mod, None)
381381
self.assertEqual(mod.name(), "yolo-system")
382+
self.assertEqual(mod.get_module_from_prefix("types").name(), "wtf-types")
382383

383384
def test_leaf_type_status(self):
384385
leaf = next(self.ctx.find_path("/yolo-system:conf/yolo-system:hostname"))

0 commit comments

Comments
 (0)