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

Source Code for Module PyFoam.Applications.PyFoamApplication

 1  #  ICE Revision: $Id: PyFoamApplication.py 9336 2008-09-08 13:33:27Z bgschaid $  
 2  """Base class for pyFoam-applications 
 3   
 4  Classes can also be called with a command-line string""" 
 5   
 6  from PyFoam.Basics.FoamOptionParser import FoamOptionParser 
 7  from PyFoam.Error import error,warning 
 8  from PyFoam.FoamInformation import oldAppConvention as oldApp 
 9   
10  import sys 
11  from os import path 
12   
13 -class PyFoamApplication(object):
14 - def __init__(self, 15 args=None, 16 description=None, 17 usage=None, 18 interspersed=False, 19 nr=None, 20 changeVersion=True, 21 exactNr=True):
22 """ 23 @param description: description of the command 24 @param usage: Usage 25 @param interspersed: Is the command line allowed to be interspersed (options after the arguments) 26 @param args: Command line arguments when using the Application as a 'class' from a script 27 @param nr: Number of required arguments 28 @param chaneVersion: May this application change the version of OF used? 29 @param exactNr: Must not have more than the required number of arguments 30 """ 31 self.parser=FoamOptionParser(args=args, 32 description=description, 33 usage=usage, 34 interspersed=interspersed) 35 if changeVersion: 36 self.parser.add_option("--foamVersion", 37 dest="foamVersion", 38 default=None, 39 help="Change the OpenFOAM-version that is to be used") 40 41 self.parser.add_option("--psyco-accelerated", 42 dest="psyco", 43 default=False, 44 action="store_true", 45 help="Accelerate the script using the psyco-library (EXPERIMENTAL and requires a separatly installed psyco)") 46 47 self.addOptions() 48 self.parser.parse(nr=nr,exactNr=exactNr) 49 self.opts=self.parser.getOptions() 50 51 if self.opts.psyco: 52 try: 53 import psco 54 psyco.full() 55 except ImportError: 56 warning("No psyco installed. Continuing without acceleration") 57 58 self.run()
59
60 - def addOptions(self):
61 """ 62 Add options to the parser 63 """ 64 pass
65
66 - def run(self):
67 """ 68 Run the real application 69 """ 70 error("Not a valid application")
71 72
73 - def error(self,*args):
74 """ 75 Prints an error message and exits 76 @param args: Arguments that are to be printed 77 """ 78 print "Error in",sys.argv[0],":", 79 for a in args: 80 print a, 81 print 82 sys.exit(-1)
83
84 - def checkCase(self,name):
85 """ 86 Check whether this is a valid OpenFOAM-case 87 @param name: the directory-bame that is supposed to be the case 88 """ 89 if not path.exists(name): 90 self.error("Case",name,"does not exist") 91 if not path.isdir(name): 92 self.error("Case",name,"is not a directory") 93 if not path.exists(path.join(name,"system")): 94 self.error("Case",name,"does not have a 'system' directory") 95 if not path.exists(path.join(name,"constant")): 96 self.error("Case",name,"does not have a 'constant' directory")
97