1
2 """
3 Application class that implements pyFoamUtilityRunner
4 """
5
6 from .PyFoamApplication import PyFoamApplication
7
8 from PyFoam.Execution.UtilityRunner import UtilityRunner
9
10 from PyFoam.ThirdParty.six import print_
11
12 import sys
13 from os import path
14
17 description="""\
18 Runs a OpenFoam Utility and analyzes the output. Needs a regular
19 expression to look for. The next 3 arguments are the usual OpenFoam
20 argumens (<solver> <directory> <case>) and passes them on (plus
21 additional arguments). Output is sent to stdout and a logfile inside
22 the case directory (PyFoamUtility.logfile). The Directory
23 PyFoamUtility.analyzed contains a file test with the information of
24 the regexp (the pattern groups).
25 """
26
27 PyFoamApplication.__init__(self,
28 exactNr=False,
29 args=args,
30 description=description)
31
33 self.parser.add_option("-r",
34 "--regexp",
35 action="append",
36 dest="regexp",
37 help="The regular expression to look for. With more than one the expresions get appended")
38
39 self.parser.add_option("-n",
40 "--name",
41 type="string",
42 dest="name",
43 default="test",
44 help="The name for the resulting file")
45
46 self.parser.add_option("--echo",
47 action="store_true",
48 dest="echo",
49 default=False,
50 help="Echo the result file after the run")
51
52 self.parser.add_option("--silent",
53 action="store_true",
54 dest="silent",
55 default=False,
56 help="Don't print the output of the utility to the console")
57
59 if self.opts.regexp==None:
60 self.parser.error("Regular expression needed")
61
62 cName=self.parser.casePath()
63
64 run=UtilityRunner(argv=self.parser.getArgs(),
65 silent=self.opts.silent,
66 server=True)
67
68 for i,r in enumerate(self.opts.regexp):
69 name=self.opts.name
70 if len(self.opts.regexp)>1:
71 name="%s_%d" % (name,i)
72 run.add(name,r)
73
74 self.addToCaseLog(cName,"Starting")
75
76 run.start()
77
78 self.addToCaseLog(cName,"Ending")
79
80 allData=run.data
81
82 for i,r in enumerate(self.opts.regexp):
83 name=self.opts.name
84 if len(self.opts.regexp)>1:
85 name="%s_%d" % (name,i)
86
87 fn=path.join(run.getDirname(),name)
88
89 data=run.analyzer.getData(name)
90 allData["analyzed"][name]=data
91
92 if data==None:
93 print_(sys.argv[0]+": No data found for expression",r)
94 else:
95 if self.opts.echo:
96 fh=open(fn)
97 print_(fh.read())
98 fh.close()
99 else:
100 print_(sys.argv[0]+": Output written to file "+fn)
101
102 self.setData(allData)
103
104
105