コード例 #1
0
    def _get_pyang_modules(self, filenames):
        modules = []
        regex_expression = re.compile(r"^(.*?)(\@(\d{4}-\d{2}-\d{2}))?\.(yang|yin)$")
        for filename in filenames:
            base_file_name = filename
            if filename.startswith('file://'):
                base_file_name = filename[len('file://') - 1:]
            try:
                fd = open(base_file_name)
                text = fd.read()
            except IOError as ex:
                err_msg = "error %s: %s\n" % (filename, str(ex))
                logger.error(err_msg)
                raise YdkGenException(err_msg)

            match = regex_expression.search(filename)
            if match is not None:
                (name, _dummy, rev, _) = match.groups()
                name = os.path.basename(name)
                logger.debug(
                    'Parsing file %s. Module name: %s. Revision: %s', filename, name, rev)
                module = self.ctx.add_module(filename, text, format, name, rev,
                                        expect_failure_error=False)
            else:
                module = self.ctx.add_module(filename, text)
            if module is None:
                raise YdkGenException('\nCould not add module "%s", (%s). \nPlease remove any duplicate files and verify that all the models pass pyang. Run "pyang *" on all the models.'%(name, filename))
            else:
                modules.append(module)
        return modules
コード例 #2
0
    def _resolve_grouping_class_cross_references(self, element):
        """
            Resolve cross references in the grouping as class code generation
            strategy.

            :param `api_model.Element` The model element whose cross references have to be
                    resolved.

            :raise `common.YdkGenException' if cross resolution of references failed.

        """
        if isinstance(element, Class) and not element.is_identity():
            uses_stmts = element.stmt.search('uses')

            # set the super classes or the extend property
            groupings_used = []
            for uses in uses_stmts:
                groupings_used.append(uses.i_grouping)

            extends = []
            for grouping_stmt in groupings_used:
                if grouping_stmt.i_class is None:
                    raise YdkGenException('Unresolved grouping class for ' +
                                          element.fqn())
                extends.append(grouping_stmt.i_class)
            element._extends = extends
        if isinstance(element, Property):
            enum_type = self.types_extractor.get_enum_type_stmt(element.stmt)
            if enum_type is not None and not isinstance(
                    element.property_type, Enum):
                if not hasattr(enum_type.parent, 'i_enum'):
                    raise YdkGenException(
                        'Cross resolution of enum failed for ' + element.fqn())

                element.property_type = enum_type.parent.i_enum

            else:
                # check for identity_ref's
                identity_ref_type = self.types_extractor.get_identity_ref_type_stmt(
                    element.stmt)
                if identity_ref_type is not None:
                    element.property_type = identity_ref_type.i_type_spec.base.i_identity.i_class
                else:
                    # check for bits
                    bits_ref_type = self.types_extractor.get_bits_type_stmt(
                        element.stmt)
                    if bits_ref_type is not None and not isinstance(
                            element.property_type, Bits):
                        if not hasattr(bits_ref_type.parent, 'i_bits'):
                            raise YdkGenException(
                                'Cross resolution of bits failed for ' +
                                element.fqn())

                        element.property_type = bits_ref_type.i_bits

        if hasattr(element, 'owned_elements'):
            for owned_element in element.owned_elements:
                self._resolve_grouping_class_cross_references(owned_element)
コード例 #3
0
    def _resolve_expanded_cross_references(self, element):
        """ Resolves cross references in the api_model Elements.

           Prerequisite before calling this function is that
           the 'pyang.statements.Statement' tree for the list of modules
           must have their associated api_model Elements set in the i_class
           i_package, i_enum variables. These variables are used to resolve
           the cross references.

          :param `NamedElement` element :- The element whose references need to be resolved.
          :raise `common.YdkGenException` If cross resolution failed.
        """
        if isinstance(element, Property):
            enum_type = self.types_extractor.get_enum_type_stmt(element.stmt)
            if enum_type is not None and not isinstance(
                    element.property_type, Enum):
                if not hasattr(enum_type.parent, 'i_enum'):
                    # case where the type is a leafref pointing to a leaf with an
                    # embedded enum definition
                    if (hasattr(enum_type.parent, 'i_property') and isinstance(
                            enum_type.parent.i_property.property_type, Enum)):
                        element.property_type = enum_type.parent.i_property.property_type
                    else:
                        raise YdkGenException(
                            'Cross resolution of enum failed for ' +
                            element.fqn())
                else:
                    element.property_type = enum_type.parent.i_enum

            else:
                # check for identity_ref's
                identity_ref_type = self.types_extractor.get_identity_ref_type_stmt(
                    element.stmt)
                if identity_ref_type is not None:
                    if not hasattr(
                            identity_ref_type.i_type_spec.base.i_identity,
                            'i_class'):
                        raise YdkGenException(
                            'Cross resolution of identity class failed for ' +
                            element.fqn())
                    element.property_type = identity_ref_type.i_type_spec.base.i_identity.i_class
                else:
                    # check for bits
                    bits_ref_type = self.types_extractor.get_bits_type_stmt(
                        element.stmt)
                    if bits_ref_type is not None and not isinstance(
                            element.property_type, Bits):
                        if not hasattr(bits_ref_type.parent, 'i_bits'):
                            raise YdkGenException(
                                'Cross resolution of bits failed for ' +
                                element.fqn())

                        element.property_type = bits_ref_type.parent.i_bits

        if hasattr(element, 'owned_elements'):
            for owned_element in element.owned_elements:
                self._resolve_expanded_cross_references(owned_element)
