#! /usr/bin/env python ###################################################################### # create_printf.py # # Reads a filename.fields file as input. # The filename.fields file will specify the fields with their datatypes, # and comments for each field. Using this information it will # generate 3 files. # # filenameOutput.h # This script will generate a printf statement for all of the fields. # Above the printf statement it will have doxygen style comments # for each field. # This script will ignore anything in the fields file other than # a field:: line. # # filenameVarDecl.h # A list of extern variable declarations that the printf statement in # filenameOutput.h will need. These guys will be global variables. # # filenameVarInit.h # A list of variable initializations that the printf statement in # filenameOutput.h will need. The character arrays just point at # NULL. The integers and floats are set to 0. # # # usage: create_printf.py filename.fields # ##### Example .fields file # # # specify database info # database::mymc # hostname::localhost # username::root # password::something # portnum::3010 # unix_socket_file::/home/mstrout/Temp/mysqld.3010 # # # specify new table name # table::practice # # # field names and datatypes (float, int, or string) # # 1 field per line, order matters # field::version(string) ! a comment about this field # field::datadir(string) ! a comment about this field # field::maxiter(int) ! a comment on all fields # field::residual(float) ! example of a float # field::cycles(ulong) ! example of an unsigned long # ###################################################################### #import MySQLdb import sys import string ##### read in .fields file if len(sys.argv) > 1: fieldsfilename = sys.argv[1] else: print "usage: create_printf filename.fields\n" sys.exit(0) fieldsfile = open(fieldsfilename) filelines = fieldsfile.readlines() # create the various output files vardeclfilename = string.split(fieldsfilename,'.')[0] + "VarDecl.h" varinitfilename = string.split(fieldsfilename,'.')[0] + "VarInit.h" printffilename = string.split(fieldsfilename,'.')[0] + "Output.h" vardeclfile = open(vardeclfilename,"w") varinitfile = open(varinitfilename,"w") printffile = open(printffilename,"w") ##### remove comment lines and blank lines def notcomment(line): if (line[0] == '#') or (string.strip(line) == ''): return 0 else: return 1 filelines = filter(notcomment,filelines) ##### field keyword # for each field add some text to the comment string, # the vardecl string, the varinit string, # the format string, and the varlist string. # start with an empty string and build up list of field defs comment = \ " This file was automatically generated by calling " + \ "\n ../create_printf.py %s\n\n" % fieldsfilename comment = comment + "