How to use the pyang.translators.schemanode.SchemaNode function in pyang

To help you get started, we’ve selected a few pyang examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mbj4668 / pyang / pyang / translators / dsdl.py View on Github external
def leaf_list_stmt(self, stmt, p_elem, pset):
        lelem = SchemaNode.leaf_list(self.qname(stmt), p_elem)
        lelem.attr["nma:leaf-list"] = "true"
        if self.has_meta:
            lelem.annot(
                SchemaNode("ref").set_attr("name", "__yang_metadata__"))
        refd = self.process_patches(pset, stmt, lelem)[0]
        lelem.minEl, lelem.maxEl = self.get_minmax(stmt, refd)
        if int(lelem.minEl) > 0: self.propagate_occur(p_elem, 2)
        self.handle_substmts(stmt, lelem)
github mbj4668 / pyang / pyang / translators / dsdl.py View on Github external
def create_roots(self, yam):
        """Create the top-level structure for module `yam`."""
        self.local_grammar = SchemaNode("grammar")
        self.local_grammar.attr = {
            "ns": yam.search_one("namespace").arg,
            "nma:module": self.module.arg}
        src_text = "YANG module '%s'" % yam.arg
        revs = yam.search("revision")
        if len(revs) > 0:
            src_text += " revision %s" % self.current_revision(revs)
        self.dc_element(self.local_grammar, "source", src_text)
        start = SchemaNode("start", self.local_grammar)
        self.data = SchemaNode("nma:data", start, interleave=True)
        self.data.occur = 2
        self.rpcs = SchemaNode("nma:rpcs", start, interleave=False)
        self.notifications = SchemaNode("nma:notifications", start,
                                        interleave=False)
github mbj4668 / pyang / pyang / translators / dsdl.py View on Github external
"""Handle substatements of augments from `auglist`.

        The augments are applied in the context of `p_elem`.  `pset`
        is a patch set containing patches that may be applicable to
        descendants.
        """
        for a in auglist:
            par = a.parent
            if a.search_one("when") is None:
                wel = p_elem
            else:
                if p_elem.interleave:
                    kw = "interleave"
                else:
                    kw = "group"
                wel = SchemaNode(kw, p_elem, interleave=p_elem.interleave)
                wel.occur = p_elem.occur
            if par.keyword == "uses":
                self.handle_substmts(a, wel, pset)
                continue
            if par.keyword == "submodule":
                mnam = par.i_including_modulename
            else:
                mnam = par.arg
            if self.prefix_stack[-1] == self.module_prefixes[mnam]:
                self.handle_substmts(a, wel, pset)
            else:
                self.prefix_stack.append(self.module_prefixes[mnam])
                self.handle_substmts(a, wel, pset)
                self.prefix_stack.pop()
github mbj4668 / pyang / pyang / translators / dsdl.py View on Github external
"""Add pattern def for `id_stmt` and all derived identities.

        The corresponding "ref" pattern is returned.
        """
        p = self.add_namespace(id_stmt.main_module())
        if id_stmt not in self.identities:   # add named pattern def
            self.identities[id_stmt] = SchemaNode.define("__%s_%s" %
                                                         (p, id_stmt.arg))
            parent = self.identities[id_stmt]
            if id_stmt in self.identity_deps:
                parent = SchemaNode.choice(parent, occur=2)
                for i in self.identity_deps[id_stmt]:
                    parent.subnode(self.add_derived_identity(i))
            idval = SchemaNode("value", parent, p+":"+id_stmt.arg)
            idval.attr["type"] = "QName"
        res = SchemaNode("ref")
        res.attr["name"] = self.identities[id_stmt].attr["name"]
        return res
github mbj4668 / pyang / pyang / translators / dsdl.py View on Github external
def bits_type(self, tchain, p_elem):
        elem = SchemaNode("list", p_elem)
        zom = SchemaNode("zeroOrMore", elem)
        choi = SchemaNode.choice(zom, occur=2)
        for bit in tchain[0].search("bit"):
            SchemaNode("value", choi, bit.arg)
github mbj4668 / pyang / pyang / translators / dsdl.py View on Github external
def add_derived_identity(self, id_stmt):
        """Add pattern def for `id_stmt` and all derived identities.

        The corresponding "ref" pattern is returned.
        """
        p = self.add_namespace(id_stmt.main_module())
        if id_stmt not in self.identities:   # add named pattern def
            self.identities[id_stmt] = SchemaNode.define("__%s_%s" %
                                                         (p, id_stmt.arg))
            parent = self.identities[id_stmt]
            if id_stmt in self.identity_deps:
                parent = SchemaNode.choice(parent, occur=2)
                for i in self.identity_deps[id_stmt]:
                    parent.subnode(self.add_derived_identity(i))
            idval = SchemaNode("value", parent, p+":"+id_stmt.arg)
            idval.attr["type"] = "QName"
        res = SchemaNode("ref")
        res.attr["name"] = self.identities[id_stmt].attr["name"]
        return res
github mbj4668 / pyang / pyang / translators / dsdl.py View on Github external
def rpc_stmt(self, stmt, p_elem, pset):
        rpcel = SchemaNode("nma:rpc", self.rpcs)
        r_pset = self.process_patches(pset, stmt, rpcel)[2]
        inpel = SchemaNode("nma:input", rpcel)
        elem = SchemaNode.element(self.qname(stmt), inpel, occur=2)
        augs, pset = self.process_patches(r_pset,stmt,elem,"input")[1:]
        inst = stmt.search_one("input")
        if inst:
            self.handle_substmts(inst, elem, pset)
        else:
            SchemaNode("empty", elem)
        self.apply_augments(augs, elem, pset)
        augs, pset = self.process_patches(r_pset,stmt,None,"output")[1:]
        oust = stmt.search_one("output")
        if oust or augs:
            outel = SchemaNode("nma:output", rpcel)
            outel.occur = 2
            if oust: self.handle_substmts(oust, outel, pset)
github mbj4668 / pyang / pyang / translators / dsdl.py View on Github external
def range_params(self, ran, kw):
        """Return list of s corresponding to range `ran`.

        `kw` is the statement keyword determining the type of the
        range, i.e. 'range' or 'length'. `ran` is the internal
        representation of a range as constructed by the `get_ranges`
        method.
        """
        if kw == "length":
            if ran[0][0] != "m" and (len(ran) == 1 or ran[0] == ran[1]):
                elem = SchemaNode("param").set_attr("name","length")
                elem.text = ran[0]
                return [elem]
            min_ = SchemaNode("param").set_attr("name","minLength")
            max_ = SchemaNode("param").set_attr("name","maxLength")
        else:
            if len(ran) == 1: ran *= 2 # duplicating the value
            min_ = SchemaNode("param").set_attr("name","minInclusive")
            max_ = SchemaNode("param").set_attr("name","maxInclusive")
        res = []
        if ran[0][0] != "m":
            elem = min_
            elem.text = ran[0]
            res.append(elem)
        if ran[1][0] != "m":
            elem = max_
            elem.text = ran[1]
            res.append(elem)
        return res
github mbj4668 / pyang / pyang / translators / dsdl.py View on Github external
def type_stmt(self, stmt, p_elem, pset):
        """Handle ``type`` statement.

        Built-in types are handled by one of the specific type
        callback methods defined below.
        """
        typedef = stmt.i_typedef
        if typedef and not stmt.i_is_derived: # just ref
            uname, dic = self.unique_def_name(typedef)
            if uname not in dic:
                self.install_def(uname, typedef, dic)
            SchemaNode("ref", p_elem).set_attr("name", uname)
            defst = typedef.search_one("default")
            if defst:
                dic[uname].default = defst.arg
                occur = 1
            else:
                occur = dic[uname].occur
            if occur > 0: self.propagate_occur(p_elem, occur)
            return
        chain = [stmt]
        tdefault = None
        while typedef:
            type_ = typedef.search_one("type")
            chain.insert(0, type_)
            if tdefault is None:
                tdef = typedef.search_one("default")
                if tdef: