def generateBlobsH(dstFile): hOut = open(dstFile,'w') writeHeader(hOut,0) hOut.write('#ifndef __BLOBS_H__\n') hOut.write('#define __BLOBS_H__\n\n') GenBlob.genBlobTree(hOut,requestList,'blob_http_rqt',False) GenBlob.genBlobTree(hOut,mimesListPost,'mimes_tree',False) GenBlob.genBlobTree(hOut,attributList,'blob_http_header_content',False) hOut.write('\n#endif\n') hOut.close()
def generateBlobsH(dstFile): hOut = open(dstFile, 'w') writeHeader(hOut, 0) hOut.write('#ifndef __BLOBS_H__\n') hOut.write('#define __BLOBS_H__\n\n') GenBlob.genBlobTree(hOut, requestList, 'blob_http_rqt', False) GenBlob.genBlobTree(hOut, mimesListPost, 'mimes_tree', False) GenBlob.genBlobTree(hOut, attributList, 'blob_http_header_content', False) hOut.write('\n#endif\n') hOut.close()
def generateBlobsH(dstFile): hOut = open(dstFile, "w") writeHeader(hOut, 0) hOut.write("#ifndef __BLOBS_H__\n") hOut.write("#define __BLOBS_H__\n\n") GenBlob.genBlobTree(hOut, requestList, "blob_http_rqt", False) GenBlob.genBlobTree(hOut, mimesListPost, "mimes_tree", False) GenBlob.genBlobTree(hOut, attributList, "blob_http_header_content", False) hOut.write("\n#endif\n") hOut.close()
def generateIndex(dstDir,sourcesMap,target,chuncksNbits,appBase,propsFilesMap): # files selection, static files and geenrators separation filesNames = sourcesMap.keys() staticFilesNames = filter(lambda x: getResourceType(x) == StaticResource,filesNames) generatorFilesNames = filter(lambda x: getResourceType(x) == DynamicResource,filesNames) # extract XML information for each generetor file (if needed) for fileName in generatorFilesNames: if not propsFilesMap[fileName].has_key('hasXml'): extractPropsFromXml(fileName,propsFilesMap[fileName]) # remove c files that do not contain any XML: they are not generators generatorFilesNames = filter(lambda x: propsFilesMap[x]['hasXml'],generatorFilesNames) # c file creation cOut = open(target,'w') writeHeader(cOut,0) cOut.write('#include "handlers.h"\n') # external handler references cOut.write('\n/********** External references **********/\n') for fileName in staticFilesNames: cName = getCName(fileName) cOut.write('extern CONST_VAR(struct output_handler_t, ' + cName + '_handler);\n') for fileName in generatorFilesNames: cFuncName = getCName(fileName[:fileName.rfind('.c')]) cOut.write('extern CONST_VAR(struct output_handler_t, ' + cFuncName + ');\n') # filesRef is a map used to associate URLs to output_handlers filesRefs = {} # static files for fileName in set(staticFilesNames): # retrieve the output_handler for this file handlerName = getCName(fileName) + '_handler' # retrive the URL for this file fileName = sourcesMap[fileName] # .embed extensions are deleted if fileName.endswith('.embed'): fileName = fileName[:fileName.rfind('.')] # URLs always use '/' as file separator if os.sep == '\\': fileName = fileName.replace('\\','/') # update filesRef filesRefs[fileName] = handlerName # the case of index.hmtl files if os.path.basename(fileName) == 'index.html': fileName = os.path.dirname(fileName) if fileName != os.sep: fileName = fileName + '/' filesRefs[fileName] = handlerName # generator # fp is the file post list fp = [] for fileName in set(generatorFilesNames): # retrieve the output_handler for this file handlerName = fileName[:fileName.rfind('.')] handlerName = getCName(handlerName) # save fileName fn = fileName # retrive the URL for this file fileName = sourcesMap[fileName] # .c extension is deleted fileName = fileName[:fileName.rfind('.c')] # URLs always use '/' as file separator if os.sep == '\\': fileName = fileName.replace('\\','/') #update filesRef filesRefs[fileName] = handlerName # search if it's POST URL if fn in postList: fp.append(fileName) # the ordered list of URLs filesList = filesRefs.keys() filesList.sort() # files index creation (table of ordered output_handlers) cOut.write('\n/********** Files index **********/\n') cOut.write('CONST_VAR(const struct output_handler_t /*CONST_VAR*/ *, resources_index[]) = {\n') # insert each handler for file in filesList: cOut.write('\t&' + filesRefs[file] + ',\n') # the final handler is NULL cOut.write('\tNULL,\n') cOut.write('};\n') # construct filesList with end caracter for post urls filesListNew = [] for fileName in filesList: if fileName in fp: filesListNew.append(fileName + chr(255)) else: filesListNew.append(fileName) # generate the URLs tree GenBlob.genBlobTree(cOut,filesListNew,'urls_tree',False) cOut.close()
def generateDynamicResource(srcFile,dstFile,dstFileInfos): # extract the properties from the XML (if needed) fileData = extractPropsFromXml(srcFile,dstFileInfos) # if the c/h file does not conatin any XML, we simply copy it: this is not a generator if not dstFileInfos['hasXml']: shutil.copyfile(srcFile,dstFile) # here, the c file is a generator. It will be enriched else: generatedMimesArray = '' cFuncName = getCName(srcFile[:srcFile.rfind('.c')]) generatedHeader = '#include "generators.h"\n' generatedHeader += '#include "stddef.h"\n\n' generatedHeader += '#include "defines.h"\n\n' generatedOutputHandler = '/********** Output handler **********/\n' # handler functions declaration if dstFileInfos.has_key('init'): generatedOutputHandler += 'static generator_init_func_t ' + dstFileInfos['init'] + ';\n' if dstFileInfos.has_key('initGet'): generatedOutputHandler += 'static generator_initget_func_t ' + dstFileInfos['initGet'] + ';\n' if dstFileInfos.has_key('doGet'): generatedOutputHandler += 'static generator_doget_func_t ' + dstFileInfos['doGet'] + ';\n' if dstFileInfos.has_key('doPostOut'): generatedOutputHandler += 'static generator_dopost_out_func_t ' + dstFileInfos['doPostOut'] + ';\n' if dstFileInfos.has_key('doPostIn'): generatedOutputHandler += 'static generator_dopost_in_func_t ' + dstFileInfos['doPostIn'] + ';\n' if dstFileInfos.has_key('doPost'): generatedOutputHandler += 'static generator_doget_func_t ' + dstFileInfos['doPost'] + ';\n' # other protocols than TCP handlers if dstFileInfos.has_key('doPacketIn'): generatedOutputHandler += '#ifndef DISABLE_GP_IP_HANDLER\n' generatedOutputHandler += 'static generator_dopacket_in_func_t ' + dstFileInfos['doPacketIn'] + ';\n' if dstFileInfos.has_key('doPacketOut'): generatedOutputHandler += 'static generator_dopacket_out_func_t ' + dstFileInfos['doPacketOut'] + ';\n' generatedOutputHandler += '#endif\n' # output_handler structure creation generatedOutputHandler += 'CONST_VAR(struct output_handler_t, ' + cFuncName + ') = {\n' # handler type if dstFileInfos.has_key('doPacketIn'): generatedOutputHandler += '\t.handler_type = type_general_ip_handler,\n' else: generatedOutputHandler += '\t.handler_type = type_generator,\n' generatedOutputHandler += '\t.handler_comet = %d,\n' %(1 if dstFileInfos['channel'] != '' else 0) generatedOutputHandler += '\t.handler_stream = %d,\n' %(1 if dstFileInfos['interaction'] == 'streaming' else 0) if dstFileInfos['persistence'] == 'persistent': etype = 'prop_persistent' elif dstFileInfos['persistence'] == 'idempotent': etype = 'prop_idempotent' elif dstFileInfos['persistence'] == 'volatile': etype = 'prop_volatile' if etype == None: etype = 'prop_persistent' # the handler is a dynamic resource (generator), we fill the function handlers fields generatedOutputHandler += '\t.handler_data = {\n' generatedOutputHandler += '\t\t.generator = {\n' generatedOutputHandler += '\t\t\t.prop = %s,\n' %etype # init if not dstFileInfos.has_key('init'): generatedOutputHandler += '\t\t\t.init = NULL,\n' else: generatedOutputHandler += '\t\t\t.init = ' + dstFileInfos['init'] + ',\n' generatedOutputHandler += '\t\t\t.handlers = {\n' if dstFileInfos.has_key('initGet') or dstFileInfos.has_key('doGet') or dstFileInfos.has_key('doPost'): generatedOutputHandler += '\t\t\t\t.get = {\n' # initGet if dstFileInfos.has_key('initGet'): generatedOutputHandler += '\t\t\t\t.initget = ' + dstFileInfos['initGet'] + ',\n' # doGet if dstFileInfos.has_key('doGet'): generatedOutputHandler += '\t\t\t\t\t.doget = ' + dstFileInfos['doGet'] + ',\n' # doPost if dstFileInfos.has_key('doPost'): generatedOutputHandler += '\t\t\t\t.doget = ' + dstFileInfos['doPost'] + ',\n' generatedOutputHandler += '\t\t\t\t},\n' #doPostIn/doPostOut if dstFileInfos.has_key('doPostIn'): generatedOutputHandler += '\t\t\t\t.post = {\n' generatedOutputHandler += '\t\t\t\t\t.dopostin = ' + dstFileInfos['doPostIn'] + ',\n' if dstFileInfos.has_key('doPostOut'): generatedOutputHandler += '\t\t\t\t\t.dopostout = ' + dstFileInfos['doPostOut'] + ',\n' generatedOutputHandler += '\t\t\t\t},\n' #doPacketIn, doPacketOut if dstFileInfos.has_key('doPacketIn'): generatedOutputHandler += '#ifndef DISABLE_GP_IP_HANDLER\n' generatedOutputHandler += '\t\t\t\t.gp_ip = {\n' generatedOutputHandler += '\t\t\t\t.dopacketin = ' + dstFileInfos['doPacketIn'] + ',\n' if dstFileInfos.has_key('doPacketOut'): generatedOutputHandler += '\t\t\t\t.dopacketout = ' + dstFileInfos['doPacketOut'] + ',\n' if dstFileInfos.has_key('protocol'): generatedOutputHandler += '\t\t\t\t.protocol = ' + dstFileInfos['protocol'] + ',\n' generatedOutputHandler += '\t\t\t\t},\n' generatedOutputHandler += '#endif\n' generatedOutputHandler += '\t\t\t},\n' generatedOutputHandler += '\t\t},\n' generatedOutputHandler += '\t},\n' # generator arguments information are also written in the structure generatedOutputHandler += '#ifndef DISABLE_ARGS\n' generatedOutputHandler += '\t.handler_args = {\n' if len(dstFileInfos['argsList']) > 0: generatedOutputHandler += '\t\t.args_tree = args_tree,\n' generatedOutputHandler += '\t\t.args_index = args_index,\n' generatedOutputHandler += '\t\t.args_size = sizeof(struct args_t)\n' else : generatedOutputHandler += '\t\t.args_tree = NULL,\n' generatedOutputHandler += '\t\t.args_index = NULL,\n' generatedOutputHandler += '\t\t.args_size = 0\n' generatedOutputHandler += '\t},\n' generatedOutputHandler += '#endif\n' generatedOutputHandler += '#ifndef DISABLE_POST\n' generatedOutputHandler += '\t.handler_mimes = {\n' if (dstFileInfos.has_key('doPostIn') and len(dstFileInfos['contentTypeList']) > 0) : generatedOutputHandler += '\t\t.mimes_index = mimes_index,\n' generatedOutputHandler += '\t\t.mimes_size = '+ str(len(dstFileInfos['contentTypeList'])) +',\n' elif(dstFileInfos.has_key('doPost') and len(dstFileInfos['argsList']) > 0) : generatedOutputHandler += '\t\t.mimes_index = mimes_index,\n' generatedOutputHandler += '\t\t.mimes_size = 1,\n' else : generatedOutputHandler += '\t\t.mimes_index = NULL,\n' generatedOutputHandler += '\t\t.mimes_size = 0,\n' generatedOutputHandler += '\t},\n' generatedOutputHandler += '#endif\n' generatedOutputHandler += '};\n' if (dstFileInfos.has_key('doPost') or dstFileInfos.has_key('doPostIn')): generatedMimesArray = '/********* Content-types array **********/\n' generatedMimesArray += 'static CONST_VAR(unsigned char, mimes_index[]) = {' countAttr = len(dstFileInfos['contentTypeList']) if(len(dstFileInfos['argsList']) > 0): if 'application/x-www-form-urlencoded' in mimesListPost: generatedMimesArray += str(mimesListPost.index('application/x-www-form-urlencoded')) else: exit('the mime type application/x-www-form-urlencoded don\'t exist in the mimeListPost file') elif len(dstFileInfos['contentTypeList']) > 0: for attrs in dstFileInfos['contentTypeList']: if attrs['type'] in mimesListPost: generatedMimesArray += str(mimesListPost.index(attrs['type'])) else: exit('the mime type ' + attrs['type'] + ' don\'t exist in the mimeListPost file') if countAttr > 1: generatedMimesArray += ',' countAttr -= 1 else: generatedMimesArray += 'NULL' generatedMimesArray += '};\n' # arguments c structure creation (this structure is directly used by the generator functions) generatedArgsStruc = '/********** Arguments structure **********/\n' generatedArgsStruc += 'struct args_t {\n' # for each argument, create a filed in the structure for attrs in dstFileInfos['argsList']: tmpString = '' if attrs['type'] == 'str': tmpString = 'char ' + attrs['name'] + '[' + attrs['size'] + ']' else: tmpString = attrs['type'] + '_t ' + attrs['name'] generatedArgsStruc += '\t' + tmpString + ';\n' generatedArgsStruc += '};\n' # arguments index creation generatedIndex = '/********** Arguments index **********/\n' generatedIndex += 'static CONST_VAR(struct arg_ref_t, args_index[]) = {\n' # for each argument, type and size informations # the offset (in bytes in the structure) is also provided for attrs in dstFileInfos['argsList']: tmpType = '' if attrs['type'] == 'str': tmpType = 'char[' + attrs['size'] + ']' else: tmpType = attrs['type'] + '_t' generatedIndex += '\t{arg_type: ' + argsTypesMap[attrs['type']] + ', arg_size: sizeof(' + tmpType + '), arg_offset: offsetof(struct args_t,' + attrs['name'] + ')},\n' generatedIndex += '};\n' # new c file creation cOut = open(dstFile,'w') writeHeader(cOut,1) cOut.write(generatedHeader) if (dstFileInfos.has_key('doPost') or dstFileInfos.has_key('doPostIn')): cOut.write('#ifndef DISABLE_POST\n') cOut.write(generatedMimesArray) cOut.write('\n') cOut.write('#endif\n') if len(dstFileInfos['argsList']) > 0: cOut.write('#ifndef DISABLE_ARGS\n') cOut.write(generatedArgsStruc) cOut.write('\n') cOut.write(generatedIndex) GenBlob.genBlobTree(cOut,map(lambda x: x['name'],dstFileInfos['argsList']),'args_tree',True) cOut.write('#endif\n') cOut.write('\n') cOut.write(generatedOutputHandler) # the end of the file contains the original c file cOut.write('\n/* End of the enriched part */\n\n') cOut.write(fileData) cOut.close()
def generateIndex(dstDir, sourcesMap, target, chuncksNbits, appBase, propsFilesMap): # files selection, static files and geenrators separation filesNames = sourcesMap.keys() staticFilesNames = filter(lambda x: getResourceType(x) == StaticResource, filesNames) generatorFilesNames = filter( lambda x: getResourceType(x) == DynamicResource, filesNames) # extract XML information for each generetor file (if needed) for fileName in generatorFilesNames: if not propsFilesMap[fileName].has_key('hasXml'): extractPropsFromXml(fileName, propsFilesMap[fileName]) # remove c files that do not contain any XML: they are not generators generatorFilesNames = filter(lambda x: propsFilesMap[x]['hasXml'], generatorFilesNames) # c file creation cOut = open(target, 'w') writeHeader(cOut, 0) cOut.write('#include "handlers.h"\n') # external handler references cOut.write('\n/********** External references **********/\n') for fileName in staticFilesNames: cName = getCName(fileName) cOut.write('extern CONST_VAR(struct output_handler_t, ' + cName + '_handler);\n') for fileName in generatorFilesNames: cFuncName = getCName(fileName[:fileName.rfind('.c')]) cOut.write('extern CONST_VAR(struct output_handler_t, ' + cFuncName + ');\n') # filesRef is a map used to associate URLs to output_handlers filesRefs = {} # static files for fileName in set(staticFilesNames): # retrieve the output_handler for this file handlerName = getCName(fileName) + '_handler' # retrive the URL for this file fileName = sourcesMap[fileName] # .embed extensions are deleted if fileName.endswith('.embed'): fileName = fileName[:fileName.rfind('.')] # URLs always use '/' as file separator if os.sep == '\\': fileName = fileName.replace('\\', '/') # update filesRef filesRefs[fileName] = handlerName # the case of index.hmtl files if os.path.basename(fileName) == 'index.html': fileName = os.path.dirname(fileName) if fileName != os.sep: fileName = fileName + '/' filesRefs[fileName] = handlerName # generator # fp is the file post list fp = [] for fileName in set(generatorFilesNames): # retrieve the output_handler for this file handlerName = fileName[:fileName.rfind('.')] handlerName = getCName(handlerName) # save fileName fn = fileName # retrive the URL for this file fileName = sourcesMap[fileName] # .c extension is deleted fileName = fileName[:fileName.rfind('.c')] # URLs always use '/' as file separator if os.sep == '\\': fileName = fileName.replace('\\', '/') #update filesRef filesRefs[fileName] = handlerName # search if it's POST URL if fn in postList: fp.append(fileName) # the ordered list of URLs filesList = filesRefs.keys() filesList.sort() # files index creation (table of ordered output_handlers) cOut.write('\n/********** Files index **********/\n') cOut.write( 'CONST_VAR(const struct output_handler_t /*CONST_VAR*/ *, resources_index[]) = {\n' ) # insert each handler for file in filesList: cOut.write('\t&' + filesRefs[file] + ',\n') # the final handler is NULL cOut.write('\tNULL,\n') cOut.write('};\n') # construct filesList with end caracter for post urls filesListNew = [] for fileName in filesList: if fileName in fp: filesListNew.append(fileName + chr(255)) else: filesListNew.append(fileName) # generate the URLs tree GenBlob.genBlobTree(cOut, filesListNew, 'urls_tree', False) cOut.close()
def generateDynamicResource(srcFile, dstFile, dstFileInfos): # extract the properties from the XML (if needed) fileData = extractPropsFromXml(srcFile, dstFileInfos) # if the c/h file does not conatin any XML, we simply copy it: this is not a generator if not dstFileInfos['hasXml']: shutil.copyfile(srcFile, dstFile) # here, the c file is a generator. It will be enriched else: generatedMimesArray = '' cFuncName = getCName(srcFile[:srcFile.rfind('.c')]) generatedHeader = '#include "generators.h"\n' generatedHeader += '#include "stddef.h"\n\n' generatedHeader += '#include "defines.h"\n\n' generatedOutputHandler = '/********** Output handler **********/\n' # handler functions declaration if dstFileInfos.has_key('init'): generatedOutputHandler += 'static generator_init_func_t ' + dstFileInfos[ 'init'] + ';\n' if dstFileInfos.has_key('initGet'): generatedOutputHandler += 'static generator_initget_func_t ' + dstFileInfos[ 'initGet'] + ';\n' if dstFileInfos.has_key('doGet'): generatedOutputHandler += 'static generator_doget_func_t ' + dstFileInfos[ 'doGet'] + ';\n' if dstFileInfos.has_key('doPostOut'): generatedOutputHandler += 'static generator_dopost_out_func_t ' + dstFileInfos[ 'doPostOut'] + ';\n' if dstFileInfos.has_key('doPostIn'): generatedOutputHandler += 'static generator_dopost_in_func_t ' + dstFileInfos[ 'doPostIn'] + ';\n' if dstFileInfos.has_key('doPost'): generatedOutputHandler += 'static generator_doget_func_t ' + dstFileInfos[ 'doPost'] + ';\n' # other protocols than TCP handlers if dstFileInfos.has_key('doPacketIn'): generatedOutputHandler += '#ifndef DISABLE_GP_IP_HANDLER\n' generatedOutputHandler += 'static generator_dopacket_in_func_t ' + dstFileInfos[ 'doPacketIn'] + ';\n' if dstFileInfos.has_key('doPacketOut'): generatedOutputHandler += 'static generator_dopacket_out_func_t ' + dstFileInfos[ 'doPacketOut'] + ';\n' generatedOutputHandler += '#endif\n' # output_handler structure creation generatedOutputHandler += 'CONST_VAR(struct output_handler_t, ' + cFuncName + ') = {\n' # handler type if dstFileInfos.has_key('doPacketIn'): generatedOutputHandler += '\t.handler_type = type_general_ip_handler,\n' else: generatedOutputHandler += '\t.handler_type = type_generator,\n' generatedOutputHandler += '\t.handler_comet = %d,\n' % ( 1 if dstFileInfos['channel'] != '' else 0) generatedOutputHandler += '\t.handler_stream = %d,\n' % ( 1 if dstFileInfos['interaction'] == 'streaming' else 0) if dstFileInfos['persistence'] == 'persistent': etype = 'prop_persistent' elif dstFileInfos['persistence'] == 'idempotent': etype = 'prop_idempotent' elif dstFileInfos['persistence'] == 'volatile': etype = 'prop_volatile' if etype == None: etype = 'prop_persistent' # the handler is a dynamic resource (generator), we fill the function handlers fields generatedOutputHandler += '\t.handler_data = {\n' generatedOutputHandler += '\t\t.generator = {\n' generatedOutputHandler += '\t\t\t.prop = %s,\n' % etype # init if not dstFileInfos.has_key('init'): generatedOutputHandler += '\t\t\t.init = NULL,\n' else: generatedOutputHandler += '\t\t\t.init = ' + dstFileInfos[ 'init'] + ',\n' generatedOutputHandler += '\t\t\t.handlers = {\n' if dstFileInfos.has_key('initGet') or dstFileInfos.has_key( 'doGet') or dstFileInfos.has_key('doPost'): generatedOutputHandler += '\t\t\t\t.get = {\n' # initGet if dstFileInfos.has_key('initGet'): generatedOutputHandler += '\t\t\t\t.initget = ' + dstFileInfos[ 'initGet'] + ',\n' # doGet if dstFileInfos.has_key('doGet'): generatedOutputHandler += '\t\t\t\t\t.doget = ' + dstFileInfos[ 'doGet'] + ',\n' # doPost if dstFileInfos.has_key('doPost'): generatedOutputHandler += '\t\t\t\t.doget = ' + dstFileInfos[ 'doPost'] + ',\n' generatedOutputHandler += '\t\t\t\t},\n' #doPostIn/doPostOut if dstFileInfos.has_key('doPostIn'): generatedOutputHandler += '\t\t\t\t.post = {\n' generatedOutputHandler += '\t\t\t\t\t.dopostin = ' + dstFileInfos[ 'doPostIn'] + ',\n' if dstFileInfos.has_key('doPostOut'): generatedOutputHandler += '\t\t\t\t\t.dopostout = ' + dstFileInfos[ 'doPostOut'] + ',\n' generatedOutputHandler += '\t\t\t\t},\n' #doPacketIn, doPacketOut if dstFileInfos.has_key('doPacketIn'): generatedOutputHandler += '#ifndef DISABLE_GP_IP_HANDLER\n' generatedOutputHandler += '\t\t\t\t.gp_ip = {\n' generatedOutputHandler += '\t\t\t\t.dopacketin = ' + dstFileInfos[ 'doPacketIn'] + ',\n' if dstFileInfos.has_key('doPacketOut'): generatedOutputHandler += '\t\t\t\t.dopacketout = ' + dstFileInfos[ 'doPacketOut'] + ',\n' if dstFileInfos.has_key('protocol'): generatedOutputHandler += '\t\t\t\t.protocol = ' + dstFileInfos[ 'protocol'] + ',\n' generatedOutputHandler += '\t\t\t\t},\n' generatedOutputHandler += '#endif\n' generatedOutputHandler += '\t\t\t},\n' generatedOutputHandler += '\t\t},\n' generatedOutputHandler += '\t},\n' # generator arguments information are also written in the structure generatedOutputHandler += '#ifndef DISABLE_ARGS\n' generatedOutputHandler += '\t.handler_args = {\n' if len(dstFileInfos['argsList']) > 0: generatedOutputHandler += '\t\t.args_tree = args_tree,\n' generatedOutputHandler += '\t\t.args_index = args_index,\n' generatedOutputHandler += '\t\t.args_size = sizeof(struct args_t)\n' else: generatedOutputHandler += '\t\t.args_tree = NULL,\n' generatedOutputHandler += '\t\t.args_index = NULL,\n' generatedOutputHandler += '\t\t.args_size = 0\n' generatedOutputHandler += '\t},\n' generatedOutputHandler += '#endif\n' generatedOutputHandler += '#ifndef DISABLE_POST\n' generatedOutputHandler += '\t.handler_mimes = {\n' if (dstFileInfos.has_key('doPostIn') and len(dstFileInfos['contentTypeList']) > 0): generatedOutputHandler += '\t\t.mimes_index = mimes_index,\n' generatedOutputHandler += '\t\t.mimes_size = ' + str( len(dstFileInfos['contentTypeList'])) + ',\n' elif (dstFileInfos.has_key('doPost') and len(dstFileInfos['argsList']) > 0): generatedOutputHandler += '\t\t.mimes_index = mimes_index,\n' generatedOutputHandler += '\t\t.mimes_size = 1,\n' else: generatedOutputHandler += '\t\t.mimes_index = NULL,\n' generatedOutputHandler += '\t\t.mimes_size = 0,\n' generatedOutputHandler += '\t},\n' generatedOutputHandler += '#endif\n' generatedOutputHandler += '};\n' if (dstFileInfos.has_key('doPost') or dstFileInfos.has_key('doPostIn')): generatedMimesArray = '/********* Content-types array **********/\n' generatedMimesArray += 'static CONST_VAR(unsigned char, mimes_index[]) = {' countAttr = len(dstFileInfos['contentTypeList']) if (len(dstFileInfos['argsList']) > 0): if 'application/x-www-form-urlencoded' in mimesListPost: generatedMimesArray += str( mimesListPost.index( 'application/x-www-form-urlencoded')) else: exit( 'the mime type application/x-www-form-urlencoded don\'t exist in the mimeListPost file' ) elif len(dstFileInfos['contentTypeList']) > 0: for attrs in dstFileInfos['contentTypeList']: if attrs['type'] in mimesListPost: generatedMimesArray += str( mimesListPost.index(attrs['type'])) else: exit('the mime type ' + attrs['type'] + ' don\'t exist in the mimeListPost file') if countAttr > 1: generatedMimesArray += ',' countAttr -= 1 else: generatedMimesArray += 'NULL' generatedMimesArray += '};\n' # arguments c structure creation (this structure is directly used by the generator functions) generatedArgsStruc = '/********** Arguments structure **********/\n' generatedArgsStruc += 'struct args_t {\n' # for each argument, create a filed in the structure for attrs in dstFileInfos['argsList']: tmpString = '' if attrs['type'] == 'str': tmpString = 'char ' + attrs['name'] + '[' + attrs['size'] + ']' else: tmpString = attrs['type'] + '_t ' + attrs['name'] generatedArgsStruc += '\t' + tmpString + ';\n' generatedArgsStruc += '};\n' # arguments index creation generatedIndex = '/********** Arguments index **********/\n' generatedIndex += 'static CONST_VAR(struct arg_ref_t, args_index[]) = {\n' # for each argument, type and size informations # the offset (in bytes in the structure) is also provided for attrs in dstFileInfos['argsList']: tmpType = '' if attrs['type'] == 'str': tmpType = 'char[' + attrs['size'] + ']' else: tmpType = attrs['type'] + '_t' generatedIndex += '\t{arg_type: ' + argsTypesMap[attrs[ 'type']] + ', arg_size: sizeof(' + tmpType + '), arg_offset: offsetof(struct args_t,' + attrs[ 'name'] + ')},\n' generatedIndex += '};\n' # new c file creation cOut = open(dstFile, 'w') writeHeader(cOut, 1) cOut.write(generatedHeader) if (dstFileInfos.has_key('doPost') or dstFileInfos.has_key('doPostIn')): cOut.write('#ifndef DISABLE_POST\n') cOut.write(generatedMimesArray) cOut.write('\n') cOut.write('#endif\n') if len(dstFileInfos['argsList']) > 0: cOut.write('#ifndef DISABLE_ARGS\n') cOut.write(generatedArgsStruc) cOut.write('\n') cOut.write(generatedIndex) GenBlob.genBlobTree( cOut, map(lambda x: x['name'], dstFileInfos['argsList']), 'args_tree', True) cOut.write('#endif\n') cOut.write('\n') cOut.write(generatedOutputHandler) # the end of the file contains the original c file cOut.write('\n/* End of the enriched part */\n\n') cOut.write(fileData) cOut.close()
def generateDynamicResource(srcFile, dstFile, dstFileInfos): # extract the properties from the XML (if needed) fileData = extractPropsFromXml(srcFile, dstFileInfos) # if the c/h file does not conatin any XML, we simply copy it: this is not a generator if not dstFileInfos["hasXml"]: shutil.copyfile(srcFile, dstFile) # here, the c file is a generator. It will be enriched else: generatedMimesArray = "" cFuncName = getCName(srcFile[: srcFile.rfind(".c")]) generatedHeader = '#include "generators.h"\n' generatedHeader += '#include "stddef.h"\n\n' generatedHeader += '#include "defines.h"\n\n' generatedOutputHandler = "/********** Output handler **********/\n" # handler functions declaration if dstFileInfos.has_key("init"): generatedOutputHandler += "static generator_init_func_t " + dstFileInfos["init"] + ";\n" if dstFileInfos.has_key("initGet"): generatedOutputHandler += "static generator_initget_func_t " + dstFileInfos["initGet"] + ";\n" if dstFileInfos.has_key("doGet"): generatedOutputHandler += "static generator_doget_func_t " + dstFileInfos["doGet"] + ";\n" if dstFileInfos.has_key("doPostOut"): generatedOutputHandler += "static generator_dopost_out_func_t " + dstFileInfos["doPostOut"] + ";\n" if dstFileInfos.has_key("doPostIn"): generatedOutputHandler += "static generator_dopost_in_func_t " + dstFileInfos["doPostIn"] + ";\n" if dstFileInfos.has_key("doPost"): generatedOutputHandler += "static generator_doget_func_t " + dstFileInfos["doPost"] + ";\n" # output_handler structure creation generatedOutputHandler += "CONST_VAR(struct output_handler_t, " + cFuncName + ") = {\n" # handler type generatedOutputHandler += "\t.handler_type = type_generator,\n" generatedOutputHandler += "\t.handler_comet = %d,\n" % (1 if dstFileInfos["channel"] != "" else 0) generatedOutputHandler += "\t.handler_stream = %d,\n" % (1 if dstFileInfos["interaction"] == "streaming" else 0) if dstFileInfos["persistence"] == "persistent": etype = "prop_persistent" elif dstFileInfos["persistence"] == "idempotent": etype = "prop_idempotent" elif dstFileInfos["persistence"] == "volatile": etype = "prop_volatile" if etype == None: etype = "prop_persistent" # the handler is a dynamic resource (generator), we fill the function handlers fields generatedOutputHandler += "\t.handler_data = {\n" generatedOutputHandler += "\t\t.generator = {\n" generatedOutputHandler += "\t\t\t.prop = %s,\n" % etype # init if not dstFileInfos.has_key("init"): generatedOutputHandler += "\t\t\t.init = NULL,\n" else: generatedOutputHandler += "\t\t\t.init = " + dstFileInfos["init"] + ",\n" # initGet if dstFileInfos.has_key("initGet"): generatedOutputHandler += "\t\t\t.initget = " + dstFileInfos["initGet"] + ",\n" # doGet or doPost generatedOutputHandler += "\t\t\t.handlers = {\n" if dstFileInfos.has_key("doGet"): generatedOutputHandler += "\t\t\t\t.doget = " + dstFileInfos["doGet"] + ",\n" if dstFileInfos.has_key("doPost"): generatedOutputHandler += "\t\t\t\t.doget = " + dstFileInfos["doPost"] + ",\n" # doPost if dstFileInfos.has_key("doPostIn"): generatedOutputHandler += "\t\t\t\t.post = {\n" generatedOutputHandler += "\t\t\t\t\t.dopostin = " + dstFileInfos["doPostIn"] + ",\n" if dstFileInfos.has_key("doPostOut"): generatedOutputHandler += "\t\t\t\t\t.dopostout = " + dstFileInfos["doPostOut"] + ",\n" generatedOutputHandler += "\t\t\t\t},\n" generatedOutputHandler += "\t\t\t},\n" generatedOutputHandler += "\t\t},\n" generatedOutputHandler += "\t},\n" # generator arguments information are also written in the structure generatedOutputHandler += "#ifndef DISABLE_ARGS\n" generatedOutputHandler += "\t.handler_args = {\n" if len(dstFileInfos["argsList"]) > 0: generatedOutputHandler += "\t\t.args_tree = args_tree,\n" generatedOutputHandler += "\t\t.args_index = args_index,\n" generatedOutputHandler += "\t\t.args_size = sizeof(struct args_t)\n" else: generatedOutputHandler += "\t\t.args_tree = NULL,\n" generatedOutputHandler += "\t\t.args_index = NULL,\n" generatedOutputHandler += "\t\t.args_size = 0\n" generatedOutputHandler += "\t},\n" generatedOutputHandler += "#endif\n" generatedOutputHandler += "#ifndef DISABLE_POST\n" generatedOutputHandler += "\t.handler_mimes = {\n" if dstFileInfos.has_key("doPostIn") and len(dstFileInfos["contentTypeList"]) > 0: generatedOutputHandler += "\t\t.mimes_index = mimes_index,\n" generatedOutputHandler += "\t\t.mimes_size = " + str(len(dstFileInfos["contentTypeList"])) + ",\n" elif dstFileInfos.has_key("doPost") and len(dstFileInfos["argsList"]) > 0: generatedOutputHandler += "\t\t.mimes_index = mimes_index,\n" generatedOutputHandler += "\t\t.mimes_size = 1,\n" else: generatedOutputHandler += "\t\t.mimes_index = NULL,\n" generatedOutputHandler += "\t\t.mimes_size = 0,\n" generatedOutputHandler += "\t},\n" generatedOutputHandler += "#endif\n" generatedOutputHandler += "};\n" if dstFileInfos.has_key("doPost") or dstFileInfos.has_key("doPostIn"): generatedMimesArray = "/********* Content-types array **********/\n" generatedMimesArray += "static CONST_VAR(unsigned char, mimes_index[]) = {" countAttr = len(dstFileInfos["contentTypeList"]) if len(dstFileInfos["argsList"]) > 0: if "application/x-www-form-urlencoded" in mimesListPost: generatedMimesArray += str(mimesListPost.index("application/x-www-form-urlencoded")) else: exit("the mime type application/x-www-form-urlencoded don't exist in the mimeListPost file") elif len(dstFileInfos["contentTypeList"]) > 0: for attrs in dstFileInfos["contentTypeList"]: if attrs["type"] in mimesListPost: generatedMimesArray += str(mimesListPost.index(attrs["type"])) else: exit("the mime type " + attrs["type"] + " don't exist in the mimeListPost file") if countAttr > 1: generatedMimesArray += "," countAttr -= 1 else: generatedMimesArray += "NULL" generatedMimesArray += "};\n" # arguments c structure creation (this structure is directly used by the generator functions) generatedArgsStruc = "/********** Arguments structure **********/\n" generatedArgsStruc += "struct args_t {\n" # for each argument, create a filed in the structure for attrs in dstFileInfos["argsList"]: tmpString = "" if attrs["type"] == "str": tmpString = "char " + attrs["name"] + "[" + attrs["size"] + "]" else: tmpString = attrs["type"] + "_t " + attrs["name"] generatedArgsStruc += "\t" + tmpString + ";\n" generatedArgsStruc += "};\n" # arguments index creation generatedIndex = "/********** Arguments index **********/\n" generatedIndex += "static CONST_VAR(struct arg_ref_t, args_index[]) = {\n" # for each argument, type and size informations # the offset (in bytes in the structure) is also provided for attrs in dstFileInfos["argsList"]: tmpType = "" if attrs["type"] == "str": tmpType = "char[" + attrs["size"] + "]" else: tmpType = attrs["type"] + "_t" generatedIndex += ( "\t{arg_type: " + argsTypesMap[attrs["type"]] + ", arg_size: sizeof(" + tmpType + "), arg_offset: offsetof(struct args_t," + attrs["name"] + ")},\n" ) generatedIndex += "};\n" # new c file creation cOut = open(dstFile, "w") writeHeader(cOut, 1) cOut.write(generatedHeader) if dstFileInfos.has_key("doPost") or dstFileInfos.has_key("doPostIn"): cOut.write("#ifndef DISABLE_POST\n") cOut.write(generatedMimesArray) cOut.write("\n") cOut.write("#endif\n") if len(dstFileInfos["argsList"]) > 0: cOut.write("#ifndef DISABLE_ARGS\n") cOut.write(generatedArgsStruc) cOut.write("\n") cOut.write(generatedIndex) GenBlob.genBlobTree(cOut, map(lambda x: x["name"], dstFileInfos["argsList"]), "args_tree", True) cOut.write("#endif\n") cOut.write("\n") cOut.write(generatedOutputHandler) # the end of the file contains the original c file cOut.write("\n/* End of the enriched part */\n\n") cOut.write(fileData) cOut.close()