1
2 """Parameter file is read into memory and modified there"""
3
4 from FileBasis import FileBasisBackup
5 from PyFoam.Basics.PlyParser import PlyParser
6 from PyFoam.Basics.FoamFileGenerator import FoamFileGenerator
7
8 from PyFoam.Basics.DataStructures import Vector,Field,Dimension,DictProxy
9
11 """ Parameterfile whose complete representation is read into
12 memory, can be manipulated and afterwards written to disk"""
13
14 - def __init__(self,name,backup=False,debug=False,boundaryDict=False):
15 """@param name: The name of the parameter file
16 @param backup: create a backup-copy of the file
17 @param boundaryDict: the file to parse is a boundary file"""
18
19 FileBasisBackup.__init__(self,name,backup=backup)
20 self.debug=debug
21 self.boundaryDict=boundaryDict
22
23 self.header=None
24 self.content=None
25
26 self.readFile()
27
34
36 return self.content[key]
37
39 self.content[key]=value
40
42 """Generates a string from the contents in memory
43 Used to be called makeString"""
44
45 string="// File generated by PyFoam - sorry for the ugliness\n\n"
46
47 generator=FoamFileGenerator(self.content,header=self.header)
48 string+=str(generator)
49
50 return string
51
53 """Class that parses a string that contains the contents of an
54 OpenFOAM-file and builds a nested structure of directories and
55 lists from it"""
56
57 - def __init__(self,content,debug=False,noHeader=False,boundaryDict=False):
58 """@param content: the string to be parsed
59 @param debug: output debug information during parsing
60 @param noHeader: switch that turns off the parsing of the header"""
61
62 self.data=None
63 self.header=None
64 self.debug=debug
65
66 if noHeader:
67 self.start='noHeader'
68
69 if boundaryDict:
70 self.start='boundaryDict'
71
72 PlyParser.__init__(self,debug=debug)
73
74
75
76
77 self.header,self.data=self.parse(content)
78
80 return self.data[key]
81
83 self.data[key]=value
84
86 """ Get the data structure"""
87 return self.data
88
90 """ Get the OpenFOAM-header"""
91 return self.header
92
93 - def printContext(self,c,ind):
94 """Prints the context of the current index"""
95 print "------"
96 print c[max(0,ind-100):max(0,ind-1)]
97 print "------"
98 print ">",c[ind-1],"<"
99 print "------"
100 print c[min(len(c),ind):min(len(c),ind+100)]
101 print "------"
102
104 """Prints the error message of the parser and exit"""
105 print "PARSER ERROR:",text
106 print "On index",ind
107 self.printContext(c,ind)
108 raise ParseError
109
110 tokens = (
111 'NAME',
112 'ICONST',
113 'FCONST',
114 'SCONST',
115 'FOAMFILE',
116 'UNIFORM',
117 'NONUNIFORM',
118 )
119
120 reserved = {
121 'FoamFile' : 'FOAMFILE',
122 'uniform' : 'UNIFORM',
123 'nonuniform' : 'NONUNIFORM',
124 }
125
127 r'[a-zA-Z_][<>(),.\*|a-zA-Z_0-9+]*'
128 t.type=self.reserved.get(t.value,'NAME')
129 return t
130
131 t_ICONST = r'(-|)\d+([uU]|[lL]|[uU][lL]|[lL][uU])?'
132
133 t_FCONST = r'(-|)((\d+)(\.\d+)(e(\+|-)?(\d+))? | (\d+)e(\+|-)?(\d+))([lL]|[fF])?'
134
135 t_SCONST = r'\"([^\\\n]|(\\.))*?\"'
136
137 literals = "(){};[]"
138
139 t_ignore=" \t"
140
141
145
146
151
152
154 print "Illegal character '%s'" % t.value[0]
155 t.lexer.skip(1)
156
158 'global : header dictbody'
159 p[0] = ( p[1] , p[2] )
160
162 'noHeader : dictbody'
163 p[0] = ( None , p[1] )
164
166 '''boundaryDict : header list
167 | header prelist '''
168
169 p[0] = ( p[1] , p[2] )
170
172 'header : FOAMFILE dictionary'
173 p[0] = p[2]
174
176 '''integer : ICONST'''
177 p[0] = int(p[1])
178
180 '''integer : FCONST'''
181 p[0] = float(p[1])
182
184 '''dictionary : '{' dictbody '}'
185 | '{' '}' '''
186 if len(p)==4:
187 p[0] = p[2]
188 else:
189 p[0] = {}
190
191 - def p_dictbody(self,p):
192 '''dictbody : dictbody dictline
193 | dictline
194 | empty'''
195
196 if len(p)==3:
197 p[0]=p[1]
198 p[0][p[2][0]]=p[2][1]
199 else:
200 if p[1]:
201 p[0]=DictProxy()
202 p[0][p[1][0]]=p[1][1]
203 else:
204 p[0]=DictProxy()
205
207 '''list : '(' itemlist ')' '''
208 p[0] = p[2]
209 if len(p[2])==3:
210 isVector=True
211 for i in p[2]:
212 try:
213 float(i)
214 except:
215 isVector=False
216 if isVector:
217 p[0]=apply(Vector,p[2])
218
220 '''prelist : integer '(' itemlist ')' '''
221 p[0] = p[3]
222
224 '''itemlist : itemlist item
225 | item '''
226 if len(p)==2:
227 if p[1]==None:
228 p[0]=[]
229 else:
230 p[0]=[ p[1] ]
231 else:
232 p[0]=p[1]
233 p[0].append(p[2])
234
236 '''dictline : NAME dictitem ';'
237 | NAME list ';'
238 | NAME prelist ';'
239 | NAME fieldvalue ';'
240 | NAME dictionary'''
241 p[0]= ( p[1] , p[2] )
242
244 '''number : integer
245 | FCONST'''
246 p[0] = p[1]
247
249 '''dimension : '[' number number number number number number number ']' '''
250 p[0]=apply(Dimension,p[2:-1])
251
253 '''vector : '(' number number number ')' '''
254 p[0]=Vector(p[2],p[3],p[4])
255
260
265
267 '''dictitem : longitem
268 | pitem'''
269 p[0] = p[1]
270
272 '''longitem : pitemlist pitem'''
273 p[0] = p[1]+(p[2],)
274
276 '''pitemlist : pitemlist pitem
277 | pitem '''
278 if len(p)==2:
279 p[0]=(p[1],)
280 else:
281 p[0]=p[1]+(p[2],)
282
284 '''pitem : NAME
285 | SCONST
286 | number
287 | dictionary
288 | list
289 | dimension
290 | empty'''
291 p[0] = p[1]
292
294 '''item : pitem
295 | list
296 | dictionary'''
297 p[0] = p[1]
298
300 'empty :'
301 pass
302
304 raise "ParserError",("Syntax error at token", p)
305
306
307
309 """Convenience class that parses only a headerless OpenFOAM dictionary"""
310
311 - def __init__(self,content,debug=False):
312 """@param content: the string to be parsed
313 @param debug: output debug information during parsing"""
314
315 FoamFileParser.__init__(self,content,debug=debug,noHeader=True,boundaryDict=False)
316
319
321 """Convenience class that parses only a OpenFOAM polyMesh-boundaries file"""
322
323 - def __init__(self,name,backup=False,debug=False):
328
329 - def parse(self,content):
330 """Constructs a representation of the file"""
331 temp=ParsedParameterFile.parse(self,content)
332 self.content={}
333 for i in range(0,len(temp),2):
334 self.content[temp[i]]=temp[i+1]
335 return self.content
336
338 string="// File generated by PyFoam - sorry for the ugliness\n\n"
339 temp=[]
340 for k,v in self.content.iteritems():
341 temp.append((k,v))
342
343 temp.sort(lambda x,y:cmp(int(x[1]["startFace"]),int(y[1]["startFace"])))
344
345 temp2=[]
346
347 for b in temp:
348 temp2.append(b[0])
349 temp2.append(b[1])
350
351 generator=FoamFileGenerator(temp2,header=self.header)
352 string+=str(generator)
353
354 return string
355