def create_inport_converter_module(parser, name, typename, verbose=False): module_dir = 'modules' if not os.path.isdir(module_dir): os.mkdir(module_dir) global_module = parser.global_module typs = global_module.find_types(typename) if len(typs) == 0: print 'Invalid Type Name (%s)' % typename raise InvalidDataTypeException() module_name = typs[0].parent.name copy_idl_and_compile(parser, typs[0].filepath) filename = '%s_InPort_%s.py' % (name, typename.replace('::', '_').strip()) f = open(os.path.join(module_dir, filename), 'w') import value_dic as vd value_dic = vd.generate_value_dic(global_module, typename, root_name='data', verbose=verbose) global _template output = "%s" % _template code = create_converter(value_dic, list_name='d_list', indent = ' ') output = output.replace('$NAME', name) typs = global_module.find_types(typename) output = output.replace('$CONSTRUCTOR', parser.generate_constructor_python(typs[0])) output = output.replace('$CODE', code) output = 'import %s\n' % module_name + output f.write(output) f.close()
def create_typefromlist_converter(parser, typ, verbose=False): # print 'create_typefromlist_converter : %s\n' % typ.name global_module = parser.global_module converter_code = '' arg_typ = typ if arg_typ.is_typedef: arg_typ = typ.obj.inner_type if not arg_typ.is_primitive: if arg_typ.obj.is_enum: return converter_code if not typ.name in converted_types: import value_dic as vd value_dic = vd.generate_value_dic(global_module, arg_typ.name, root_name='data', verbose=verbose) converted_types.append(typ.name) converter_code = _fromlist_converter_template converter_code = converter_code.replace('$TYPENAME', typ.name.split('::')[-1]) converter_code = converter_code.replace('$CONSTRUCTOR', parser.generate_constructor_python(typ.obj)) code = create_fromlist_converter(value_dic, list_name='d_list', indent = ' ') converter_code = converter_code.replace('$CODE', code) return converter_code
def create_inport_converter_module(parser, name, typename, verbose=False): module_dir = 'modules' if not os.path.isdir(module_dir): os.mkdir(module_dir) global_module = parser.global_module typs = global_module.find_types(typename) if len(typs) == 0: print 'Invalid Type Name (%s)' % typename raise InvalidDataTypeException() module_name = typs[0].parent.name copy_idl_and_compile(parser, typs[0].filepath) filename = '%s_InPort_%s.py' % (name, typename.replace('::', '_').strip()) f = open(os.path.join(module_dir, filename), 'w') import value_dic as vd value_dic = vd.generate_value_dic(global_module, typename, root_name='data', verbose=verbose) global _template output = "%s" % _template code = create_tolist_converter(value_dic, list_name='d_list', indent=' ') output = output.replace('$NAME', name) typs = global_module.find_types(typename) output = output.replace('$CONSTRUCTOR', parser.generate_constructor_python(typs[0])) output = output.replace('$CODE', code) output = 'import %s\n' % module_name + output f.write(output) f.close()
def create_typetolist_converter(parser, typ, verbose=False): # print 'create_typetolist_converter : %s\n' % typ.name global_module = parser.global_module converter_code = '' arg_typ = typ if arg_typ.is_primitive: return '' if arg_typ.obj.is_typedef: arg_typ = arg_typ.obj.type if arg_typ.is_primitive: return '' #return create_typetolist_converter(parser, arg_typ, verbose=verbose) if arg_typ.obj.is_sequence: inner_type = arg_typ.obj.inner_type ## Add Innter type converter first. converter_code = create_typetolist_converter(parser, inner_type, verbose=verbose) if not inner_type.is_primitive: if inner_type.obj.is_typedef: inner_type = inner_type.obj.type if inner_type.is_primitive: code = """ d_list = [] for d in data: d_list.append(str(d)) """ converter_code = converter_code + _tolist_converter_template converter_code = converter_code.replace('$TYPENAME', typ.name.split('::')[-1]) converter_code = converter_code.replace('$CONSTRUCTOR', parser.generate_constructor_python(typ.obj)) converter_code = converter_code.replace('$CODE', code) return converter_code else: if inner_type.obj.is_enum: code = """ d_list = [] for d in data: d_list.append(str(d._v)) """ else: code = """ d_list = [] for d in data: d_list = d_list + _%s_tolist(d) """ % inner_type.name.split('::')[-1] converter_code = converter_code + _tolist_converter_template converter_code = converter_code.replace('$TYPENAME', typ.name.split('::')[-1]) converter_code = converter_code.replace('$CONSTRUCTOR', parser.generate_constructor_python(typ.obj)) #code = create_tolist_converter(value_dic, list_name='d_list', indent = ' ') converter_code = converter_code.replace('$CODE', code) return converter_code if arg_typ.obj.is_enum: return converter_code if not arg_typ.name in converted_types: import value_dic as vd value_dic = vd.generate_value_dic(global_module, arg_typ.name, root_name='data', verbose=verbose) print arg_typ.name, value_dic converted_types.append(arg_typ.name) converter_code = _tolist_converter_template converter_code = converter_code.replace('$TYPENAME', typ.name.split('::')[-1]) converter_code = converter_code.replace('$CONSTRUCTOR', parser.generate_constructor_python(arg_typ.obj)) code = create_tolist_converter(value_dic, list_name='d_list', indent = ' ') converter_code = converter_code.replace('$CODE', code) print converter_code # print typ, 'cc', converter_code return converter_code