Skip to content

Commit 7aaea74

Browse files
samuel-gauthierrjarry
authored andcommitted
schema: add basic support for choice and case
Let's add enough to parse a model with choices and cases, so that iter_tree is working. Before this patch, the unit test added by this patch would fail with: > ====================================================================== > ERROR: test_iter_tree (test_schema.ContainerTest) > ---------------------------------------------------------------------- > Traceback (most recent call last): > File "(...)/libyang-python/tests/test_schema.py", line 278, in test_iter_tree > tree = list(self.container.iter_tree()) > File "(...)/libyang-python/.tox/py3/lib/python3.9/site-packages/libyang/schema.py", line 1148, in iter_tree > yield self.new(self.context, n) > File "(...)/libyang-python/.tox/py3/lib/python3.9/site-packages/libyang/schema.py", line 1192, in new > raise TypeError("node type %s not implemented" % cdata.nodetype) > TypeError: node type 2 not implemented Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com> Acked-by: Robin Jarry <robin@jarry.cc>
1 parent 30120a3 commit 7aaea74

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

libyang/schema.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,8 @@ class SNode:
878878
__slots__ = ("context", "cdata", "cdata_parsed", "__dict__")
879879

880880
CONTAINER = lib.LYS_CONTAINER
881+
CHOICE = lib.LYS_CHOICE
882+
CASE = lib.LYS_CASE
881883
LEAF = lib.LYS_LEAF
882884
LEAFLIST = lib.LYS_LEAFLIST
883885
LIST = lib.LYS_LIST
@@ -1196,6 +1198,26 @@ def children(self, types: Optional[Tuple[int, ...]] = None) -> Iterator[SNode]:
11961198
return iter_children(self.context, self.cdata, types=types)
11971199

11981200

1201+
# -------------------------------------------------------------------------------------
1202+
@SNode.register(SNode.CHOICE)
1203+
class SChoice(SNode):
1204+
def __iter__(self) -> Iterator[SNode]:
1205+
return self.children()
1206+
1207+
def children(self, types: Optional[Tuple[int, ...]] = None) -> Iterator[SNode]:
1208+
return iter_children(self.context, self.cdata, types=types)
1209+
1210+
1211+
# -------------------------------------------------------------------------------------
1212+
@SNode.register(SNode.CASE)
1213+
class SCase(SNode):
1214+
def __iter__(self) -> Iterator[SNode]:
1215+
return self.children()
1216+
1217+
def children(self, types: Optional[Tuple[int, ...]] = None) -> Iterator[SNode]:
1218+
return iter_children(self.context, self.cdata, types=types)
1219+
1220+
11991221
# -------------------------------------------------------------------------------------
12001222
@SNode.register(SNode.LIST)
12011223
class SList(SNode):

tests/test_diff.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ class DiffTest(unittest.TestCase):
8181
(EnumStatusAdded, "/yolo-system:state/url/proto"),
8282
(EnumStatusRemoved, "/yolo-system:conf/url/proto"),
8383
(EnumStatusRemoved, "/yolo-system:state/url/proto"),
84+
(SNodeAdded, "/yolo-system:conf/pill/red/out"),
85+
(SNodeAdded, "/yolo-system:state/pill/red/out"),
86+
(SNodeAdded, "/yolo-system:conf/pill/blue/in"),
87+
(SNodeAdded, "/yolo-system:state/pill/blue/in"),
88+
(SNodeAdded, "/yolo-system:alarm-triggered/severity"),
89+
(SNodeAdded, "/yolo-system:alarm-triggered/description"),
8490
)
8591
)
8692

tests/test_schema.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,20 +258,20 @@ def test_cont_attrs(self):
258258

259259
def test_cont_iter(self):
260260
children = list(iter(self.container))
261-
self.assertEqual(len(children), 9)
261+
self.assertEqual(len(children), 11)
262262

263263
def test_cont_children_leafs(self):
264264
leafs = list(self.container.children(types=(SNode.LEAF,)))
265-
self.assertEqual(len(leafs), 7)
265+
self.assertEqual(len(leafs), 9)
266266

267267
def test_cont_parent(self):
268268
self.assertIsNone(self.container.parent())
269269

270270
def test_iter_tree(self):
271271
tree = list(self.container.iter_tree())
272-
self.assertEqual(len(tree), 15)
273-
tree = list(self.container.iter_tree(full=True))
274272
self.assertEqual(len(tree), 20)
273+
tree = list(self.container.iter_tree(full=True))
274+
self.assertEqual(len(tree), 25)
275275

276276

277277
# -------------------------------------------------------------------------------------
@@ -455,3 +455,7 @@ def test_leaf_parent(self):
455455
self.assertIsNotNone(parent)
456456
self.assertIsInstance(parent, SList)
457457
self.assertEqual(parent.name(), "url")
458+
459+
def test_iter_tree(self):
460+
leaf = next(self.ctx.find_path("/yolo-system:conf"))
461+
self.assertEqual(len(list(leaf.iter_tree(full=True))), 23)

tests/yang/yolo/yolo-system.yang

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ module yolo-system {
6060
type string;
6161
}
6262

63+
choice pill {
64+
case red {
65+
leaf out {
66+
type boolean;
67+
}
68+
}
69+
case blue {
70+
leaf in {
71+
type boolean;
72+
}
73+
}
74+
}
75+
6376
list url {
6477
description
6578
"An URL.";

0 commit comments

Comments
 (0)