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

Source Code for Module PyFoam.Applications.PyFoamApplication

  1  #  ICE Revision: $Id: PyFoamApplication.py 9442 2008-09-23 09:11:07Z bgschaid $  
  2  """Base class for pyFoam-applications 
  3   
  4  Classes can also be called with a command-line string""" 
  5   
  6  from optparse import OptionGroup 
  7  from PyFoam.Basics.FoamOptionParser import FoamOptionParser 
  8  from PyFoam.Error import error,warning 
  9  from PyFoam.FoamInformation import oldAppConvention as oldApp 
 10   
 11  import sys 
 12  from os import path 
 13   
14 -class PyFoamApplication(object):
15 - def __init__(self, 16 args=None, 17 description=None, 18 usage=None, 19 interspersed=False, 20 nr=None, 21 changeVersion=True, 22 exactNr=True):
23 """ 24 @param description: description of the command 25 @param usage: Usage 26 @param interspersed: Is the command line allowed to be interspersed (options after the arguments) 27 @param args: Command line arguments when using the Application as a 'class' from a script 28 @param nr: Number of required arguments 29 @param chaneVersion: May this application change the version of OF used? 30 @param exactNr: Must not have more than the required number of arguments 31 """ 32 self.parser=FoamOptionParser(args=args, 33 description=description, 34 usage=usage, 35 interspersed=interspersed) 36 self.generalOpts=None 37 38 grp=OptionGroup(self.parser, 39 "Default", 40 "Options common to all PyFoam-applications") 41 42 if changeVersion: 43 grp.add_option("--foamVersion", 44 dest="foamVersion", 45 default=None, 46 help="Change the OpenFOAM-version that is to be used") 47 48 grp.add_option("--psyco-accelerated", 49 dest="psyco", 50 default=False, 51 action="store_true", 52 help="Accelerate the script using the psyco-library (EXPERIMENTAL and requires a separatly installed psyco)") 53 grp.add_option("--profile-python", 54 dest="profilePython", 55 default=False, 56 action="store_true", 57 help="Profile the python-script (not the OpenFOAM-program) - mostly of use for developers") 58 grp.add_option("--profile-hotshot", 59 dest="profileHotshot", 60 default=False, 61 action="store_true", 62 help="Profile the python-script using the hotshot-library (not the OpenFOAM-program) - mostly of use for developers - EXPERIMENTAL") 63 64 self.parser.add_option_group(grp) 65 66 self.addOptions() 67 self.parser.parse(nr=nr,exactNr=exactNr) 68 self.opts=self.parser.getOptions() 69 70 if self.opts.psyco: 71 try: 72 import psco 73 psyco.full() 74 except ImportError: 75 warning("No psyco installed. Continuing without acceleration") 76 77 if self.opts.profilePython or self.opts.profileHotshot: 78 if self.opts.profilePython and self.opts.profileHotshot: 79 self.error("Profiling with hotshot and regular profiling are mutual exclusive") 80 print "Running profiled" 81 if self.opts.profilePython: 82 import profile 83 else: 84 import hotshot 85 profileData=path.basename(sys.argv[0])+".profile" 86 if self.opts.profilePython: 87 profile.runctx('self.run()',None,{'self':self},profileData) 88 print "Reading python profile" 89 import pstats 90 stats=pstats.Stats(profileData) 91 else: 92 profileData+=".hotshot" 93 prof=hotshot.Profile(profileData) 94 prof.runctx('self.run()',{},{'self':self}) 95 print "Writing and reading hotshot profile" 96 prof.close() 97 import hotshot.stats 98 stats=hotshot.stats.load(profileData) 99 stats.strip_dirs() 100 stats.sort_stats('time','calls') 101 stats.print_stats(20) 102 else: 103 self.run()
104
105 - def ensureGeneralOptions(self):
106 if self.generalOpts==None: 107 self.generalOpts=OptionGroup(self.parser, 108 "General", 109 "General options for the control of OpenFOAM-runs") 110 self.parser.add_option_group(self.generalOpts)
111
112 - def addOptions(self):
113 """ 114 Add options to the parser 115 """ 116 pass
117
118 - def run(self):
119 """ 120 Run the real application 121 """ 122 error("Not a valid application")
123 124
125 - def error(self,*args):
126 """ 127 Prints an error message and exits 128 @param args: Arguments that are to be printed 129 """ 130 print "Error in",sys.argv[0],":", 131 for a in args: 132 print a, 133 print 134 sys.exit(-1)
135
136 - def checkCase(self,name):
137 """ 138 Check whether this is a valid OpenFOAM-case 139 @param name: the directory-bame that is supposed to be the case 140 """ 141 if not path.exists(name): 142 self.error("Case",name,"does not exist") 143 if not path.isdir(name): 144 self.error("Case",name,"is not a directory") 145 if not path.exists(path.join(name,"system")): 146 self.error("Case",name,"does not have a 'system' directory") 147 if not path.exists(path.join(name,"constant")): 148 self.error("Case",name,"does not have a 'constant' directory")
149