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