Skip to content

Commit 7dea0b2

Browse files
samuel-gauthierrjarry
authored andcommitted
schema: add module import
Parse the import section of a module in an Import class. Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com> Acked-by: Robin Jarry <robin@jarry.cc>
1 parent 7aaea74 commit 7dea0b2

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

libyang/schema.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ def revisions(self) -> Iterator["Revision"]:
112112
for revision in ly_array_iter(self.cdata.parsed.revs):
113113
yield Revision(self.context, revision, self)
114114

115+
def imports(self) -> Iterator["Import"]:
116+
for i in ly_array_iter(self.cdata.parsed.imports):
117+
yield Import(self.context, i, self)
118+
115119
def __iter__(self) -> Iterator["SNode"]:
116120
return self.children()
117121

@@ -268,6 +272,52 @@ def __str__(self):
268272
return self.date()
269273

270274

275+
# -------------------------------------------------------------------------------------
276+
class Import:
277+
__slots__ = ("context", "cdata", "module", "__dict__")
278+
279+
def __init__(self, context: "libyang.Context", cdata, module):
280+
self.context = context
281+
self.cdata = cdata # C type: "struct lysp_revision *"
282+
self.module = module
283+
284+
def name(self) -> str:
285+
return c2str(self.cdata.name)
286+
287+
def prefix(self) -> Optional[str]:
288+
return c2str(self.cdata.prefix)
289+
290+
def description(self) -> Optional[str]:
291+
return c2str(self.cdata.dsc)
292+
293+
def reference(self) -> Optional[str]:
294+
return c2str(self.cdata.ref)
295+
296+
def extensions(self) -> Iterator["ExtensionParsed"]:
297+
for ext in ly_array_iter(self.cdata.exts):
298+
yield ExtensionParsed(self.context, ext, self.module)
299+
300+
def get_extension(
301+
self, name: str, prefix: Optional[str] = None, arg_value: Optional[str] = None
302+
) -> Optional["ExtensionParsed"]:
303+
for ext in self.extensions():
304+
if ext.name() != name:
305+
continue
306+
if prefix is not None and ext.module().name() != prefix:
307+
continue
308+
if arg_value is not None and ext.argument() != arg_value:
309+
continue
310+
return ext
311+
return None
312+
313+
def __repr__(self):
314+
cls = self.__class__
315+
return "<%s.%s: %s>" % (cls.__module__, cls.__name__, str(self))
316+
317+
def __str__(self):
318+
return self.name()
319+
320+
271321
# -------------------------------------------------------------------------------------
272322
class Extension:
273323
__slots__ = ("context", "cdata", "__dict__")

tests/test_schema.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ def test_mod_enable_features(self):
7373
self.assertTrue(self.module.feature_state("turbo-boost"))
7474
self.module.feature_disable_all()
7575

76+
def test_mod_imports(self):
77+
imports = list(self.module.imports())
78+
self.assertEqual(imports[0].name(), "omg-extensions")
79+
self.assertEqual(imports[1].name(), "wtf-types")
80+
self.assertEqual(len(imports), 2)
81+
7682
def test_mod_features(self):
7783
features = list(self.module.features())
7884
self.assertEqual(len(features), 2)

0 commit comments

Comments
 (0)