Package PyFoam :: Package Basics :: Module FoamOptionParser
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.FoamOptionParser

  1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/Basics/FoamOptionParser.py 7815 2012-01-30T19:52:34.081422Z bgschaid  $  
  2  """Parse options for the PyFoam-Scripts""" 
  3   
  4  from optparse import OptionParser,TitledHelpFormatter 
  5  from PyFoam import versionString 
  6   
  7  from PyFoam.FoamInformation import changeFoamVersion 
  8  from PyFoam.FoamInformation import oldAppConvention as oldApp 
  9   
 10  from PyFoam.Error import error,warning 
 11   
 12  from os import path,environ 
 13  from copy import deepcopy 
 14   
15 -class FoamOptionParser(OptionParser):
16 """Wrapper to the usual OptionParser to honor the conventions of OpenFOAM-utilities 17 18 Options that are not used by the script are passed to the OpenFOAM-application""" 19
20 - def __init__(self, 21 args=None, 22 usage=None, 23 version=None, 24 description=None, 25 interspersed=False):
26 """ 27 @param usage: usage string. If missing a default is used 28 @param version: if missing the PyFoam-version is used 29 @param description: description of the utility 30 @param interspersed: needs to be false if options should be passed to an OpenFOAM-utility 31 @param args: Command line arguments. If unset sys.argv[1:] is used. 32 Can be a string: it will be splitted then unsing the spaces (very primitive), or a list of strings (prefered) 33 """ 34 if usage==None: 35 if oldApp(): 36 usage="%prog [options] <foamApplication> <caseDir> <caseName> [foamOptions]" 37 else: 38 usage="%prog [options] <foamApplication> [foamOptions]" 39 40 if version==None: 41 version="%prog "+versionString() 42 43 if args==None: 44 self.argLine=None 45 elif type(args)==str: 46 self.argLine=args.split() 47 else: 48 self.argLine=map(str,args) 49 50 OptionParser.__init__(self, 51 usage=usage, 52 # prog=self.__type__.__name__, 53 version=version, 54 description=description, 55 formatter=TitledHelpFormatter()) 56 57 if interspersed: 58 self.enable_interspersed_args() 59 else: 60 self.disable_interspersed_args() 61 62 self.options=None 63 self.args=None 64 65 self.__foamVersionChanged=False 66 self.__oldEnvironment=None
67
68 - def restoreEnvironment(self):
69 """Restore the environment to its old glory... if it was changed""" 70 if self.__foamVersionChanged: 71 # print "Restoring the environment" 72 environ.update(self.__oldEnvironment)
73
74 - def parse(self,nr=None,exactNr=True):
75 """ 76 parse the options 77 @param nr: minimum number of arguments that are to be passed to the application 78 3 is default for pre-1.5 versions of OpenFOAM 79 """ 80 (self.options,self.args)=self.parse_args(args=self.argLine) 81 82 if "foamVersion" in dir(self.options): 83 if self.options.foamVersion!=None: 84 if self.options.force32 and self.options.force64: 85 error("A version can't be 32 and 64 bit at the same time") 86 87 self.__foamVersionChanged=True 88 self.__oldEnvironment=deepcopy(environ) 89 90 changeFoamVersion(self.options.foamVersion, 91 force64=self.options.force64, 92 force32=self.options.force32, 93 compileOption=self.options.compileOption) 94 elif self.options.force32 or self.options.force64: 95 warning("Foring version to be 32 or 64 bit, but no version chosen. Doing nothing") 96 elif self.options.compileOption: 97 warning("No OpenFOAM-version chosen. Can't set compile-option to",self.options.compileOption) 98 99 if nr==None: 100 if oldApp(): 101 nr=3 102 else: 103 nr=1 104 105 if len(self.args)<nr: 106 self.error("Too few arguments (%d needed, %d given)" %(nr,len(self.args))) 107 108 maxNr=nr 109 if not oldApp(): 110 if "-case" in self.args: 111 maxNr+=2 112 113 if exactNr and len(self.args)>maxNr: 114 self.error("Too many arguments (%d needed, %d given)" %(nr,len(self.args))) 115 116 tmp=self.args 117 self.args=[] 118 for a in tmp: 119 if a.find(" ")>=0 or a.find("(")>=0: 120 a="\""+a+"\"" 121 self.args.append(a)
122
123 - def getArgs(self):
124 """Return the arguments left after parsing""" 125 if self.args!=None: 126 return self.args 127 else: 128 return []
129
130 - def getApplication(self):
131 """Return the OpenFOAM-Application to be run""" 132 if self.args!=None: 133 return self.args[0] 134 else: 135 return None
136
137 - def getOptions(self):
138 """Return the options""" 139 if self.options==None: 140 self.error("options have not been parsed yet") 141 142 return self.options
143
144 - def casePath(self):
145 """Returns the path to the case (if applicable)""" 146 if oldApp(): 147 return path.join(self.getArgs()[1],self.getArgs()[2]) 148 else: 149 if "-case" in self.getArgs(): 150 return path.normpath(self.getArgs()[self.getArgs().index("-case")+1]) 151 else: 152 return path.abspath(path.curdir)
153