コード例 #4
0
    def _validate_pyang_modules(self, filenames):
        # all the module have been added so get the context to validate
        # call prevalidate before this and post validate after
        self.ctx.validate()

        def keyfun(e):
            if e[0].ref == filenames[0]:
                return 0
            else:
                return 1

        self.ctx.errors.sort(key=lambda e: (e[0].ref, e[0].line))
        if len(filenames) > 0:
            # first print error for the first filename given
            self.ctx.errors.sort(key=keyfun)

        error_messages = []
        for (epos, etag, eargs) in self.ctx.errors:
            elevel = error.err_level(etag)
            if error.is_warning(elevel):
                logger.warning('%s: %s\n' %
                               (str(epos), error.err_to_str(etag, eargs)))
            else:
                err_msg = '%s: %s\n' % (str(epos), error.err_to_str(etag, eargs))
                logger.error(err_msg)
                error_messages.append(err_msg)

        if len(error_messages) > 0:
            err_msg = '\n'.join(error_messages)
            raise YdkGenException('''\nError occured: "%s". \nThe models supplied to the YDK generator are invalid. Please make sure the models are valid by compiling the models together using pyang. Please run "pyang *" in the models directory, make sure there are no errors and then try running the generator again. If there are model errors, please fix the errors by editing the model, contacting the model owner or deleting the model from the list of models to generate the YDK bindings for.'''%err_msg)
コード例 #5
0
    def _validate_pyang_modules(self, filenames):
        # all the module have been added so get the context to validate
        # call prevalidate before this and post validate after
        self.ctx.validate()

        def keyfun(e):
            if e[0].ref == filenames[0]:
                return 0
            else:
                return 1

        self.ctx.errors.sort(key=lambda e: (e[0].ref, e[0].line))
        if len(filenames) > 0:
            # first print error for the first filename given
            self.ctx.errors.sort(key=keyfun)

        error_messages = []
        for (epos, etag, eargs) in self.ctx.errors:
            elevel = error.err_level(etag)
            if error.is_warning(elevel):
                logger.warning('%s: %s\n' %
                               (str(epos), error.err_to_str(etag, eargs)))
            else:
                err_msg = '%s: %s\n' % (str(epos), error.err_to_str(
                    etag, eargs))
                logger.error(err_msg)
                error_messages.append(err_msg)

        if len(error_messages) > 0:
            err_msg = '\n'.join(error_messages)
            raise YdkGenException(err_msg)
コード例 #6
0
    def _get_pyang_modules(self, filenames):
        modules = []
        regex_expression = re.compile(
            r"^(.*?)(\@(\d{4}-\d{2}-\d{2}))?\.(yang|yin)$")
        for filename in filenames:
            base_file_name = filename
            if filename.startswith('file://'):
                base_file_name = filename[len('file://') - 1:]
            try:
                fd = open(base_file_name)
                text = fd.read()
            except IOError as ex:
                err_msg = "error %s: %s\n" % (filename, str(ex))
                logger.error(err_msg)
                raise YdkGenException(err_msg)

            match = regex_expression.search(filename)
            self.ctx.yin_module_map = {}
            if match is not None:
                (name, _dummy, rev, _) = match.groups()
                name = os.path.basename(name)
                logger.debug('Parsing file %s format %s name %s revision %s',
                             filename, format, name, rev)
                module = self.ctx.add_module(filename,
                                             text,
                                             format,
                                             name,
                                             rev,
                                             expect_failure_error=False)
            else:
                module = self.ctx.add_module(filename, text)
            if module is None:
                raise YdkGenException('Could not add module ')
            else:
                modules.append(module)
        return modules
コード例 #7
0
ファイル: generate.py プロジェクト: rubiruchi/ydk-gen
    options = parser.parse_args()

    if options.verbose:
        init_verbose_logger()

    if 'YDKGEN_HOME' not in os.environ:
        logger.debug("YDKGEN_HOME not set."
                     " Assuming current directory is working directory.")
        ydk_root = os.getcwd()
    else:
        ydk_root = os.environ['YDKGEN_HOME']

    if options.cached_output_dir:
        if options.output_directory is None or not options.core:
            raise YdkGenException(
                '--output-directory needs to be specified with --cached-output-dir and --core options'
            )

    if options.output_directory is None:
        output_directory = '%s/gen-api' % ydk_root
    else:
        output_directory = options.output_directory

    language = ''
    if options.libydk:
        options.cpp = True
        options.core = True
        language = 'cpp'
    elif options.cpp:
        language = 'cpp'
    elif options.go: