Ejemplo n.º 1
0
def print_children(i_children,
                   module,
                   fd,
                   prefix,
                   path,
                   ctx,
                   level=0,
                   exdata=None):
    global depth
    if len(i_children) > 0:
        depth = max(depth, level)
    for ch in i_children:
        disabled_feature = False
        iffeature = ch.search('if-feature')
        for f in iffeature:
            # find a feature in imported module [by robot]
            if ":" in f.arg:
                import_prefix, import_feature = util.split_identifier(f.arg)
                if import_prefix is not None and import_feature is not None:
                    import_pos = None
                    import_err = []
                    import_module = util.prefix_to_module(
                        module, import_prefix, import_pos, import_err)
                    if import_module is not None:
                        if import_module in ctx.features and import_feature not in ctx.features[
                                import_module.arg]:
                            print(ch.search("feature"),
                                  ch.search('if-feature'),
                                  ctx.features[module.arg], "disabled feature")
                            # ignore disabled feature
                            disabled_feature = True
            else:
                if f.arg not in ctx.features[module.arg]:
                    print(ch.search("feature"), ch.search('if-feature'),
                          ctx.features[module.arg], "disabled feature")
                    # ignore disabled feature
                    disabled_feature = True


#            if f.arg not in ctx.features[module.arg]:
#                print (ch.search("feature"), ch.search('if-feature'), ctx.features[module.arg], "disabled feature")
#                # ignore disabled feature
#                disabled_feature = True
        if not disabled_feature:
            print_node(ch, module, fd, prefix, path, ctx, level, exdata=exdata)
Ejemplo n.º 2
0
def typestring(node):
    def get_nontypedefstring(node):
        s = ""
        found = False
        t = node.search_one('type')
        if t is not None:
            s = t.arg + '\n'
            if t.arg == 'enumeration':
                found = True
                s = s + ' : {'
                for enums in t.substmts:
                    s = s + enums.arg + ','
                s = s + '}'
            elif t.arg == 'leafref':
                found = True
                s = s + ' : '
                p = t.search_one('path')
                if p is not None:
                    s = s + p.arg

            elif t.arg == 'identityref':
                found = True
                b = t.search_one('base')
                if b is not None:
                    s = s + ' {' + b.arg + '}'

            elif t.arg == 'union':
                found = True
                uniontypes = t.search('type')
                s = s + '{' + uniontypes[0].arg
                for uniontype in uniontypes[1:]:
                    s = s + ', ' + uniontype.arg
                s = s + '}'

            typerange = t.search_one('range')
            if typerange is not None:
                found = True
                s = s + ' [' + typerange.arg + ']'
            length = t.search_one('length')
            if length is not None:
                found = True
                s = s + ' {length = ' + length.arg + '}'

            pattern = t.search_one('pattern')
            if pattern is not None:  # truncate long patterns
                found = True
                s = s + ' {pattern = ' + pattern.arg + '}'
        return s

    s = get_nontypedefstring(node)

    if s != "":
        t = node.search_one('type')
        # chase typedef
        type_namespace = None
        i_type_name = None
        name = t.arg
        if name.find(":") == -1:
            prefix = None
        else:
            [prefix, name] = name.split(':', 1)
        if prefix is None or t.i_module.i_prefix == prefix:
            # check local typedefs
            pmodule = node.i_module
            typedef = statements.search_typedef(t, name)
        else:
            # this is a prefixed name, check the imported modules
            err = []
            pmodule = util.prefix_to_module(t.i_module, prefix, t.pos, err)
            if pmodule is None:
                return
            typedef = statements.search_typedef(pmodule, name)
        if typedef != None:
            s = s + get_nontypedefstring(typedef)
    return s
