#!/usr/local/bin/python # # generates a set of files from a given template file # usage: decl-gen decl-tmpl.f90 # # preamble keywords are # name: the templated file name to be generated # decl: a list of variable types that varies across files (this should be a # python expression) # init: dictionary containing values for parameter initialization # tmpl: marks the end of the preamble and beginning of the template part # # The program performs search and replace on keywords of the form %keyword%. # %rn% a number unique to each file. Used as random number seed. # %decl% the loop index (from decl: above) import re from sys import * import random oname = re.compile("name:\s+") odecl = re.compile("decl:\s+") oinit = re.compile("init:\s+") otmpl = re.compile("tmpl:") try: input = open(argv[1],'r') except: print "open error" while 1: line = input.readline() if not line: break else: print "line:",line if otmpl.search(line)!=None: print "tmpl matched" break mo=oname.search(line) if mo!=None: name = line[mo.end():-1] print name mo=odecl.search(line) if mo!=None: decl = line[mo.end():-1] mo=oinit.search(line) if mo!=None: init = line[mo.end():-1] # read template part of file into a buffer i=0 buff=[] while 1: line = input.readline() if not line: break else: buff.append(line) i=i+1 # open a file and substitute as appropriate #decl = "%s"%1 #print "decl:"+decl init_list = eval(init) #rn = 10 rn = random.randint(1,10000) for iid in eval(decl): rn = rn +1 sdecl = "%s"%iid fname = re.sub("%decl%",iid,name) print "fname:"+fname try: output = open(fname,'w') except: print "write open error" for line in buff: new = re.sub("%rn%",`rn`,line) new1 = re.sub("%init%",init_list[iid],new) new2 = re.sub("%decl%",iid,new1) output.write(new2);