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