Ejemplo n.º 3
0
def getType(node):

    global codegenTypesToYangTypesMap
    xpath = statements.mk_path_str(node, True)

    def resolveType(stmt, nodeType):
        if nodeType == "string" \
            or nodeType == "instance-identifier" \
            or nodeType == "identityref":
            return codegenTypesToYangTypesMap["string"]
        elif nodeType == "enumeration":
            enums = []
            for enum in stmt.substmts:
                if enum.keyword == "enum":
                    enums.append(enum.arg)
            return codegenTypesToYangTypesMap["string"], enums
        elif nodeType == "empty" or nodeType == "boolean":
            return {"type": "boolean", "format": "boolean"}
        elif nodeType == "leafref":
            return handle_leafref(node, xpath)
        elif nodeType == "union":
            return codegenTypesToYangTypesMap["string"]
        elif nodeType == "decimal64":
            return codegenTypesToYangTypesMap[nodeType]
        elif nodeType in [
                'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32',
                'uint64', 'binary', 'bits'
        ]:
            return codegenTypesToYangTypesMap[nodeType]
        else:
            print("no base type found")
            sys.exit(2)

    base_types = [
        'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32',
        'uint64', 'decimal64', 'string', 'boolean', 'enumeration', 'bits',
        'binary', 'leafref', 'identityref', 'empty', 'union',
        'instance-identifier'
    ]
    # Get Type of a node
    t = node.search_one('type')

    while t.arg not in base_types:
        # chase typedef
        name = t.arg
        if name.find(":") == -1:
            prefix = None
        else:
            [prefix, name] = name.split(':', 1)
        if prefix is None or t.i_module.i_prefix == prefix:
            # check local typedefs
            pmodule = node.i_module
            typedef = statements.search_typedef(t, name)
        else:
            # this is a prefixed name, check the imported modules
            err = []
            pmodule = util.prefix_to_module(t.i_module, prefix, t.pos, err)
            if pmodule is None:
                return
            typedef = statements.search_typedef(pmodule, name)

        if typedef is None:
            print(
                "Typedef ", name,
                " is not found, make sure all dependent modules are present")
            sys.exit(2)
        t = typedef.search_one('type')

    return resolveType(t, t.arg)
Ejemplo n.º 4
0
def typestring(node):

    def get_nontypedefstring(node):
        s = {}
        found = False
        t = node.search_one('type')
        if t is not None:
            s['type'] = t.arg
            if t.arg == 'enumeration':
                found = True
                s['enumeration'] = []
                for enums in t.substmts:
                    s['enumeration'].append(enums.arg)
            elif t.arg == 'leafref':
                found = True
                p = t.search_one('path')
                if p is not None:
                    s['path'] = p.arg

            elif t.arg == 'identityref':
                found = True
                b = t.search_one('base')
                if b is not None:
                    s['base'] = b.arg

            elif t.arg == 'union':
                found = True
                uniontypes = t.search('type')
                s['union'] = [uniontypes[0].arg]
                for uniontype in uniontypes[1:]:
                    s['union'].append(uniontype.arg)

            typerange = t.search_one('range')
            if typerange is not None:
                found = True
                s['type_range'] = typerange.arg
            length = t.search_one('length')
            if length is not None:
                found = True
                s['length'] = length.arg

            pattern = t.search_one('pattern')
            if pattern is not None:
                found = True
                s['pattern'] = json_escape(pattern.arg)
        return s

    s = get_nontypedefstring(node)

    if len(s) != 0:
        t = node.search_one('type')
        # chase typedef
        type_namespace = None
        i_type_name = None
        name = t.arg
        if name.find(":") == -1:
            prefix = None
        else:
            [prefix, name] = name.split(':', 1)
        if prefix is None or t.i_module.i_prefix == prefix:
            # check local typedefs
            pmodule = node.i_module
            typedef = statements.search_typedef(t, name)
        else:
            # this is a prefixed name, check the imported modules
            err = []
            pmodule = util.prefix_to_module(
                t.i_module, prefix, t.pos, err)
            if pmodule is None:
                return
            typedef = statements.search_typedef(pmodule, name)
        if typedef != None:
            s['typedef'] = get_nontypedefstring(typedef)
    return s
