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

Source Code for Module PyFoam.Applications.CaseBuilder

  1  """ 
  2  Application-class that implements pyFoamCaseBuilder.py 
  3  """ 
  4  from optparse import OptionGroup 
  5  from os import path 
  6  import os 
  7  import shutil 
  8   
  9  from PyFoamApplication import PyFoamApplication 
 10  from CaseBuilderBackend import CaseBuilderFile 
 11  from CommonCaseBuilder import CommonCaseBuilder 
 12   
 13  from PyFoam.Error import error 
 14   
15 -class CaseBuilder(PyFoamApplication, 16 CommonCaseBuilder):
17 - def __init__(self,args=None):
18 description=""" 19 Gets a XML-file that describes how to build a case from a case 20 template and some parameters 21 """ 22 PyFoamApplication.__init__(self, 23 args=args, 24 description=description, 25 usage="%prog <DescriptionFile>", 26 interspersed=True, 27 nr=0, 28 exactNr=False)
29
30 - def addOptions(self):
31 info=OptionGroup(self.parser, 32 "Information", 33 "Information about the case") 34 self.parser.add_option_group(info) 35 36 info.add_option("--short-description", 37 action="store_true", 38 dest="short", 39 default=False, 40 help="Print a short description of the case and exit") 41 42 info.add_option("--arguments", 43 action="store_true", 44 dest="args", 45 default=False, 46 help="Describes the additional arguments") 47 48 info.add_option("--help-text", 49 action="store_true", 50 dest="help", 51 default=False, 52 help="Prints the help text in the description file") 53 54 info.add_option("--boundaries", 55 action="store_true", 56 dest="bounds", 57 default=False, 58 help="Describes the boundaries") 59 60 info.add_option("--long-description", 61 action="store_true", 62 dest="long", 63 default=False, 64 help="Print a long description of the case and exit") 65 66 CommonCaseBuilder.addOptions(self) 67 68 how=OptionGroup(self.parser, 69 "How", 70 "How the case should be built") 71 self.parser.add_option_group(how) 72 73 how.add_option("--force", 74 action="store_true", 75 dest="force", 76 default=False, 77 help="Remove the case-directory if it exists")
78
79 - def printTitle(self,title):
80 print 81 print title 82 print "="*len(title)
83
84 - def run(self):
85 if self.pathInfo(): 86 return 87 88 if len(self.parser.getArgs())<1: 89 error("No description file given") 90 91 fName=self.searchDescriptionFile(self.parser.getArgs()[0]) 92 93 desc=CaseBuilderFile(fName) 94 95 print "Read case description",desc.name() 96 97 stopIt=False 98 99 if self.opts.long: 100 self.opts.short=True 101 self.opts.args=True 102 self.opts.bounds=True 103 self.opts.help=True 104 105 if self.opts.short: 106 print 107 print "Description: ",desc.description() 108 print "Template: ",desc.templatePath() 109 print "Initial Condition:",desc.initialDir() 110 stopIt=True 111 112 if self.opts.help: 113 self.printTitle("Help") 114 print desc.helpText() 115 stopIt=True 116 117 if self.opts.args: 118 args=desc.arguments() 119 mLen=apply(max,map(len,args)) 120 aDesc=desc.argumentDescriptions() 121 format="%%%ds : %%s" % mLen 122 123 self.printTitle("Arguments") 124 for a in args: 125 print format % (a,aDesc[a]) 126 stopIt=True 127 128 if self.opts.bounds: 129 bounds=desc.boundaries() 130 mLen=apply(max,map(len,bounds)) 131 bDesc=desc.boundaryDescriptions() 132 bPat=desc.boundaryPatternDict() 133 format="%%%ds : %%s \n\tPattern: '%%s'" % mLen 134 135 self.printTitle("Boundaries") 136 for i,a in enumerate(bounds): 137 print format % (a,bDesc[a],bPat[a]) 138 stopIt=True 139 140 if stopIt: 141 print 142 print "Not doing anything" 143 return 144 145 args=desc.arguments() 146 147 if len(self.parser.getArgs())<2: 148 error("Missing a casename:",self.buildUsage(args)) 149 150 cName=self.parser.getArgs()[1] 151 if len(self.parser.getArgs())!=len(args)+2: 152 error("Wrong number of arguments:",self.buildUsage(args)) 153 154 aDict={} 155 for i,a in enumerate(args): 156 tmp=self.parser.getArgs()[2+i] 157 if (tmp[0]=='"' or tmp[0]=="'") and tmp[0]==tmp[-1]: 158 tmp=tmp[1:-1] 159 aDict[a]=tmp 160 161 if path.exists(cName): 162 if self.opts.force: 163 shutil.rmtree(cName) 164 else: 165 error("Case directory",cName,"already exists") 166 167 print "Building case",cName 168 169 msg=desc.verifyArguments(aDict) 170 if msg: 171 error("Error verifying arguments:",msg) 172 173 desc.buildCase(cName,aDict)
174
175 - def buildUsage(self,args):
176 usage="<casename>" 177 for a in args: 178 usage+=" <"+a+">" 179 return usage
180