def __handle_type_conversion(self): for srcMod in self.modules.itervalues(): for tc in srcMod.typeCoverts: iutil.DEBUG('>>> tc.yinNode.identifier=%s, tc.yinNode.token=%s' % (tc.yinNode.identifier, tc.yinNode.token)) iutil.DEBUG('>>> Recursive type conversion begin: type[%s] from module[%s]' % (tc.identifier, srcMod.get_name())) cpp_type = self.__recursive_convert(srcMod, tc.identifier) if yc.isYangBuiltinType(cpp_type): cpp_type = yc.yang2cppBuiltinType(cpp_type) tc.yinNode.cpp_type = cpp_type iutil.DEBUG('>>> Recursive type conversion end: orignal type[%s] --> builtin type[%s]\n' % (tc.identifier, tc.yinNode.cpp_type))
def __recursive_convert(self, srcMod_, type_ident): # Will circular definition occur here? Seems that we should use yang compiler to prevent it. iutil.DEBUG('Convert type<%s> from module<%s>' % (type_ident, srcMod_.get_name())) if yc.isYangBuiltinType(type_ident): return type_ident else: prefix, ident = yc.getPrefixAndTag(type_ident) if not prefix: new_type, tarMod = srcMod_.find_type_with_mod(ident) if new_type: return self.__recursive_convert(tarMod, new_type) else: iutil.ERROR('Unkonw type <%s> from module<%s>' % (ident, srcMod_.get_name())) return ident else: tarMod = self.__find_module_by_prefix(srcMod_, prefix) if not tarMod: return type_ident return self.__recursive_convert(tarMod, ident)
def __handle_node(self, N, xmlNode): analyzed = True if N.match('module'): # New namespace occur N.namespace = yc.create_cpp_namespace(N.identifier) # Module name self.set_name(N.identifier) self.set_prefix(xmlNode.find('prefix').attrib['value']) elif N.match('submodule'): self.set_name(N.identifier) # belongs-to belongsTo_node = xmlNode.find('belongs-to') mod_name = belongsTo_node.attrib['module'] pre = belongsTo_node.find('prefix').attrib['value'] self.belongs_to = mod_name self.set_prefix(pre) # New namespace occur N.namespace = yc.create_cpp_namespace(mod_name) elif N.match('belongs-to'): #mod_name , pre = (xmlNode.attrib['module'], xmlNode.find('prefix').attrib['value']) #self.belongs_to = mod_name #self.set_prefix(pre) pass elif N.match('import'): name, pre = (xmlNode.attrib['module'], xmlNode.find('prefix').attrib['value']) im = ct_ORM.ormImport(name, N) self.imports[pre] = im elif N.match('include'): #TODO: Think about it, do I need to insert submoudle's yin tree here? mod_name = xmlNode.attrib['module'] i = ct_ORM.ormIncluding(mod_name, N) self.includes[mod_name] = i elif N.match('typedef'): _tar_type = xmlNode.attrib['name'] _org_type = xmlNode.find('type').attrib['name'] self.typedefs[_tar_type] = _org_type elif N.match('default'): N.default = N.value elif N.match('type'): if yc.isYangBuiltinType(N.identifier): N.cpp_type = yc.yang2cppBuiltinType(N.identifier) else: t = ct_ORM.ormTypeConvert(self.get_prefix(), N) self.typeCoverts.append(t) elif N.match('leaf'): #for subN in N.subNodes: # if subN.match('type'): # N.cpp_type = subN.identifier # elif subN.match('default'): # N.default = subN.value pass elif N.match('container'): # New namespace occur N.namespace = yc.create_cpp_namespace(N.identifier) # Other N.complex = True c = ct_ORM.ormContainer(self.get_prefix(), N) N.parNode.stmts.insert(0, c) elif N.match('list'): # New namespace occur N.namespace = yc.create_cpp_namespace(N.identifier) # Other N.complex = True l = ct_ORM.ormListItem(self.get_prefix(), N) lm = ct_ORM.ormList(self.get_prefix(), N) # l should be defined before lm N.parNode.stmts.insert(0, lm) N.parNode.stmts.insert(0, l) elif N.match('leaf-list'): N.complex = True ll = ct_ORM.ormLeafList(self.get_prefix(), N) N.parNode.stmts.insert(0, ll) elif N.match('grouping'): # New namespace occur N.namespace = yc.create_cpp_namespace('grouping-' + N.identifier) N.invisible = True # other g = ct_ORM.ormGrouping(self.get_prefix(), N) if(N.identifier in self.groupings): iutil.WARNING('Repeated grouping<%s> in module<%s>' % (N.identifier, self.get_name())) self.groupings[N.identifier] = g elif N.match('uses'): # New namespace occur N.namespace = yc.create_cpp_namespace('uses-' + N.identifier) u = ct_ORM.ormUses(self.get_prefix(), N) self.uses.append(u) elif N.match('augment'): # New namespace occur N.namespace = yc.create_cpp_namespace(self.prefix + N.augTarNode) N.invisible = True # other a = ct_ORM.ormAugment(self.get_prefix(), N) self.augments.append(a) elif N.match('choice'): # New namespace occur N.namespace = yc.create_cpp_namespace(N.identifier) N.complex = True ch = ct_ORM.ormChoice(self.get_prefix(), N) N.parNode.stmts.insert(0, ch) elif N.match('case'): # New namespace occur N.namespace = yc.create_cpp_namespace(N.identifier) N.complex = True ca = ct_ORM.ormCase(self.get_prefix(), N) N.parNode.stmts.insert(0, ca) elif N.match('identity'): base_name = None for xmlChild in xmlNode: if xmlChild.tag == 'base': base_name = xmlChild.attrib['name'] ident = ct_ORM.ormIdentity(self.get_prefix(), N, base_name) self.identities.append(ident) else: if not N.isInterested(): analyzed = False iutil.DEBUG('Not interested token<%s>' % N.token) return analyzed