How to use the pyang.plugin.PyangPlugin 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 osrg / gobgp / tools / pyang_plugins / bgpyang2golang.py View on Github external
// limitations under the License.

// Code generated by pyang. DO NOT EDIT.
"""

EQUAL_TYPE_LEAF = 0
EQUAL_TYPE_ARRAY = 1
EQUAL_TYPE_MAP = 2
EQUAL_TYPE_CONTAINER = 3


def pyang_plugin_init():
    plugin.register_plugin(GolangPlugin())


class GolangPlugin(plugin.PyangPlugin):

    def __init__(self, name=None):
        super(GolangPlugin, self).__init__(name=name)
        self.multiple_modules = True

    def add_output_format(self, fmts):
        fmts['golang'] = self

    def emit(self, ctx, modules, fd):
        ctx.golang_identity_map = {}
        ctx.golang_typedef_map = {}
        ctx.golang_struct_def = []
        ctx.golang_struct_names = {}

        ctx.emitted_type_names = {}
github mbj4668 / pyang / test / test_transform / mod-desc.py View on Github external
from pyang import plugin
from pyang import statements


def pyang_plugin_init():
    plugin.register_plugin(ModDescPlugin())


class ModDescPlugin(plugin.PyangPlugin):
    def add_transform(self, xforms):
        xforms['mod-desc'] = self

    def transform(self, ctx, modules):
        for module in modules:
            mod_desc(module, '-- I added this!')


def mod_desc(stmt, text):
    desc = stmt.search_one('description')
    if desc:
        desc.arg += ' ' + text

    # XXX for some reason validate_module() crashes with undefined i_module if
    #     add description to module or submodule
    elif stmt.keyword not in ['module', 'submodule']:
github mbj4668 / pyang / pyang / translators / yang.py View on Github external
"""YANG output plugin"""

import optparse

from .. import plugin
from .. import util
from .. import grammar

def pyang_plugin_init():
    plugin.register_plugin(YANGPlugin())

class YANGPlugin(plugin.PyangPlugin):
    def add_output_format(self, fmts):
        fmts['yang'] = self
        self.handle_comments = True

    def add_opts(self, optparser):
        optlist = [
            optparse.make_option("--yang-canonical",
                                 dest="yang_canonical",
                                 action="store_true",
                                 help="Print in canonical order"),
            optparse.make_option("--yang-remove-unused-imports",
                                 dest="yang_remove_unused_imports",
                                 action="store_true"),
            optparse.make_option("--yang-remove-comments",
                                 dest="yang_remove_comments",
                                 action="store_true"),
github robshakir / pyangbind / pyangbind / plugin / pybind.py View on Github external
}

# We have a set of types which support "range" statements in RFC6020. This
# list determins types that should be allowed to have a "range" argument.
INT_RANGE_TYPES = ["uint8", "uint16", "uint32", "uint64", "int8", "int16", "int32", "int64"]

# The types that are built-in to YANG
YANG_BUILTIN_TYPES = list(class_map.keys()) + ["container", "list", "rpc", "notification", "leafref"]


# Base machinery to support operation as a plugin to pyang.
def pyang_plugin_init():
    plugin.register_plugin(PyangBindClass())


class PyangBindClass(plugin.PyangPlugin):

    def add_output_format(self, fmts):
        # Add the 'pybind' output format to pyang.
        self.multiple_modules = True
        fmts["pybind"] = self

    def emit(self, ctx, modules, fd):
        # When called, call the build_pyangbind function.
        build_pybind(ctx, modules, fd)

    def add_opts(self, optparser):
        # Add pyangbind specific operations to pyang. These are documented in the
        # options, but are essentially divided into three sets.
        #   * xpathhelper - How pyangbind should deal with xpath expressions.
        #     This module is documented in lib/xpathhelper and describes how
        #     to support registration, updates, and retrieval of xpaths.
github mbj4668 / pyang / pyang / plugins / metadata.py View on Github external
"""ietf-yang-metadata plugin

Verifies metadata YANG statements as defined in RFC 7952
"""

from pyang import plugin
from pyang import grammar

md_module_name = 'ietf-yang-metadata'

class MDPlugin(plugin.PyangPlugin):
    pass

def pyang_plugin_init():
    """Called by pyang plugin framework at to initialize the plugin."""

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

    # Register that we handle extensions from the YANG module
    # 'ietf-yang-metadata'
    grammar.register_extension_module(md_module_name)

    # Register the special grammar
    for stmt, occurence, (arg, rules), add_to_stmts in md_stmts:
        grammar.add_stmt((md_module_name, stmt), (arg, rules))
        grammar.add_to_stmts_rules(add_to_stmts,
github mbj4668 / pyang / pyang / plugins / jsonxsl.py View on Github external
"""Classification of types suited for JSON translation."""

type_class.update((t,t) for t in
                  ("empty", "instance-identifier", "identityref", "string"))

union_class = dict((t,"integer") for t in
                   ("int8", "int16", "int32",
                   "uint8", "uint16", "uint32"))
"""Classification of types needed for resolving union-typed values."""

union_class.update({"boolean": "boolean"})

def pyang_plugin_init():
    plugin.register_plugin(JsonXslPlugin())

class JsonXslPlugin(plugin.PyangPlugin):
    def add_output_format(self, fmts):
        self.multiple_modules = True
        fmts['jsonxsl'] = self

    def setup_fmt(self, ctx):
        ctx.implicit_errors = False

    def emit(self, ctx, modules, fd):
        """Main control function.

        Set up the top-level parts of the stylesheet, then process
        recursively all nodes in all data trees, and finally emit the
        serialized stylesheet.
        """
        for (epos, etag, eargs) in ctx.errors:
            if error.is_error(error.err_level(etag)):
github CiscoDevNet / yang-explorer / server / explorer / plugins / pyimport.py View on Github external
import sys
from pyang import plugin
import xml.etree.ElementTree as ET


def pyang_plugin_init():
    plugin.register_plugin(PyImportPlugin())


def print_help():
    print("""
    pyang -f pyimport [option] 
    """)


class PyImportPlugin(plugin.PyangPlugin):
    def add_output_format(self, fmts):
        self.multiple_modules = True
        fmts['pyimport'] = self

    def add_opts(self, optparser):
        optlist = [
            optparse.make_option("--pyimport-help",
                                 dest="pyimport_help",
                                 action="store_true",
                                 help="Print help on PyImport and exit"),
            ]
        g = optparser.add_option_group("PyImport output specific options")
        g.add_options(optlist)

    def setup_ctx(self, ctx):
        if ctx.opts.tree_help:
github mbj4668 / pyang / pyang / plugins / jtox.py View on Github external
"""JTOX output plugin

This plugin takes a YANG data model and produces a JSON driver file
that can be used by the *json2xml* script for translating a valid JSON
configuration or state data to XML.
"""

import json

from pyang import plugin, error
from pyang.util import unique_prefixes

def pyang_plugin_init():
    plugin.register_plugin(JtoXPlugin())

class JtoXPlugin(plugin.PyangPlugin):
    def add_output_format(self, fmts):
        self.multiple_modules = True
        fmts['jtox'] = self

    def setup_fmt(self, ctx):
        ctx.implicit_errors = False

    def emit(self, ctx, modules, fd):
        """Main control function.
        """
        for epos, etag, eargs in ctx.errors:
            if error.is_error(error.err_level(etag)):
                raise error.EmitError("JTOX plugin needs a valid module")
        tree = {}
        mods = {}
        annots = {}
github mbj4668 / pyang / pyang / translators / xsd.py View on Github external
'submodule':        ('name',        False,      False),
     'type':             ('name',        False,      False),
     'typedef':          ('name',        False,      False),
     'unique':           ('tag',         False,      False),
     'units':            ('name',        False,      True),
     'uses':             ('name',        False,      False),
     'value':            ('value',       False,      False),
     'when':             ('condition',   False,      True),
     'yang-version':     ('value',       False,      True),
     'yin-element':      ('value',       False,      False),
     }

def pyang_plugin_init():
    plugin.register_plugin(XSDPlugin())

class XSDPlugin(plugin.PyangPlugin):
    def add_opts(self, optparser):
        optlist = [
            optparse.make_option("--xsd-global-complex-types",
                                 dest="xsd_global_complex_types",
                                 action="store_true",
                                 help="Make all complex types global instead" +
                                      " of inline"),
            optparse.make_option("--xsd-groups",
                                 dest="xsd_groups",
                                 action="store_true",
                                 help="EXPERIMENTAL: does not work correctly"),
            optparse.make_option("--xsd-no-appinfo",
                                 dest="xsd_no_appinfo",
                                 action="store_true",
                                 help="Do not print YANG specific appinfo"),
            optparse.make_option("--xsd-no-imports",
github mbj4668 / pyang / pyang / plugins / depend.py View on Github external
"""Makefile dependency rule output plugin

"""

import optparse
import sys
import os.path

from pyang import plugin
from pyang import error

def pyang_plugin_init():
    plugin.register_plugin(DependPlugin())

class DependPlugin(plugin.PyangPlugin):
    def add_opts(self, optparser):
        optlist = [
            optparse.make_option("--depend-target",
                                 dest="depend_target",
                                 help="Makefile rule target"),
            optparse.make_option("--depend-no-submodules",
                                 dest="depend_no_submodules",
                                 action="store_true",
                                 help="Do not generate dependencies for " \
                                 "included submodules"),
            optparse.make_option("--depend-from-submodules",
                                 dest="depend_from_submodules",
                                 action="store_true",
                                 help="Generate dependencies from " \
                                 "included submodules"),
            optparse.make_option("--depend-recurse",