1 """Parameter file is read into memory and modified there"""
2
3 import string,sys
4
5 from FileBasis import FileBasisBackup
6 from PyFoam.Basics.PlyParser import PlyParser
7 from PyFoam.Basics.FoamFileGenerator import FoamFileGenerator
8
10 """ Parameterfile whose complete representation is read into
11 memory, can be manipulated and afterwards written to disk"""
12
13 - def __init__(self,name,backup=False,debug=False,boundaryDict=False):
14 """@param name: The name of the parameter file
15 @param backup: create a backup-copy of the file
16 @param boundaryDict: the file to parse is a boundary file"""
17
18 FileBasisBackup.__init__(self,name,backup=backup)
19 self.debug=debug
20 self.boundaryDict=boundaryDict
21
22 self.header=None
23 self.content=None
24
25 self.readFile()
26
28 """Constructs a representation of the file"""
29 parser=FoamFileParser(content,debug=self.debug,boundaryDict=self.boundaryDict)
30 self.content=parser.getData()
31 self.header=parser.getHeader()
32 return self.content
33
35 return self.content[key]
36
38 self.content[key]=value
39
41 """Generates a string from the contents in memory"""
42 string="// File generated by PyFoam - sorry for the ugliness\n\n"
43
44
45
46
47
48 generator=FoamFileGenerator(self.content,header=self.header)
49 string+=generator.makeString()
50
51 return string
52
54 """Class that parses a string that contains the contents of an
55 OpenFOAM-file and builds a nested structure of directories and
56 lists from it"""
57
58 - def __init__(self,content,debug=False,noHeader=False,boundaryDict=False):
59 """@param content: the string to be parsed
60 @param debug: output debug information during parsing
61 @param noHeader: switch that turns off the parsing of the header"""
62
63 self.data=None
64 self.header=None
65 self.debug=debug
66
67 if noHeader:
68 self.start='noHeader'
69
70 if boundaryDict:
71 self.start='boundaryDict'
72
73 PlyParser.__init__(self,debug=debug)
74
75
76
77
78 self.header,self.data=self.parse(content)
79
81 """ Get the data structure"""
82 return self.data
83
85 """ Get the OpenFOAM-header"""
86 return self.header
87
88 - def printContext(self,c,ind):
89 """Prints the context of the current index"""
90 print "------"
91 print c[max(0,ind-100):max(0,ind-1)]
92 print "------"
93 print ">",c[ind-1],"<"
94 print "------"
95 print c[min(len(c),ind):min(len(c),ind+100)]
96 print "------"
97
99 """Prints the error message of the parser and exit"""
100 print "PARSER ERROR:",text
101 print "On index",ind
102 self.printContext(c,ind)
103 raise ParseError
104 sys.exit(-1)
105
106 tokens = (
107 'NAME',
108 'ICONST',
109 'FCONST',
110 'SCONST',
111 'FOAMFILE',
112 'UNIFORM',
113 'NONUNIFORM',
114 )
115
116 reserved = {
117 'FoamFile' : 'FOAMFILE',
118 'uniform' : 'UNIFORM',
119 'nonuniform' : 'NONUNIFORM',
120 }
121
123 r'[a-zA-Z_][<>(),.\*|a-zA-Z_0-9]*'
124 t.type=self.reserved.get(t.value,'NAME')
125 return t
126
127 t_ICONST = r'(-|)\d+([uU]|[lL]|[uU][lL]|[lL][uU])?'
128
129 t_FCONST = r'(-|)((\d+)(\.\d+)(e(\+|-)?(\d+))? | (\d+)e(\+|-)?(\d+))([lL]|[fF])?'
130
131 t_SCONST = r'\"([^\\\n]|(\\.))*?\"'
132
133 literals = "(){};[]"
134
135 t_ignore=" \t"
136
137
141
142
147
148
150 print "Illegal character '%s'" % t.value[0]
151 t.lexer.skip(1)
152
154 'global : header dictbody'
155 p[0] = ( p[1] , p[2] )
156
158 'noHeader : dictbody'
159 p[0] = ( None , p[1] )
160
162 'boundaryDict : header list'
163
164 p[0] = ( p[1] , p[2] )
165
167 'header : FOAMFILE dictionary'
168 p[0] = p[2]
169
171 '''dictionary : '{' dictbody '}'
172 | '{' '}' '''
173 if len(p)==4:
174 p[0] = p[2]
175 else:
176 p[0] = {}
177
178 - def p_dictbody(self,p):
179 '''dictbody : dictbody dictline
180 | dictline
181 | empty'''
182
183 if len(p)==3:
184 p[0]=p[1]
185 p[0][p[2][0]]=p[2][1]
186 else:
187 if p[1]:
188 p[0]={p[1][0]:p[1][1]}
189 else:
190 p[0]={}
191
193 '''list : '(' itemlist ')' '''
194 p[0] = p[2]
195
197 '''list : ICONST '(' itemlist ')' '''
198 p[0] = p[3]
199
201 '''itemlist : itemlist item
202 | item '''
203 if len(p)==2:
204 if p[1]==None:
205 p[0]=[]
206 else:
207 p[0]=[ p[1] ]
208 else:
209 p[0]=p[1]
210 p[0].append(p[2])
211
213 '''dictline : NAME dictitem ';'
214 | NAME list ';'
215 | NAME fieldvalue ';'
216 | NAME dictionary'''
217 p[0]= ( p[1] , p[2] )
218
220 '''number : ICONST
221 | FCONST'''
222 p[0] = p[1]
223
225 '''dimension : '[' number number number number number number number ']' '''
226 p[0]=string.join(p[1:])
227
229 '''vector : '(' number number number ')' '''
230 p[0]=string.join(p[1:])
231
233 '''fieldvalue : UNIFORM number
234 | UNIFORM vector'''
235 p[0] = p[1]+" "+p[2]
236
238 '''fieldvalue : NONUNIFORM NAME list'''
239 p[0] = (p[1]+" "+p[2] , p[3])
240
242 '''dictitem : longitem
243 | pitem'''
244 p[0] = p[1]
245
247 '''longitem : pitemlist pitem'''
248 p[0] = p[1]+(p[2],)
249
251 '''pitemlist : pitemlist pitem
252 | pitem '''
253 if len(p)==2:
254 p[0]=(p[1],)
255 else:
256 p[0]=p[1]+(p[2],)
257
259 '''pitem : NAME
260 | SCONST
261 | number
262 | dimension
263 | empty'''
264 p[0] = p[1]
265
267 '''item : pitem
268 | list
269 | dictionary'''
270 p[0] = p[1]
271
273 'empty :'
274 pass
275
277 print "Syntax error at token", p
278
279 self.yacc.errok()
280