How to use pyang - 10 common examples

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 / plugins / yangxml.py View on Github external
# This pyang plugin generates a random XML instance document adhering
# to a YANG module.

from pyang import plugin
from pyang import types
from pyang import statements
import sys
from random import randint, random

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

class YANGXMLPlugin(plugin.PyangPlugin):
    def add_output_format(self, fmts):
        fmts['xml'] = self
    def emit(self, ctx, modules, writef):
        module = modules[0]
        emit_xml(module, writef)
    
def emit_xml(module, fd):
    # pick one top-level child
    if len(module.i_children) == 0:
        return
    c = pick(module.i_children)
    attrs = ' xmlns="%s"' % module.search_one('namespace').arg
    if c.keyword == 'choice':
        sys.stderr.write("Cannot handle choice on top-level")
        sys.exit(1)
    emit_stmt(c, fd, '', attrs, 1)
github mbj4668 / pyang / test / test_transform / add-foo.py View on Github external
from pyang import plugin
from pyang import statements


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


class AddFoo(plugin.PyangPlugin):
    def add_transform(self, xforms):
        xforms['add-foo'] = self

    def transform(self, ctx, modules):
        for module in modules:
            for stmt in module.substmts:
                if stmt.keyword == 'container':
                    foo = stmt.search_one('foo')
                    if not foo:
                        foo = add_leaf(stmt, 'foo', 'string')


def add_leaf(parent, name, type_name):
    leaf = statements.new_statement(parent.top, parent, parent.pos,
                                    'leaf', name)
    parent.substmts.append(leaf)
github mbj4668 / pyang / test / plugins / yangxml.py View on Github external
def pyang_plugin_init():
    plugin.register_plugin(YANGXMLPlugin())
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 CiscoDevNet / ydk-gen / ydkgen / builder / test_case / test_value_builder.py View on Github external
def _get_path_target_type_stmt(self, type_stmt):
        """Return target pyang statement."""
        type_spec = type_stmt.i_type_spec
        while all([isinstance(type_spec, ptypes.PathTypeSpec),
                   hasattr(type_spec, 'i_target_node')]):
            type_stmt = type_spec.i_target_node.search_one('type')
            type_spec = type_stmt.i_type_spec
        return type_stmt
github CiscoDevNet / ydk-gen / ydkgen / builder / test_case / test_value_builder.py View on Github external
def _get_union_type_spec(self, orig_type_spec):
        """Return union type_spec and type_stmt."""
        type_spec = orig_type_spec
        while isinstance(type_spec, ptypes.UnionTypeSpec):
            type_stmt = choice(type_spec.types)
            type_spec = self.types_extractor.get_property_type(type_stmt)
            if hasattr(type_spec, 'i_type_spec'):
                type_spec = type_spec.i_type_spec

        return type_spec, type_stmt
github CiscoDevNet / ydk-gen / ydkgen / builder / test_case / test_value_builder.py View on Github external
def _set_prop_type(self, prop):
        """Set self.type_spec and self.type_stmt:

            - Trace path statement and set destination statement's
              pyang statement as self.type_stmt, pyang TypeSpec as
              self.type_spec.
            - If prop represents union property, chose one.
        """
        type_spec = prop.property_type
        type_stmt = prop.stmt.search_one('type')

        if isinstance(type_spec, ptypes.PathTypeSpec):
            type_stmt = self._get_path_target_type_stmt(type_stmt)
            type_spec = type_stmt.i_type_spec

        if isinstance(type_spec, ptypes.UnionTypeSpec):
            type_spec, type_stmt = self._get_union_type_spec(type_spec)

        self.type_spec = type_spec
        self.type_stmt = type_stmt
github mbj4668 / pyang / pyang / translators / yang.py View on Github external
def pyang_plugin_init():
    plugin.register_plugin(YANGPlugin())
github mbj4668 / pyang / pyang / plugins / sid.py View on Github external
def pyang_plugin_init():
    plugin.register_plugin(SidPlugin())