Skip to content

Commit

Permalink
schema: add fraction_digits support
Browse files Browse the repository at this point in the history
This patch introduces fraction_digits() and all_fraction_digits()
functions for SLeaf and SLeafList.

Fixes: #86
Signed-off-by: Stefan Gula <[email protected]>
Signed-off-by: Samuel Gauthier <[email protected]>
  • Loading branch information
steweg authored and samuel-gauthier committed Jan 26, 2024
1 parent d5f48d6 commit b62fb0d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions libyang/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,22 @@ def all_ranges(self) -> Iterator[str]:
if rng is not None:
yield rng

def fraction_digits(self) -> Optional[int]:
if not self.cdata_parsed:
return None
if self.cdata.basetype != self.DEC64:
return None
return self.cdata_parsed.fraction_digits

def all_fraction_digits(self) -> Iterator[int]:
if self.cdata.basetype == lib.LY_TYPE_UNION:
for t in self.union_types():
yield from t.all_fraction_digits()
else:
fd = self.fraction_digits()
if fd is not None:
yield fd

STR_TYPES = frozenset((STRING, BINARY, ENUM, IDENT, BITS))

def length(self) -> Optional[str]:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,14 @@ def test_iter_tree(self):
leaf = next(self.ctx.find_path("/yolo-system:conf"))
self.assertEqual(len(list(leaf.iter_tree(full=True))), 23)

def test_leaf_type_fraction_digits(self):
self.ctx.load_module("yolo-nodetypes")
leaf = next(self.ctx.find_path("/yolo-nodetypes:conf/percentage"))
self.assertIsInstance(leaf, SLeaf)
t = leaf.type()
self.assertIsInstance(t, Type)
self.assertEqual(next(t.all_fraction_digits(), None), 2)


# -------------------------------------------------------------------------------------
class LeafTest(unittest.TestCase):
Expand Down

0 comments on commit b62fb0d

Please sign in to comment.