1
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
14 description="""
15 Generates a file from a template file. Usually the name of the template file
16 is the name of the file with the extension '.template' (unless specified
17 otherwise). The file is generated by replacing everything in the template
18 file that is enclosed by $ $ with calculated expression. values are given in
19 a Python-dictionary. Lines in the template file that start with $$ are used as
20 definitons for intermediate expressions
21 """
22
23 PyFoamApplication.__init__(self,
24 args=args,
25 description=description,
26 usage="%prog [options] <file> <vals>",
27 nr=2,
28 changeVersion=False,
29 interspersed=True)
30
32 self.parser.add_option("--template-file",
33 action="store",
34 default=None,
35 dest="template",
36 help="Name 0f the template file")
37
38 self.parser.add_option("--test",
39 action="store_true",
40 dest="test",
41 default=False,
42 help="Doesn't write to the file, but outputs the result on stdout")
43
44
46 fName=self.parser.getArgs()[0]
47 vals=eval(self.parser.getArgs()[1])
48
49 if self.opts.template==None:
50 template=fName+".template"
51 else:
52 template=self.opts.template
53
54 t=TemplateFile(name=template)
55
56 if self.opts.test:
57 print t.getString(vals)
58 else:
59 t.writeToFile(fName,vals)
60