Ejemplo n.º 5
0
    def build_store_from_definitions(self, ctx, defnd):
        unresolved_identities = list(defnd.keys())
        unresolved_identity_count = {k: 0 for k in defnd}
        error_ids = []

        mod_ref_prefixes = module_import_prefixes(ctx)

        while len(unresolved_identities):
            this_id = unresolved_identities.pop(0)
            iddef = defnd[this_id]

            base = iddef.search_one("base")
            try:
                mainmod = iddef.main_module()
            except AttributeError:
                mainmod = None
            if mainmod is not None:
                defmod = mainmod

            defining_module = defmod.arg
            namespace = defmod.search_one("namespace").arg
            prefix = defmod.search_one("prefix").arg

            if base is None:
                # Add a new identity which can be a base
                tid = Identity(iddef.arg)
                tid.source_module = defining_module
                tid.source_namespace = namespace
                tid.add_prefix(prefix)
                self.add_identity(tid)

                if defining_module in mod_ref_prefixes:
                    for i in mod_ref_prefixes[defining_module]:
                        tid.add_prefix(i)

            else:
                # Determine what the name of the base and the prefix for
                # the base should be
                if ":" in base.arg:
                    base_pfx, base_name = base.arg.split(":")
                else:
                    base_pfx, base_name = prefix, base.arg

                parent_module = util.prefix_to_module(defmod, base_pfx,
                                                      base.pos, ctx.errors)

                # Find whether we have the base in the store
                base_id = self.find_identity_by_source_name(
                    parent_module.arg, base_name)

                if base_id is None:
                    # and if not, then push this identity back onto the stack
                    unresolved_identities.append(this_id)
                    unresolved_identity_count[this_id] += 1
                    # FIXME(Joey Wilhelm): `unresolved_idc` and `ident` are both undefined. What is the intent of this code?
                    # if unresolved_identity_count[this_id] > 1000:
                    #   if unresolved_idc[ident] > 1000:
                    #     sys.stderr.write("could not find a match for %s base: %s\n" %
                    #       (iddef.arg, base_name))
                    #     error_ids.append(ident)
                else:
                    # Check we don't already have this identity defined
                    if self.find_identity_by_source_name(
                            defining_module, iddef.arg) is None:
                        # otherwise, create a new identity that reflects this one
                        tid = Identity(iddef.arg)
                        tid.source_module = defining_module
                        tid.source_namespace = namespace
                        tid.add_prefix(prefix)
                        base_id.add_child(tid)
                        self.add_identity(tid)

                        if defining_module in mod_ref_prefixes:
                            for i in mod_ref_prefixes[defining_module]:
                                tid.add_prefix(i)

            if error_ids:
                raise TypeError("could not resolve identities %s" % error_ids)

        self._build_inheritance()
