Package PyFoam :: Package RunDictionary :: Module ParsedParameterFile
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.RunDictionary.ParsedParameterFile

  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   
9 -class ParsedParameterFile(FileBasisBackup):
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
27 - def parse(self,content):
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
34 - def __getitem__(self,key):
35 return self.content[key]
36
37 - def __setitem__(self,key,value):
38 self.content[key]=value
39
40 - def makeString(self):
41 """Generates a string from the contents in memory""" 42 string="// File generated by PyFoam - sorry for the ugliness\n\n" 43 ## if self.header: 44 ## generator=FoamFileGenerator(self.header) 45 ## string+=generator.makeString() 46 ## string+="\n// End of header\n\n" 47 48 generator=FoamFileGenerator(self.content,header=self.header) 49 string+=generator.makeString() 50 51 return string
52
53 -class FoamFileParser(PlyParser):
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 #sys.setrecursionlimit(50000) 76 #print sys.getrecursionlimit() 77 78 self.header,self.data=self.parse(content)
79
80 - def getData(self):
81 """ Get the data structure""" 82 return self.data
83
84 - def getHeader(self):
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
98 - def parserError(self,text,c,ind):
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
122 - def t_NAME(self,t):
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 # Define a rule so we can track line numbers
138 - def t_newline(self,t):
139 r'\n+' 140 t.lexer.lineno += len(t.value)
141 142 # C or C++ comment (ignore)
143 - def t_ccode_comment(self,t):
144 r'(/\*(.|\n)*\*/)|(//.*)' 145 t.lexer.lineno += t.value.count('\n') 146 pass
147 148 # Error handling rule
149 - def t_error(self,t):
150 print "Illegal character '%s'" % t.value[0] 151 t.lexer.skip(1)
152
153 - def p_global(self,p):
154 'global : header dictbody' 155 p[0] = ( p[1] , p[2] )
156
157 - def p_noHeader(self,p):
158 'noHeader : dictbody' 159 p[0] = ( None , p[1] )
160
161 - def p_boundaryDict(self,p):
162 'boundaryDict : header list' 163 # p[0] = ( p[1] , dict(zip(p[2][::2],p[2][1::2])) ) 164 p[0] = ( p[1] , p[2] )
165
166 - def p_header(self,p):
167 'header : FOAMFILE dictionary' 168 p[0] = p[2]
169
170 - def p_dictionary(self,p):
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
192 - def p_list_normal(self,p):
193 '''list : '(' itemlist ')' ''' 194 p[0] = p[2]
195
196 - def p_list_prefixed(self,p):
197 '''list : ICONST '(' itemlist ')' ''' 198 p[0] = p[3]
199
200 - def p_itemlist(self,p):
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
212 - def p_dictline(self,p):
213 '''dictline : NAME dictitem ';' 214 | NAME list ';' 215 | NAME fieldvalue ';' 216 | NAME dictionary''' 217 p[0]= ( p[1] , p[2] )
218
219 - def p_number(self,p):
220 '''number : ICONST 221 | FCONST''' 222 p[0] = p[1]
223
224 - def p_dimension(self,p):
225 '''dimension : '[' number number number number number number number ']' ''' 226 p[0]=string.join(p[1:])
227
228 - def p_vector(self,p):
229 '''vector : '(' number number number ')' ''' 230 p[0]=string.join(p[1:])
231
232 - def p_fieldvalue_uniform(self,p):
233 '''fieldvalue : UNIFORM number 234 | UNIFORM vector''' 235 p[0] = p[1]+" "+p[2]
236
237 - def p_fieldvalue_nonuniform(self,p):
238 '''fieldvalue : NONUNIFORM NAME list''' 239 p[0] = (p[1]+" "+p[2] , p[3])
240
241 - def p_dictitem(self,p):
242 '''dictitem : longitem 243 | pitem''' 244 p[0] = p[1]
245
246 - def p_longitem(self,p):
247 '''longitem : pitemlist pitem''' 248 p[0] = p[1]+(p[2],)
249
250 - def p_pitemlist(self,p):
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
258 - def p_pitem(self,p):
259 '''pitem : NAME 260 | SCONST 261 | number 262 | dimension 263 | empty''' 264 p[0] = p[1]
265
266 - def p_item(self,p):
267 '''item : pitem 268 | list 269 | dictionary''' 270 p[0] = p[1]
271
272 - def p_empty(self,p):
273 'empty :' 274 pass
275
276 - def p_error(self,p):
277 print "Syntax error at token", p # .type, p.lineno 278 # Just discard the token and tell the parser it's okay. 279 self.yacc.errok()
280