def find_target_node(ctx, stmt, is_instance_allowed = False): """Find the target node for the 'refine' or 'augment' statements""" parent = stmt.parent if stmt.arg == '.': return parent # parse the path into a list of two-tuples of (prefix,identifier) pstr = '/' + stmt.arg path = [(m[1], m[2]) for m in syntax.re_schema_node_id_part.findall(pstr)] node = parent # go down the path for (prefix, identifier) in path: if not is_instance_allowed and node is not parent and is_instation(node): err_add(ctx.errors, stmt.pos, 'BAD_REF_AUG', (node.arg, node.pos)) return None module = statements.prefix_to_module(parent.i_module, prefix, stmt.pos, ctx.errors) if module is None: return None child = statements.search_child(node.i_children, module.i_modulename, identifier) if child is None: err_add(ctx.errors, stmt.pos, 'NODE_NOT_FOUND', (module.i_modulename, identifier)) return None node = child return node
def resolve_complex_type(ctx, prefix, name, stmt): """Resolves a complex type by prefix and name in the scope of stmt.""" if prefix is None or stmt.i_module.i_prefix == prefix: # check local complex types stmt.i_complex_type = search_complex_type(stmt, name) else: # this is a prefixed name, check the imported modules pmodule = statements.prefix_to_module(stmt.i_module, prefix, stmt.pos, ctx.errors) if pmodule is None: return stmt.i_complex_type = search_complex_type(pmodule, name)
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 = statements.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 = statements.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
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 = statements.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
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 = statements.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
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 = statements.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 = statements.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
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 = statements.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)
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 = statements.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