Skip to content

Commit b62fb0d

Browse files
stewegsamuel-gauthier
authored andcommitted
schema: add fraction_digits support
This patch introduces fraction_digits() and all_fraction_digits() functions for SLeaf and SLeafList. Fixes: CESNET#86 Signed-off-by: Stefan Gula <steweg@gmail.com> Signed-off-by: Samuel Gauthier <samuel.gauthier@6wind.com>
1 parent d5f48d6 commit b62fb0d

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

libyang/schema.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,22 @@ def all_ranges(self) -> Iterator[str]:
622622
if rng is not None:
623623
yield rng
624624

625+
def fraction_digits(self) -> Optional[int]:
626+
if not self.cdata_parsed:
627+
return None
628+
if self.cdata.basetype != self.DEC64:
629+
return None
630+
return self.cdata_parsed.fraction_digits
631+
632+
def all_fraction_digits(self) -> Iterator[int]:
633+
if self.cdata.basetype == lib.LY_TYPE_UNION:
634+
for t in self.union_types():
635+
yield from t.all_fraction_digits()
636+
else:
637+
fd = self.fraction_digits()
638+
if fd is not None:
639+
yield fd
640+
625641
STR_TYPES = frozenset((STRING, BINARY, ENUM, IDENT, BITS))
626642

627643
def length(self) -> Optional[str]:

tests/test_schema.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,14 @@ def test_iter_tree(self):
477477
leaf = next(self.ctx.find_path("/yolo-system:conf"))
478478
self.assertEqual(len(list(leaf.iter_tree(full=True))), 23)
479479

480+
def test_leaf_type_fraction_digits(self):
481+
self.ctx.load_module("yolo-nodetypes")
482+
leaf = next(self.ctx.find_path("/yolo-nodetypes:conf/percentage"))
483+
self.assertIsInstance(leaf, SLeaf)
484+
t = leaf.type()
485+
self.assertIsInstance(t, Type)
486+
self.assertEqual(next(t.all_fraction_digits(), None), 2)
487+
480488

481489
# -------------------------------------------------------------------------------------
482490
class LeafTest(unittest.TestCase):

0 commit comments

Comments
 (0)