1
2 """Basis for the handling of OpenFOAM-files
3
4 Transparently accepts gnuzipped files"""
5
6 import os,re
7 from os import path
8 from tempfile import mktemp
9 import gzip
10
11
12 from PyFoam.Basics.Utilities import Utilities
13 from PyFoam.Basics.LineReader import LineReader
14
15 from PyFoam.Error import warning
16
18 """ Base class for the other OpenFOAM--file-classes"""
19
20 removedString="//PyFoamRemoved: "
21 """Comment for lines that were overwritten by PyFoam-routines"""
22
23 addedString="//PyFoamAdded"
24 """Comment for lines that were added by PyFoam-routines"""
25
26 - def __init__(self,name,createZipped=True):
27 """@param name: Name of the file. If the field is zipped the .gz is
28 appended
29 @param createZipped: if the file doesnot exist: should it be created
30 as a zipped file?"""
31 self.name = path.abspath(name)
32 self.exists = False
33
34 if path.exists(self.name):
35 self.exists = True
36 self.zipped=False
37 if path.splitext(self.name)[1]==".gz":
38 self.zipped=True
39 elif path.exists(self.name+".gz"):
40 warning(self.name+".gz","and",self.name,"existing - using the unzipped")
41 elif path.exists(self.name+".gz"):
42 self.zipped=True
43 self.exists = True
44 else:
45 self.zipped=createZipped
46
47 if path.splitext(self.name)[1]==".gz":
48 self.name=self.name[:-3]
49
50 self.fh=None
51 self.content=None
52
54 """The full filename with appended .gz (if zipped)"""
55 if self.zipped:
56 return self.name+".gz"
57 else:
58 return self.name
59
61 """Returns the basic file name (without .gz)"""
62 return path.basename(self.name)
63
64 - def openFile(self,keepContent=False,mode="r"):
65 """opens the file. To be overloaded by derived classes"""
66 if not keepContent:
67 self.content=None
68 if self.zipped:
69 self.fh=gzip.open(self.name+".gz",mode)
70 else:
71 self.fh=open(self.name,mode)
72
74 """ closes the file"""
75 self.fh.close()
76 self.fh=None
77
83
85 """ write the whole File from memory
86 @param content: content that should replace the old content"""
87 if content!=None:
88 self.content=content
89 if self.content!=None:
90 self.openFile(keepContent=True,mode="w")
91 self.fh.write(str(self))
92 self.closeFile()
93
95 """ Writes a copy of the file. Extends with .gz if the original
96 is zipped
97 @param name: Name under which the file is written"""
98 if path.abspath(self.name)==path.abspath(name):
99 warning(name,"and",self.name,"seem to be the same. Nothing done")
100 return
101
102 erase=False
103 if self.content==None:
104 erase=True
105 self.readFile()
106
107 tmp=self.name
108 self.name=name
109 self.writeFile()
110 self.name=tmp
111
112 if erase:
113 self.content=None
114
116 """ Parse a string that is to be the content, to be overriden
117 by the sub-classes"""
118
119 return cnt
120
122 """Build a string from self.content, to be overriden by sub-classes"""
123
124 return self.content
125
127 """creates a temporary file"""
128 fn=mktemp(dir=path.dirname(self.name))
129 if self.zipped:
130 fh=gzip.open(fn,"w")
131 else:
132 fh=open(fn,"w")
133
134 return fh,fn
135
136 - def goTo(self,l,s,out=None,echoLast=False,stop=None):
137 """Read lines until a token is found
138
139 @param l: a LineReader object
140 @param s: the string to look for
141 @param out: filehandle to echo the lines to
142 @param stop: pattern that indicates that exp will never be found (only passed through to goMatch)
143 @param echoLast: echo the line with the string"""
144 exp=re.compile("( |^)"+s+"( |$)")
145 self.goMatch(l,exp,out=out,stop=stop)
146 if out!=None and echoLast:
147 out.write(l.line+"\n")
148
149 - def goMatch(self,l,exp,out=None,stop=None):
150 """Read lines until a regular expression is matched
151
152 @param l: a LineReader object
153 @param exp: the expression to look for
154 @param out: filehandle to echo the lines to
155 @param stop: pattern that indicates that exp will never be found
156 @return: match-object if exp is found, the line if stop is found and None if the end of the file is reached"""
157 while l.read(self.fh):
158 m=exp.match(l.line)
159 if m!=None:
160 return m
161 elif stop!=None:
162 if stop.match(l.line):
163 return l.line
164 if out!=None:
165 out.write(l.line+"\n")
166
167 return None
168
170 """Copy the rest of the file
171
172 @param l: a LineReader object
173 @param out: filehandle to echo the lines to"""
174 while l.read(self.fh):
175 out.write(l.line+"\n")
176
178 """Undo all the manipulations done by PyFOAM
179
180 Goes through the file and removes all lines that were added"""
181 rmExp= re.compile("^"+self.removedString+"(.*)$")
182 addExp=re.compile("^(.*)"+self.addedString+"$")
183
184 l=LineReader()
185 self.openFile()
186
187 (fh,fn)=self.makeTemp()
188
189 while l.read(self.fh):
190 toPrint=l.line
191
192 m=addExp.match(l.line)
193 if m!=None:
194 continue
195
196 m=rmExp.match(l.line)
197 if m!=None:
198 toPrint=m.group(1)
199
200 fh.write(toPrint+"\n")
201
202 self.closeFile()
203 fh.close()
204 os.rename(fn,self.name)
205
221
223 """A file with a backup-copy"""
224
225 - def __init__(self,name,backup=False,createZipped=True):
226 """@param name: The name of the parameter file
227 @type name: str
228 @param backup: create a backup-copy of the file
229 @type backup: boolean"""
230
231 FileBasis.__init__(self,name,createZipped=createZipped)
232
233 if backup:
234 self.backupName=self.name+".backup"
235 self.execute("cp "+self.name+" "+self.backupName)
236 else:
237 self.backupName=None
238
240 """if a backup-copy was made the file is restored from this"""
241 if self.backupName!=None:
242 self.execute("cp "+self.backupName+" "+self.name)
243 self.execute("rm "+self.backupName)
244
248