How to use the pyang.util 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 ansible-network / network-engine / lookup_plugins / yang2spec.py View on Github external
if i.arg == "identityref":
                    subtypes.append(i.search_one('base'))
                else:
                    subtypes.append(i)
        elif base_t.arg == "identityref":
            subtypes = [base_t.search_one('base')]
        else:
            subtypes = [base_t]

        any_unknown = False
        for i in subtypes:
            # Resolve this typedef to the module that it
            # was defined by

            if ":" in i.arg:
                defining_module = util.prefix_to_module(defnd[t].i_module,
                                                        i.arg.split(":")[0], defnd[t].pos, ctx.errors)
            else:
                defining_module = defnd[t].i_module

            belongs_to = defining_module.search_one('belongs-to')
            if belongs_to is not None:
                for mod in ctx.modules:
                    if mod[0] == belongs_to.arg:
                        defining_module = ctx.modules[mod]

            real_pfx = defining_module.search_one('prefix').arg

            if ":" in i.arg:
                tn = u"%s:%s" % (real_pfx, i.arg.split(":")[1])
            elif i.arg not in base_types:
                # If this was not a base type (defined in YANG) then resolve it
github mbj4668 / pyang / pyang / plugins / check_update.py View on Github external
newkey = new.search_one('key')
    if oldkey is None and newkey is None:
        pass
    elif oldkey is None and newkey is not None:
        err_def_added(newkey, ctx)
    elif oldkey is not None and newkey is None:
        err_def_removed(oldkey, new, ctx)
    else:
        # check the key argument string; i_key is not set in groupings
        oldks = [k for k in oldkey.arg.split() if k != '']
        newks = [k for k in newkey.arg.split() if k != '']
        if len(oldks) != len(newks):
            err_def_changed(oldkey, newkey, ctx)
        else:
            for ok, nk in zip(oldks, newks):
                if util.split_identifier(ok)[1] != util.split_identifier(nk)[1]:
                    err_def_changed(oldkey, newkey, ctx)
                    return
github mbj4668 / pyang / pyang / types.py View on Github external
def str_to_val(self, errors, pos, string, module):
        if string.find(":") == -1:
            prefix = None
            name = string
        else:
            [prefix, name] = string.split(':', 1)
        if prefix is None or module.i_prefix == prefix:
            # check local identities
            pmodule = module
        else:
            # this is a prefixed name, check the imported modules
            pmodule = util.prefix_to_module(module, prefix, pos, errors)
            if pmodule is None:
                return None
        if name not in pmodule.i_identities:
            err_add(errors, pos, 'TYPE_VALUE',
                    (string, self.definition, 'identityref not found'))
            return None
        val = pmodule.i_identities[name]
        for idbase in self.idbases:
            my_identity = idbase.i_identity
            if not is_derived_from(val, my_identity):
                err_add(errors, pos, 'TYPE_VALUE',
                        (string, self.definition,
                         'identityref not derived from %s' % my_identity.arg))
                return None
        return val
github mbj4668 / pyang / pyang / statements.py View on Github external
def v_xpath(ctx, stmt):
    if stmt.parent.keyword == 'augment':
        node = stmt.parent.i_target_node
    elif getattr(stmt, 'i_origin', None) == 'uses':
        node = util.data_node_up(stmt.parent)
    else:
        node = stmt.parent
    if node is not None:
        node = util.closest_ancestor_data_node(node)
    xpath.v_xpath(ctx, stmt, node)
github mbj4668 / pyang / pyang / statements.py View on Github external
target.substmts.append(new)
            else:
                err_add(ctx.errors, refinement.pos, 'BAD_REFINEMENT',
                        (target.keyword, target.i_module.i_modulename,
                         target.arg, keyword))
                return

    (_arg_type, subspec) = grammar.stmt_map[stmt.parent.keyword]
    subspec = grammar.flatten_spec(subspec)
    whens = list(stmt.search('when'))
    for s in whens:
        s.i_origin = 'uses'
    iffeatures = list(stmt.search('if-feature'))
    # first, copy the grouping into our i_children
    for g in stmt.i_grouping.i_children:
        if util.keysearch(g.keyword, 0, subspec) is None:
            err_add(ctx.errors, stmt.pos, 'UNEXPECTED_KEYWORD_USES',
                    (util.keyword_to_str(g.raw_keyword),
                     util.keyword_to_str(stmt.parent.raw_keyword),
                     g.pos))
            continue

        # don't copy the type since it cannot be modified anyway.
        # not copying the type also works better for some plugins that
        # generate output from the i_children list.
        def post_copy(old, new):
            # inline the definition into our module
            new.i_module = stmt.i_module
            if hasattr(old, 'i_not_implemented'):
                new.i_not_implemented = old.i_not_implemented
            new.i_children = []
            new.i_uniques = []