Ejemplo n.º 6
0
    def build_store_from_definitions(self, ctx, defnd):
        unresolved_identities = list(defnd.keys())
        unresolved_identity_count = {k: 0 for k in defnd}
        error_ids = []

        mod_ref_prefixes = module_import_prefixes(ctx)

        while len(unresolved_identities):
            this_id = unresolved_identities.pop(0)
            iddef = defnd[this_id]

            base = iddef.search_one("base")
            try:
                mainmod = iddef.main_module()
            except AttributeError:
                mainmod = None
            if mainmod is not None:
                defmod = mainmod

            defining_module = defmod.arg
            namespace = defmod.search_one("namespace").arg
            prefix = defmod.search_one("prefix").arg

            if base is None:
                # Add a new identity which can be a base
                tid = Identity(iddef.arg)
                tid.source_module = defining_module
                tid.source_namespace = namespace
                tid.add_prefix(prefix)
                self.add_identity(tid)

                if defining_module in mod_ref_prefixes:
                    for i in mod_ref_prefixes[defining_module]:
                        tid.add_prefix(i)

            else:
                # Determine what the name of the base and the prefix for
                # the base should be
                if ":" in base.arg:
                    base_pfx, base_name = base.arg.split(":")
                else:
                    base_pfx, base_name = prefix, base.arg

                parent_module = util.prefix_to_module(defmod, base_pfx, base.pos, ctx.errors)

                # Find whether we have the base in the store
                base_id = self.find_identity_by_source_name(parent_module.arg, base_name)

                if base_id is None:
                    # and if not, then push this identity back onto the stack
                    unresolved_identities.append(this_id)
                    unresolved_identity_count[this_id] += 1
                    # FIXME(Joey Wilhelm): `unresolved_idc` and `ident` are both undefined. What is the intent of this code?
                    # if unresolved_identity_count[this_id] > 1000:
                    #   if unresolved_idc[ident] > 1000:
                    #     sys.stderr.write("could not find a match for %s base: %s\n" %
                    #       (iddef.arg, base_name))
                    #     error_ids.append(ident)
                else:
                    # Check we don't already have this identity defined
                    if self.find_identity_by_source_name(defining_module, iddef.arg) is None:
                        # otherwise, create a new identity that reflects this one
                        tid = Identity(iddef.arg)
                        tid.source_module = defining_module
                        tid.source_namespace = namespace
                        tid.add_prefix(prefix)
                        base_id.add_child(tid)
                        self.add_identity(tid)

                        if defining_module in mod_ref_prefixes:
                            for i in mod_ref_prefixes[defining_module]:
                                tid.add_prefix(i)

            if error_ids:
                raise TypeError("could not resolve identities %s" % error_ids)

        self._build_inheritance()
Ejemplo n.º 7
0
    def find_target_node(self, stmt):
        inthismod = True
        if stmt.arg.startswith('/'):
            is_absolute = True
            arg = stmt.arg
        else:
            is_absolute = False
            arg = "/" + stmt.arg
        # parse the path into a list of two-tuples of (prefix,identifier)
        path = [(m[1], m[2])
                for m in syntax.re_schema_node_id_part.findall(arg)]
        # find the module of the first node in the path
        (prefix, identifier) = path[0]
        if prefix == '':
            inthismod = True
        else:
            inthismod = (prefix == self.thismod_prefix)
        # sys.stderr.write("prefix for %s : %s \n" %(path, prefix))
        module = util.prefix_to_module(stmt.i_module, prefix, stmt.pos,
                                       self._ctx.errors)
        if module is None:
            # error is reported by prefix_to_module
            return inthismod, None
        if is_absolute:
            # find the first node
            node = statements.search_data_keyword_child(
                module.i_children, module.i_modulename, identifier)
            if node is None:
                # check all our submodules
                for inc in module.search('include'):
                    submod = self._ctx.get_module(inc.arg)
                    if submod is not None:
                        node = statements.search_data_keyword_child(
                            submod.i_children, submod.i_modulename, identifier)
                        if node is not None:
                            break
                if node is None:
                    err_add(self._ctx.errors, stmt.pos, 'NODE_NOT_FOUND',
                            (module.arg, identifier))
                    return inthismod, None
            path = path[1:]
        else:
            if hasattr(stmt.parent, 'i_annotate_node'):
                node = stmt.parent.i_annotate_node
            else:
                err_add(self._ctx.errors, stmt.pos, 'BAD_ANNOTATE', ())
                return inthismod, None

        # then recurse down the path
        for prefix, identifier in path:
            module = util.prefix_to_module(stmt.i_module, prefix, stmt.pos,
                                           self._ctx.errors)
            if module is None:
                return None
            if hasattr(node, 'i_children'):
                children = node.i_children
            else:
                children = []
            child = statements.search_data_keyword_child(
                children, module.i_modulename, identifier)
            if child is None:
                err_add(self._ctx.errors, stmt.pos, 'NODE_NOT_FOUND',
                        (module.arg, identifier))
                return inthismod, None
            node = child

        stmt.i_annotate_node = node
        return inthismod, node