From 7ca1516ed85e2aaab6f646469d00a9d2b2e2da1b Mon Sep 17 00:00:00 2001 From: Pedro Henrique de Souza Date: Thu, 11 Apr 2024 12:46:33 -0300 Subject: [PATCH] schema: add support to identities Enhances the functionality of the schema by providing access to identities information. Signed-off-by: Pedro Henrique de Souza --- cffi/cdefs.h | 11 ++++++++++ libyang/schema.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/cffi/cdefs.h b/cffi/cdefs.h index e5bcc935..13b6367e 100644 --- a/cffi/cdefs.h +++ b/cffi/cdefs.h @@ -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 *); diff --git a/libyang/schema.py b/libyang/schema.py index deec466d..4c071f21 100644 --- a/libyang/schema.py +++ b/libyang/schema.py @@ -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) @@ -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__")