github mbj4668 / pyang / pyang / translators / dsdl.py View on Github external
`p_elem` is the parent node in the output schema. `pset` is
        the current "patch set" - a dictionary with keys being QNames
        of schema nodes at the current level of hierarchy for which
        (or descendants thereof) any pending patches exist. The values
        are instances of the Patch class.

        All handler methods are defined below and must have the same
        arguments as this method. They should create the output schema
        fragment corresponding to `stmt`, apply all patches from
        `pset` belonging to `stmt`, insert the fragment under `p_elem`
        and perform all side effects as necessary.
        """
        if self.debug > 0:
            sys.stderr.write("Handling '%s %s'\n" %
                             (util.keyword_to_str(stmt.raw_keyword), stmt.arg))
        try:
            method = self.stmt_handler[stmt.keyword]
        except KeyError:
            if isinstance(stmt.keyword, tuple):
                try:
                    method = self.ext_handler[stmt.keyword[0]][stmt.keyword[1]]
                except KeyError:
                    method = self.rng_annotation
                method(stmt, p_elem)
                return
            else:
                raise error.EmitError(
                    "Unknown keyword %s - this should not happen.\n"
                    % stmt.keyword)
        method(stmt, p_elem, pset)
github mbj4668 / pyang / pyang / statements.py View on Github external
err_add(ctx.errors, pathpos, 'LEAFREF_BAD_PREDICATE',
                        (ptr.i_module.arg, ptr.arg, stmt.arg, stmt.pos))
                raise NotFound
            if ptr.keyword in _keyword_with_children:
                ptr = search_data_node(ptr.i_children, module_name, name,
                                       last_skipped)
                if not is_submodule_included(path, ptr):
                    ptr = None
                if ptr is None:
                    err_add(ctx.errors, pathpos, 'LEAFREF_IDENTIFIER_NOT_FOUND',
                            (module_name, name, stmt.arg, stmt.pos))
                    raise NotFound
            else:
                err_add(ctx.errors, pathpos, 'LEAFREF_IDENTIFIER_BAD_NODE',
                        (module_name, name, stmt.arg, stmt.pos,
                         util.keyword_to_str(ptr.keyword)))
                raise NotFound
            path_list.append(('dn', ptr))
            i = i + 1
        return (key_list, keys, ptr, path_list)
github mbj4668 / pyang / pyang / yin_parser.py View on Github external
except KeyError:
                error.err_add(self.ctx.errors, e.pos,
                              'MODULE_NOT_IMPORTED', e.ns)
                return None
            keywd = (prefix, e.local_name)
            keywdstr = util.keyword_to_str(keywd)
            if 'no_extensions' in self.extra:
                return None
            res = self.find_extension(e.ns, e.local_name)
            if res is None:
                error.err_add(self.ctx.errors, e.pos,
                              'UNKNOWN_KEYWORD', keywdstr)
                return None
            (arg_is_elem, argname)  = res

        keywdstr = util.keyword_to_str(keywd)
        if arg_is_elem == True:
            # find the argument element
            arg_elem = e.find_child(e.ns, argname)
            if arg_elem is None:
                arg = None
                error.err_add(self.ctx.errors, e.pos,
                              'MISSING_ARGUMENT_ELEMENT', (argname, keywdstr))

            else:
                if self.ctx.trim_yin:
                    arg = "\n".join([x.strip() for x in
                                     arg_elem.data.strip().splitlines()])
                else:
                    arg = arg_elem.data
                e.remove_child(arg_elem)
        elif arg_is_elem == False: