How to use the pyang.grammar 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 / plugins / restconf.py View on Github external
def pyang_plugin_init():
    """Called by pyang plugin framework at to initialize the plugin."""

    # Register the plugin
    plugin.register_plugin(RESTCONFPlugin())

    # Register that we handle extensions from the YANG module 'ietf-restconf'
    grammar.register_extension_module(restconf_module_name)

    yd = (restconf_module_name, 'yang-data')
    statements.add_data_keyword(yd)
    statements.add_keyword_with_children(yd)
    statements.add_keywords_with_no_explicit_config(yd)

    # Register the special grammar
    for stmt, occurence, (arg, rules), add_to_stmts in restconf_stmts:
        grammar.add_stmt((restconf_module_name, stmt), (arg, rules))
        grammar.add_to_stmts_rules(add_to_stmts,
                                   [((restconf_module_name, stmt), occurence)])

    # Add validation functions
    statements.add_validation_fun('expand_2',
                                  [yd],
                                  v_yang_data)
github mbj4668 / pyang / pyang / translators / yin.py View on Github external
if prefix is not None:
            rev = None
            r = imp.search_one('revision-date')
            if r is not None:
                rev = r.arg
            mod = statements.modulename_to_module(module, imp.arg, rev)
            if mod is not None:
                ns = mod.search_one('namespace')
                if ns is not None:
                    fd.write('\n')
                    fd.write(' ' * len(module.keyword))
                    fd.write('  xmlns:' + prefix.arg + '=' +
                             quoteattr(ns.arg))
    fd.write('>\n')
    if ctx.opts.yin_canonical:
        substmts = grammar.sort_canonical(module.keyword, module.substmts)
    else:
        substmts = module.substmts
    for s in substmts:
        emit_stmt(ctx, module, s, fd, '  ', '  ')
    fd.write('\n' % module.keyword)
github mbj4668 / pyang / pyang / statements.py View on Github external
def v_grammar_module(ctx, stmt):
    # check the statement hierarchy
    grammar.chk_module_statements(ctx, stmt, ctx.canonical)
    # check revision statements order
    prev = None
    stmt.i_latest_revision = None
    for r in stmt.search('revision'):
        if stmt.i_latest_revision is None or r.arg > stmt.i_latest_revision:
            stmt.i_latest_revision = r.arg
        if prev is not None and r.arg > prev:
            err_add(ctx.errors, r.pos, 'REVISION_ORDER', ())
        prev = r.arg
github mbj4668 / pyang / pyang / translators / yang.py View on Github external
def make_link_list(ctx, stmt, link_list):
    if 'last' in link_list:
        link_list[ link_list['last'] ] = stmt
    link_list['last'] = stmt

    if len(stmt.substmts) > 0:
        if ctx.opts.yang_canonical:
            substmts = grammar.sort_canonical(stmt.keyword, stmt.substmts)
        else:
            substmts = stmt.substmts
        for i, s in enumerate(substmts, start=1):
            make_link_list(ctx, s, link_list)
github mbj4668 / pyang / pyang / statements.py View on Github external
def v_grammar_identifier(ctx, stmt):
    try:
        (arg_type, _subspec) = grammar.stmt_map[stmt.keyword]
    except KeyError:
        return
    if (arg_type == 'identifier' and
        grammar.re_identifier_illegal_prefix.search(stmt.arg) is not None):
        if stmt.keyword == 'module' or stmt.keyword == 'submodule':
            mod = stmt
        else:
            mod = stmt.i_module
        if mod.i_version == '1':
            err_add(ctx.errors, stmt.pos, 'XML_IDENTIFIER', stmt.arg)
github mbj4668 / pyang / pyang / statements.py View on Github external
def v_grammar_identifier(ctx, stmt):
    try:
        (arg_type, _subspec) = grammar.stmt_map[stmt.keyword]
    except KeyError:
        return
    if (arg_type == 'identifier' and
        grammar.re_identifier_illegal_prefix.search(stmt.arg) is not None):
        if stmt.keyword == 'module' or stmt.keyword == 'submodule':
            mod = stmt
        else:
            mod = stmt.i_module
        if mod.i_version == '1':
            err_add(ctx.errors, stmt.pos, 'XML_IDENTIFIER', stmt.arg)
github mbj4668 / pyang / pyang / statements.py View on Github external
def v_grammar_identifier(ctx, stmt):
    try:
        (arg_type, _subspec) = grammar.stmt_map[stmt.keyword]
    except KeyError:
        return
    if (arg_type == 'identifier' and
        grammar.re_identifier_illegal_prefix.search(stmt.arg) is not None):
        if stmt.keyword == 'module' or stmt.keyword == 'submodule':
            mod = stmt
        else:
            mod = stmt.i_module
        if mod.i_version == '1':
            err_add(ctx.errors, stmt.pos, 'XML_IDENTIFIER', stmt.arg)
github mbj4668 / pyang / pyang / plugins / lint.py View on Github external
def v_chk_hyphenated_names(ctx, stmt):
    if stmt.keyword in grammar.stmt_map:
        (arg_type, subspec) = grammar.stmt_map[stmt.keyword]
        if ((arg_type == 'identifier' or arg_type == 'enum-arg') and
            not_hyphenated(stmt.arg)):
            error.err_add(ctx.errors, stmt.pos, 'LINT_NOT_HYPHENATED', stmt.arg)
github mbj4668 / pyang / pyang / plugins / restconf.py View on Github external
"""Called by pyang plugin framework at to initialize the plugin."""

    # Register the plugin
    plugin.register_plugin(RESTCONFPlugin())

    # Register that we handle extensions from the YANG module 'ietf-restconf'
    grammar.register_extension_module(restconf_module_name)

    yd = (restconf_module_name, 'yang-data')
    statements.add_data_keyword(yd)
    statements.add_keyword_with_children(yd)
    statements.add_keywords_with_no_explicit_config(yd)

    # Register the special grammar
    for stmt, occurence, (arg, rules), add_to_stmts in restconf_stmts:
        grammar.add_stmt((restconf_module_name, stmt), (arg, rules))
        grammar.add_to_stmts_rules(add_to_stmts,
                                   [((restconf_module_name, stmt), occurence)])

    # Add validation functions
    statements.add_validation_fun('expand_2',
                                  [yd],
                                  v_yang_data)

    # Register special error codes
    error.add_error_code('RESTCONF_YANG_DATA_CHILD', 1,
                         "the 'yang-data' extension must have exactly one " +
                         "child that is a container")
github mbj4668 / pyang / pyang / plugins / structure.py View on Github external
statements.add_keywords_with_no_explicit_config(sx)
    asx = (module_name, 'augment-structure')
    statements.add_data_keyword(asx)
    statements.add_keyword_with_children(asx)
    statements.add_keywords_with_no_explicit_config(asx)

    # Register the special grammar
    for (stmt, occurance, (arg, rules), add_to_stmts) in structure_stmts:
        grammar.add_stmt((module_name, stmt), (arg, rules))
        grammar.add_to_stmts_rules(add_to_stmts,
                                   [((module_name, stmt), occurance)])

body_stmts = [
    ('description', '?'),
    ('reference', '?'),
    ('$interleave', grammar.data_def_stmts)
    ]

## FIXME: validate augment-structure

structure_stmts = [

    # (, ,
    #  (, ),
    #   can occur>)

    ('structure', '*',
     ('identifier', body_stmts),
     ['module', 'submodule']),

    ('augment-structure', '*',
     ('absolute-schema-nodeid', body_stmts),