Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/multiple fixes #74

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: 3.x
python-version: 3.11
- uses: actions/cache@v3
with:
path: ~/.cache/pip
Expand Down
14 changes: 12 additions & 2 deletions libyang/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,27 @@ def get_module(self, name: str) -> Module:

return Module(self, mod)

def find_path(self, path: str, output: bool = False) -> Iterator[SNode]:
def find_path(
self,
path: str,
output: bool = False,
root_node: Optional["libyang.SNode"] = None,
) -> Iterator[SNode]:
if self.cdata is None:
raise RuntimeError("context already destroyed")

if root_node is not None:
ctx_node = root_node.cdata
else:
ctx_node = ffi.NULL

flags = 0
if output:
flags |= lib.LYS_FIND_XP_OUTPUT

node_set = ffi.new("struct ly_set **")
if (
lib.lys_find_xpath(self.cdata, ffi.NULL, str2c(path), 0, node_set)
lib.lys_find_xpath(self.cdata, ctx_node, str2c(path), 0, node_set)
!= lib.LY_SUCCESS
):
raise self.error("cannot find path")
Expand Down
21 changes: 18 additions & 3 deletions libyang/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,16 +555,27 @@ def typedef(self) -> "Typedef":
return import_module.get_typedef(type_name)
return None

def union_types(self) -> Iterator["Type"]:
def union_types(self, with_typedefs: bool = False) -> Iterator["Type"]:
if self.cdata.basetype != self.UNION:
return

typedef = self.typedef()
t = ffi.cast("struct lysc_type_union *", self.cdata)
if self.cdata_parsed and self.cdata_parsed.types != ffi.NULL:
for union_type, union_type_parsed in zip(
ly_array_iter(t.types), ly_array_iter(self.cdata_parsed.types)
):
yield Type(self.context, union_type, union_type_parsed)
elif (
with_typedefs
and typedef
and typedef.cdata
and typedef.cdata.type.types != ffi.NULL
):
for union_type, union_type_parsed in zip(
ly_array_iter(t.types), ly_array_iter(typedef.cdata.type.types)
):
yield Type(self.context, union_type, union_type_parsed)
else:
for union_type in ly_array_iter(t.types):
yield Type(self.context, union_type, None)
Expand Down Expand Up @@ -1208,7 +1219,7 @@ def __init__(self, context: "libyang.Context", cdata):
self.cdata_leaf = ffi.cast("struct lysc_node_leaf *", cdata)
self.cdata_leaf_parsed = ffi.cast("struct lysp_node_leaf *", self.cdata_parsed)

def default(self) -> Union[None, bool, int, str]:
def default(self) -> Union[None, bool, int, str, float]:
if not self.cdata_leaf.dflt:
return None
val = lib.lyd_value_get_canonical(self.context.cdata, self.cdata_leaf.dflt)
Expand All @@ -1220,6 +1231,8 @@ def default(self) -> Union[None, bool, int, str]:
return val == "true"
if val_type.base() in Type.NUM_TYPES:
return int(val)
if val_type.base() == Type.DEC64:
return float(val)
return val

def units(self) -> Optional[str]:
Expand Down Expand Up @@ -1267,7 +1280,7 @@ def type(self) -> Type:
self.context, self.cdata_leaflist.type, self.cdata_leaflist_parsed.type
)

def defaults(self) -> Iterator[str]:
def defaults(self) -> Iterator[Union[None, bool, int, str, float]]:
if self.cdata_leaflist.dflts == ffi.NULL:
return
arr_length = ffi.cast("uint64_t *", self.cdata_leaflist.dflts)[-1]
Expand All @@ -1283,6 +1296,8 @@ def defaults(self) -> Iterator[str]:
ret = val == "true"
elif val_type in Type.NUM_TYPES:
ret = int(val)
elif val_type == Type.DEC64:
ret = float(val)
yield ret

def must_conditions(self) -> Iterator[str]:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,9 @@ def test_leaf_type_union(self):
self.assertEqual(t.name(), "types:number")
self.assertEqual(t.base(), Type.UNION)
types = set(u.name() for u in t.union_types())
types2 = set(u.name() for u in t.union_types(with_typedefs=True))
self.assertEqual(types, set(["int16", "int32", "uint16", "uint32"]))
self.assertEqual(types2, set(["signed", "unsigned"]))
for u in t.union_types():
ext = u.get_extension(
"type-desc", prefix="omg-extensions", arg_value=f"<{u.name()}>"
Expand Down