make_structs.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 from snippets.generators.cppgen.cppgen import CppGen
00003 from snippets.generators.elements.structure import Structure
00004 from snippets.generators.elements.function import Function
00005 from snippets.generators.elements.variable import Variable
00006 import xml.etree.ElementTree as ET
00007 from _dbus_bindings import String
00008 
00009 def gen_factory_initializer(structs, bindent = '', indent = '  '):
00010     """
00011     Generates the factory map initializer.
00012         
00013     bindent -- the current indent in the document
00014     indent -- the usual minimum indentation
00015     """
00016     code = ''
00017     for struct in structs:
00018         code = (code + indent + '{' + struct.name + '::CID, createInstance<' +
00019          struct.name + '>},\n')   
00020     
00021     #Remove the last comma and newline
00022     return code[:-2]
00023 
00024 def gen_names(structs, bindent = '', indent = '  '):
00025     """
00026     Generates the human readable names of CIDs.
00027         
00028     bindent -- the current indent in the document
00029     indent -- the usual minimum indentation
00030     """
00031     code = ''
00032     for struct in structs:
00033         code = (code + indent + '{' + struct.name + '::CID, \"' + 
00034             struct.name + '\"},\n')   
00035     
00036     #Remove the last comma and newline
00037     return code[:-2]
00038 
00039 
00040 def create_datatypes(xmlroot, gen, folder = ''):
00041     structs = []
00042     
00043     for node in xmlroot.findall('struct'):
00044         structs.append(Structure(node))
00045         
00046     for struct in structs:
00047         gen.packable_types.append(struct.name)
00048         
00049     fdt = open(folder + '/' + 'datatype_defs.h','w')
00050     fdt2 = open(folder + '/' + 'datatype_impl.h','w')
00051     for struct in structs:
00052         fdt.write(gen.gen_header(struct))
00053         fdt.write('\n\n')
00054         fdt2.write(gen.gen_impl(struct, serialization = False))
00055         fdt2.write('\n\n')
00056         
00057     fdt.close()
00058     
00059     return structs
00060           
00061 def create_messages(xmlroot, gen, prefix, folder = ''):
00062     structs = []
00063     for node in xmlroot.findall('struct'):
00064         structs.append(Structure(node))
00065     
00066     for struct in structs:
00067         gen.packable_types.append(struct.name)
00068         
00069     for s in structs:
00070         '''Add automatic inheritance and virtual method implementations'''
00071         s.inherit = 'SeatracMessage'
00072         #Ptr typedef
00073         t = ('boost::shared_ptr< ' + s.name +' > ', 'Ptr')     
00074         s.typedefs.append(t)  
00075         t = ('boost::shared_ptr< ' + s.name +' const > ', 'ConstPtr') 
00076         s.typedefs.append(t)  
00077         #get_cid function
00078         f = Function()
00079         f.name = 'getCid'
00080         f.ret = 'int'
00081         f.body = 'return ' + s.name + '::CID;'
00082         f.inline = True
00083         f.qual = 'const'
00084         s.methods.append(f)
00085         
00086         #Test message type
00087         f = Function()
00088         f.name = 'isCommand'
00089         f.ret = 'bool'
00090         if prefix == "command":
00091             f.body = 'return true;'
00092         else:
00093             f.body = 'return false;'
00094             
00095         f.qual = 'const'
00096         s.methods.append(f)
00097                       
00098     
00099     filebase = folder + '/' + prefix
00100     
00101     cmi = open(filebase + '_factory_initializer.h','w')
00102     cmi.write(gen_factory_initializer(structs))
00103     cmi.close()
00104     
00105     cmi = open(filebase + '_names.h','w')
00106     cmi.write(gen_names(structs))
00107     cmi.close()
00108     
00109     smsg = open(filebase + '_defs.h','w')
00110     smsgimpl = open(filebase + '_impl.h','w')
00111     for struct in structs:
00112         smsg.write(gen.gen_header(struct))
00113         smsg.write('\n\n')
00114         smsgimpl.write(gen.gen_impl(struct, serialization=False))
00115         smsgimpl.write('\n\n')
00116     smsg.close()
00117         
00118     return structs
00119 
00120 def cpp_create_structs(gen, definition, folder, name):
00121     structs = []
00122     
00123     xmlroot = ET.parse(definition).getroot()
00124     
00125     for node in xmlroot.findall('struct'):
00126         structs.append(Structure(node))
00127         
00128     for struct in structs:
00129         gen.packable_types.append(struct.name)
00130         
00131     fdt = open(folder + '/' + name + '_defs.h','w')
00132     fdt2 = open(folder + '/' + name + '_impl.h','w')
00133         
00134     for struct in structs:
00135         fdt.write(gen.gen_header(struct))
00136         fdt.write('\n\n')
00137         fdt2.write(gen.gen_impl(struct, serialization = False))
00138         fdt2.write('\n\n')
00139         
00140     fdt.close()
00141     fdt2.close()
00142 
00143         
00144     return structs
00145     
00146 
00147 if __name__ == '__main__':
00148     import argparse
00149     parser = argparse.ArgumentParser(description='Create C++ or Java structures from XML defintion.')
00150     parser.add_argument('--definition', 
00151                         help='the structure XML definition file', required=True)
00152     parser.add_argument('--output-dir', default='.',
00153                         help='directory where to save the files (default: .)')
00154     parser.add_argument('--output-name', default='datatype',
00155                         help='base name of the definition and implementation file (default: datatype)')
00156 
00157     args = parser.parse_args()
00158     
00159     
00160     gen = CppGen()
00161     cpp_create_structs(gen, args.definition, args.output_dir, args.output_name)
00162     
00163 #     
00164 #     gen = CppGen()
00165 #     gen.array_len_type = 'uint8_t'   
00166 #     
00167 #     '''Parse the Seatrac data types and create the definitions'''
00168 
00169 #     '''Parse the Seatrac commands and create the initializer'''
00170 #     cmd = create_messages(ET.parse('definitions/SeatracCommands.xml').getroot(),
00171 #                           gen, 
00172 #                           'command',
00173 #                           'include/labust/seatrac/detail/')
00174 #     '''Parse the Seatrac responses and create the initializer'''
00175 #     resp = create_messages(ET.parse('definitions/SeatracResponses.xml').getroot(),
00176 #                            gen, 
00177 #                            'response',
00178 #                            'include/labust/seatrac/detail/')  
00179 #     
00180 #     exit()
00181 #     
00182 #     structs.extend(dt);
00183 #     structs.extend(cmd);
00184 #     structs.extend(resp);
00185 #   
00186 #     '''Create boost serializator for all defined structures'''  
00187 #     serdef = open('include/labust/seatrac/detail/serialization_defs.h','w')
00188 #     for struct in structs:
00189 #         serdef.write(struct_serializer.gen_serializer(struct, ns='labust::seatrac'))
00190 #         serdef.write('\n')
00191         


snippets
Author(s): Gyula Nagy
autogenerated on Fri Aug 28 2015 11:22:33