How to use the pyang.util.keyword_to_str 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 / grammar.py View on Github external
(subspec, cansubspec), canonical)
            spec = match_res
        else:
            # unknown extension
            stmt.is_grammatically_valid = True
            nspec = [('$any', '*')]
            _chk_stmts(ctx, stmt.pos, stmt.substmts, stmt,
                       (nspec, nspec), canonical)
        # update last know position
        pos = stmt.pos
    # any non-optional statements left are errors
    for (keywd, occurance) in spec[0]:
        if occurance == '1' or occurance == '+':
            if parent is None:
                error.err_add(ctx.errors, pos, 'EXPECTED_KEYWORD',
                              util.keyword_to_str(keywd))
            else:
                error.err_add(ctx.errors, pos, 'EXPECTED_KEYWORD_2',
                              (util.keyword_to_str(keywd),
                               util.keyword_to_str(parent.raw_keyword)))
github mbj4668 / pyang / pyang / statements.py View on Github external
(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 = []
            new.pos.uses_pos = stmt.pos
            # build the i_children list of pointers
            if hasattr(old, 'i_children'):
github mbj4668 / pyang / pyang / grammar.py View on Github external
if match_res is None and chk_grammar:
            if canonical:
                save_errors = ctx.errors
                ctx.errors = []
                if _match_stmt(ctx, stmt, (spec[1], []), False) is not None:
                    ctx.errors = save_errors
                    error.err_add(ctx.errors, stmt.pos,
                                  'UNEXPECTED_KEYWORD_CANONICAL',
                                  util.keyword_to_str(stmt.raw_keyword))
                else:
                    ctx.errors = save_errors
                    error.err_add(ctx.errors, stmt.pos, 'UNEXPECTED_KEYWORD',
                                  util.keyword_to_str(stmt.raw_keyword))
            else:
                error.err_add(ctx.errors, stmt.pos, 'UNEXPECTED_KEYWORD',
                              util.keyword_to_str(stmt.raw_keyword))
        elif match_res is not None and chk_grammar:
            try:
                (arg_type, subspec) = stmt_map[stmt.keyword]
            except KeyError:
                error.err_add(ctx.errors, stmt.pos, 'UNEXPECTED_KEYWORD',
                              util.keyword_to_str(stmt.raw_keyword))
                return
            # verify the statement's argument
            if arg_type is None and stmt.arg is not None:
                error.err_add(ctx.errors, stmt.pos,
                              'UNEXPECTED_ARGUMENT', stmt.arg)
            elif arg_type is not None and stmt.arg is None:
                error.err_add(ctx.errors, stmt.pos,
                              'EXPECTED_ARGUMENT',
                              util.keyword_to_str(stmt.keyword))
            elif (arg_type is not None and arg_type != 'string' and
github mbj4668 / pyang / pyang / grammar.py View on Github external
elif util.is_prefixed(stmt.keyword):
            # allow extension statements mixed with these
            # set canonical to False in this call to just remove the
            # matching stmt from the spec
            match_res = _match_stmt(ctx, stmt, (spec[i+1:], canspec), False)
            if match_res is not None:
                return (spec[:i+1] + match_res[0], canspec)
            else:
                return None
        elif keywd == '$cut':
            # any non-optional statements left are errors
            for keywd, occurence in spec[:i]:
                if occurence == '1' or occurence == '+':
                    error.err_add(ctx.errors, stmt.pos, 'UNEXPECTED_KEYWORD_1',
                                  (util.keyword_to_str(stmt.raw_keyword),
                                   util.keyword_to_str(keywd)))
            # consume them so we don't report the same error again
            spec = spec[i:]
            i = 0
        elif canonical:
            if occurence == '1' or occurence == '+':
                error.err_add(ctx.errors, stmt.pos,
                              'UNEXPECTED_KEYWORD_CANONICAL_1',
                              (util.keyword_to_str(stmt.raw_keyword),
                               util.keyword_to_str(keywd)))
                # consume it so we don't report the same error again
                spec = spec[i:]
                i = 0
        # check next in spec
        i += 1
    return None
github mbj4668 / pyang / pyang / grammar.py View on Github external
# unknown extension
            stmt.is_grammatically_valid = True
            nspec = [('$any', '*')]
            _chk_stmts(ctx, stmt.pos, stmt.substmts, stmt,
                       (nspec, nspec), canonical)
        # update last know position
        pos = stmt.pos
    # any non-optional statements left are errors
    for (keywd, occurance) in spec[0]:
        if occurance == '1' or occurance == '+':
            if parent is None:
                error.err_add(ctx.errors, pos, 'EXPECTED_KEYWORD',
                              util.keyword_to_str(keywd))
            else:
                error.err_add(ctx.errors, pos, 'EXPECTED_KEYWORD_2',
                              (util.keyword_to_str(keywd),
                               util.keyword_to_str(parent.raw_keyword)))
github mbj4668 / pyang / pyang / grammar.py View on Github external
stmt.is_grammatically_valid = True
            nspec = [('$any', '*')]
            _chk_stmts(ctx, stmt.pos, stmt.substmts, stmt,
                       (nspec, nspec), canonical)
        # update last know position
        pos = stmt.pos
    # any non-optional statements left are errors
    for (keywd, occurance) in spec[0]:
        if occurance == '1' or occurance == '+':
            if parent is None:
                error.err_add(ctx.errors, pos, 'EXPECTED_KEYWORD',
                              util.keyword_to_str(keywd))
            else:
                error.err_add(ctx.errors, pos, 'EXPECTED_KEYWORD_2',
                              (util.keyword_to_str(keywd),
                               util.keyword_to_str(parent.raw_keyword)))
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): # extension
                self.handle_extension(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 / grammar.py View on Github external
# unknown extension
            stmt.is_grammatically_valid = True
            nspec = [('$any', '*')]
            _chk_stmts(ctx, stmt.pos, stmt.substmts, stmt,
                       (nspec, nspec), canonical)
        # update last know position
        pos = stmt.pos
    # any non-optional statements left are errors
    for keywd, occurence in spec[0]:
        if occurence == '1' or occurence == '+':
            if parent is None:
                error.err_add(ctx.errors, pos, 'EXPECTED_KEYWORD',
                              util.keyword_to_str(keywd))
            else:
                error.err_add(ctx.errors, pos, 'EXPECTED_KEYWORD_2',
                              (util.keyword_to_str(keywd),
                               util.keyword_to_str(parent.raw_keyword)))