<commit_before>"""Tosca output plugin

Idea copied from tree.
"""

import optparse
import sys
import yaml
import pyang.plugins.tree
import StringIO
from pyang import plugin
from pyang import statements


DEFAULT_INTERFACE = {"cloudify.interfaces.lifecycle":
                     {"create":
                      {"implementation": "vcloud.cloudify_yang_plugin.create",
                       "inputs": {}},
                      "delete":
                      {"implementation": "vcloud.cloudify_yang_plugin.delete",
                       "inputs": {}}}}


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


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

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

    def setup_ctx(self, ctx):
        if ctx.opts.tosca_help:
            print_help()
            sys.exit(0)

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

    def emit(self, ctx, modules, fd):
        emit_yaml(ctx, modules, fd)


def print_help():
    print("""
    Convert YANG files to TOSCA files with YAML format
""")


def emit_yaml(ctx, modules, fd):
    output = {'data_types': {}, 'node_types': {}}
    xmlns = []
    main_module = None
    for module in modules:
        namespace = "{}".format(module.search_one('namespace').arg)
        children = _collect_children(module)
        if children:
            main_module = "{}".format(module.arg)
            xmlns.append({"_": namespace})
        else:
            prefix = "{}".format(module.i_prefix)
            xmlns.append({prefix: namespace})
    if not main_module:
        return fd.write("Can't find main module")
    output['node_types'][main_module] = {'properties':
                                         {'edit-config':
                                          {'type': 'edit-config',
                                           'required': False},
                                          'metadata':
                                          {'defaults':
                                           {'xmlns': xmlns}}}}
    output['data_types']['edit-config'] = {'properties':
                                           {'target': {'default': 'running'},
                                            'config': {'type': 'config'}}}
    for module in modules:
            _handle_edit_config(module, output)
            _handle_custom_rpc(module, main_module, output)

    treeout = _get_tree_representation(ctx, modules)
    dump = yaml.dump(output, width=100, allow_unicode=True,
                     default_flow_style=False)
    fd.write(dump + treeout)


def _handle_edit_config(module, output):
    children = _collect_children(module)
    if children:
        module_types = {}
        _handle_children(children, module, module_types, output)
        output['data_types']['config'] = {'properties': module_types}


def _handle_custom_rpc(module, main_module, output):
    rpcs = [ch for ch in module.i_children
            if ch.keyword == 'rpc']
    for rpc in rpcs:
        rpc_name = "{}".format(rpc.arg)
        input_stmt = rpc.search_one('input')
        node_info = output['node_types'][main_module]['properties']
        if input_stmt:
            node_info.update({rpc_name: {'type': rpc_name,<commit_msg>Replace list to dict in metadata property<commit_after>"""Tosca output plugin

Idea copied from tree.
"""

import optparse
import sys
import yaml
import pyang.plugins.tree
import StringIO
from pyang import plugin
from pyang import statements


DEFAULT_INTERFACE = {"cloudify.interfaces.lifecycle":
                     {"create":
                      {"implementation": "vcloud.cloudify_yang_plugin.create",
                       "inputs": {}},
                      "delete":
                      {"implementation": "vcloud.cloudify_yang_plugin.delete",
                       "inputs": {}}}}


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


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

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

    def setup_ctx(self, ctx):
        if ctx.opts.tosca_help:
            print_help()
            sys.exit(0)

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

    def emit(self, ctx, modules, fd):
        emit_yaml(ctx, modules, fd)


def print_help():
    print("""
    Convert YANG files to TOSCA files with YAML format
""")


def emit_yaml(ctx, modules, fd):
    output = {'data_types': {}, 'node_types': {}}
    xmlns = {}
    main_module = None
    for module in modules:
        namespace = "{}".format(module.search_one('namespace').arg)
        children = _collect_children(module)
        if children:
            main_module = "{}".format(module.arg)
            xmlns["_"] = namespace
        else:
            prefix = "{}".format(module.i_prefix)
            xmlns[prefix] = namespace
    if not main_module:
        return fd.write("Can't find main module")
    output['node_types'][main_module] = {'properties':
                                         {'edit-config':
                                          {'type': 'edit-config',
                                           'required': False},
                                          'metadata':
                                          {'default':
                                           {'xmlns': xmlns}}}}
    output['data_types']['edit-config'] = {'properties':
                                           {'target': {'default': 'running'},
                                            'config': {'type': 'config'}}}
    for module in modules:
            _handle_edit_config(module, output)
            _handle_custom_rpc(module, main_module, output)

    treeout = _get_tree_representation(ctx, modules)
    dump = yaml.dump(output, width=100, allow_unicode=True,
                     default_flow_style=False)
    fd.write(dump + treeout)


def _handle_edit_config(module, output):
    children = _collect_children(module)
    if children:
        module_types = {}
        _handle_children(children, module, module_types, output)
        output['data_types']['config'] = {'properties': module_types}


def _handle_custom_rpc(module, main_module, output):
    rpcs = [ch for ch in module.i_children
            if ch.keyword == 'rpc']
    for rpc in rpcs:
        rpc_name = "{}".format(rpc.arg)
        input_stmt = rpc.search_one('input')
        node_info = output['node_types'][main_module]['properties']
        if input_stmt:
            node_info.update({rpc_name: {'type': rpc_name,
{'fraction_non_alphanumeric': 0.11519176040113836, 'fraction_numerical': 0.0010841577449518904, 'mean_word_length': 1.848321111539946, 'pattern_counts': {'":': 15, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4783681', 'n_tokens_mistral': 2006, 'n_tokens_neox': 1938, 'n_words': 439}