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

Source Code for Module PyFoam.Applications.UtilityRunnerApp

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