Skip to content

Commit

Permalink
schema: adding parent_node API to extensions
Browse files Browse the repository at this point in the history
This patch adds parent_node API, which allows caller to get parent PNode
from PExtension or SNode from Extension instance.

Signed-off-by: Stefan Gula <[email protected]>
Signed-off-by: Samuel Gauthier <[email protected]>
  • Loading branch information
steweg authored and samuel-gauthier committed Sep 27, 2024
1 parent 8d64c70 commit adab39c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cffi/cdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ enum ly_stmt {
LY_STMT_ARG_VALUE
};

#define LY_STMT_NODE_MASK ...

#define LY_LOLOG ...
#define LY_LOSTORE ...
#define LY_LOSTORE_LAST ...
Expand Down
1 change: 1 addition & 0 deletions libyang/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
from .log import configure_logging
from .schema import (
Extension,
ExtensionParsed,
Feature,
IfAndFeatures,
IfFeature,
Expand Down
16 changes: 16 additions & 0 deletions libyang/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,14 @@ def name(self) -> str:
def module(self) -> Module:
return self._module_from_parsed()

def parent_node(self) -> Optional["PNode"]:
if not bool(self.cdata.parent_stmt & lib.LY_STMT_NODE_MASK):
return None
try:
return PNode.new(self.context, self.cdata.parent, self.module())
except LibyangError:
return None


# -------------------------------------------------------------------------------------
class ExtensionCompiled(Extension):
Expand All @@ -428,6 +436,14 @@ def module(self) -> Module:
raise self.context.error("cannot get module")
return Module(self.context, self.cdata_def.module)

def parent_node(self) -> Optional["SNode"]:
if not bool(self.cdata.parent_stmt & lib.LY_STMT_NODE_MASK):
return None
try:
return SNode.new(self.context, self.cdata.parent)
except LibyangError:
return None


# -------------------------------------------------------------------------------------
class _EnumBit:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from libyang import (
Context,
Extension,
ExtensionParsed,
IfFeature,
IfOrFeatures,
IOType,
Expand Down Expand Up @@ -496,6 +497,11 @@ def test_rpc_extensions(self):
self.assertEqual(len(ext), 1)
ext = self.rpc.get_extension("require-admin", prefix="omg-extensions")
self.assertIsInstance(ext, Extension)
self.assertIsInstance(ext.parent_node(), SRpc)
parsed = self.rpc.parsed()
ext = parsed.get_extension("require-admin", prefix="omg-extensions")
self.assertIsInstance(ext, ExtensionParsed)
self.assertIsInstance(ext.parent_node(), PAction)

def test_rpc_params(self):
leaf = next(self.rpc.children())
Expand Down Expand Up @@ -609,6 +615,7 @@ def test_leaf_type_extensions(self):
"type-desc", prefix="omg-extensions", arg_value="<protocol>"
)
self.assertIsInstance(ext, Extension)
self.assertIsNone(ext.parent_node())

def test_leaf_type_enum(self):
leaf = next(
Expand Down

0 comments on commit adab39c

Please sign in to comment.