Package PyFoam :: Package Applications :: Module FromTemplate
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Applications.FromTemplate

 1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/Applications/FromTemplate.py 7660 2012-01-07T16:44:40.128256Z bgschaid  $  
 2  """ 
 3  Application class that implements pyFoamFromTemplate 
 4  """ 
 5   
 6  import sys 
 7   
 8  from PyFoamApplication import PyFoamApplication 
 9   
10  from PyFoam.Basics.TemplateFile import TemplateFile 
11   
12 -class FromTemplate(PyFoamApplication):
13 - def __init__(self,args=None):
14 description="""\ 15 Generates a file from a template file. Usually the name of the 16 template file is the name of the file with the extension '.template' 17 (unless specified otherwise). The file is generated by replacing 18 everything in the template file that is enclosed by $ $ with 19 calculated expression. values are given in a Python-dictionary. Lines 20 in the template file that start with $$ are used as definitons for 21 intermediate expressions 22 """ 23 24 PyFoamApplication.__init__(self, 25 args=args, 26 description=description, 27 usage="%prog [options] <file> <vals>", 28 nr=2, 29 changeVersion=False, 30 interspersed=True)
31
32 - def addOptions(self):
33 self.parser.add_option("--template-file", 34 action="store", 35 default=None, 36 dest="template", 37 help="Name 0f the template file") 38 39 self.parser.add_option("--test", 40 action="store_true", 41 dest="test", 42 default=False, 43 help="Doesn't write to the file, but outputs the result on stdout")
44 45
46 - def run(self):
47 fName=self.parser.getArgs()[0] 48 vals=eval(self.parser.getArgs()[1]) 49 50 if self.opts.template==None: 51 template=fName+".template" 52 else: 53 template=self.opts.template 54 55 t=TemplateFile(name=template) 56 57 if self.opts.test: 58 print t.getString(vals) 59 else: 60 t.writeToFile(fName,vals)
61