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

schema: add support to identities #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions cffi/cdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,17 @@ struct lysp_feature {
uint16_t flags;
};

struct lysp_ident {
const char *name;
struct lysp_qname *iffeatures;
const char **bases;
const char *dsc;
const char *ref;
struct lysp_ext_instance *exts;
uint16_t flags;
};

LY_ERR lys_identity_iffeature_value(const struct lysc_ident *);
LY_ERR lys_feature_value(const struct lys_module *, const char *);
struct lysp_feature* lysp_feature_next(const struct lysp_feature *, const struct lysp_module *, uint32_t *);

Expand Down
52 changes: 52 additions & 0 deletions libyang/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def revisions(self) -> Iterator["Revision"]:
for revision in ly_array_iter(self.cdata.parsed.revs):
yield Revision(self.context, revision, self)

def identities(self) -> Iterator["Identity"]:
for identity in ly_array_iter(self.cdata.parsed.identities):
yield Identity(self.context, identity, self)

def typedefs(self) -> Iterator["Typedef"]:
for typedef in ly_array_iter(self.cdata.parsed.typedefs):
yield Typedef(self.context, typedef)
Expand Down Expand Up @@ -294,6 +298,54 @@ def __str__(self):
return self.date()


# -------------------------------------------------------------------------------------
class Identity:
_slots_ = ("context", "cdata", "module", "_dict_")

def _init_(self, context: "libyang.Context", cdata, module):
self.context = context
self.cdata = cdata # C type: "struct lysp_ident *"
self.module = module

def name(self) -> str:
return c2str(self.cdata.name)

def if_features(self) -> Iterator["IfFeatureExpr"]:
arr_length = ffi.cast("uint64_t *", self.cdata.iffeatures)[-1]
for i in range(arr_length):
yield IfFeatureExpr(self.context, self.cdata.iffeatures[i])

def description(self) -> Optional[str]:
return c2str(self.cdata.dsc)

def reference(self) -> Optional[str]:
return c2str(self.cdata.ref)

def extensions(self) -> Iterator["ExtensionParsed"]:
for ext in ly_array_iter(self.cdata.exts):
yield ExtensionParsed(self.context, ext, self.module)

def get_extension(
self, name: str, prefix: Optional[str] = None, arg_value: Optional[str] = None
) -> Optional["ExtensionParsed"]:
for ext in self.extensions():
if ext.name() != name:
continue
if prefix is not None and ext.module().name() != prefix:
continue
if arg_value is not None and ext.argument() != arg_value:
continue
return ext
return None

def _repr_(self):
cls = self._class_
return "<%s.%s: %s>" % (cls._module, cls.name_, str(self))

def _str_(self):
return self.name()


# -------------------------------------------------------------------------------------
class Import:
__slots__ = ("context", "cdata", "module", "__dict__")
Expand Down
Loading