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

Source Code for Module PyFoam.RunDictionary.FileBasis

  1  #  ICE Revision: $Id: FileBasis.py 8608 2008-03-25 09:19:35Z bgschaid $  
  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   
17 -class FileBasis(Utilities):
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):
27 """@param name: Name of the file. If the field is zipped the .gz is 28 appended""" 29 self.name = path.abspath(name) 30 if path.exists(self.name): 31 self.zipped=False 32 if path.splitext(self.name)[1]==".gz": 33 self.zipped=True 34 elif path.exists(self.name+".gz"): 35 warning(self.name+".gz","and",self.name,"existing - using the unzipped") 36 elif path.exists(self.name+".gz"): 37 self.zipped=True 38 else: 39 self.zipped=True 40 41 if path.splitext(self.name)[1]==".gz": 42 self.name=self.name[:-3] 43 44 self.fh=None 45 self.content=None
46
47 - def realName(self):
48 """The full filename with appended .gz (if zipped)""" 49 if self.zipped: 50 return self.name+".gz" 51 else: 52 return self.name
53
54 - def baseName(self):
55 """Returns the basic file name (without .gz)""" 56 return path.basename(self.name)
57
58 - def openFile(self,keepContent=False,mode="r"):
59 """opens the file. To be overloaded by derived classes""" 60 if not keepContent: 61 self.content=None 62 if self.zipped: 63 self.fh=gzip.open(self.name+".gz",mode) 64 else: 65 self.fh=open(self.name,mode)
66
67 - def closeFile(self):
68 """ closes the file""" 69 self.fh.close() 70 self.fh=None
71
72 - def readFile(self):
73 """ read the whole File into memory""" 74 self.openFile() 75 self.content=self.parse(self.fh.read()) 76 self.closeFile()
77
78 - def writeFile(self,content=None):
79 """ write the whole File from memory 80 @param content: content that should replace the old content""" 81 if content!=None: 82 self.content=content 83 if self.content!=None: 84 self.openFile(keepContent=True,mode="w") 85 self.fh.write(str(self)) 86 self.closeFile()
87
88 - def writeFileAs(self,name):
89 """ Writes a copy of the file. Extends with .gz if the original 90 is zipped 91 @param name: Name under which the file is written""" 92 if path.abspath(self.name)==path.abspath(name): 93 warning(name,"and",self.name,"seem to be the same. Nothing done") 94 return 95 96 erase=False 97 if self.content==None: 98 erase=True 99 self.readFile() 100 101 tmp=self.name 102 self.name=name 103 self.writeFile() 104 self.name=tmp 105 106 if erase: 107 self.content=None
108
109 - def parse(self,cnt):
110 """ Parse a string that is to be the content, to be overriden 111 by the sub-classes""" 112 113 return cnt
114
115 - def __str__(self):
116 """Build a string from self.content, to be overriden by sub-classes""" 117 118 return self.content
119
120 - def makeTemp(self):
121 """creates a temporary file""" 122 fn=mktemp(dir=path.dirname(self.name)) 123 if self.zipped: 124 fh=gzip.open(fn,"w") 125 else: 126 fh=open(fn,"w") 127 128 return fh,fn
129
130 - def goTo(self,l,s,out=None,echoLast=False,stop=None):
131 """Read lines until a token is found 132 133 @param l: a LineReader object 134 @param s: the string to look for 135 @param out: filehandle to echo the lines to 136 @param stop: pattern that indicates that exp will never be found (only passed through to goMatch) 137 @param echoLast: echo the line with the string""" 138 exp=re.compile("( |^)"+s+"( |$)") 139 m=self.goMatch(l,exp,out=out,stop=stop) 140 if out!=None and echoLast: 141 out.write(l.line+"\n")
142
143 - def goMatch(self,l,exp,out=None,stop=None):
144 """Read lines until a regular expression is matched 145 146 @param l: a LineReader object 147 @param exp: the expression to look for 148 @param out: filehandle to echo the lines to 149 @param stop: pattern that indicates that exp will never be found 150 @return: match-object if exp is found, the line if stop is found and None if the end of the file is reached""" 151 while l.read(self.fh): 152 m=exp.match(l.line) 153 if m!=None: 154 return m 155 elif stop!=None: 156 if stop.match(l.line): 157 return l.line 158 if out!=None: 159 out.write(l.line+"\n") 160 161 return None
162
163 - def copyRest(self,l,out):
164 """Copy the rest of the file 165 166 @param l: a LineReader object 167 @param out: filehandle to echo the lines to""" 168 while l.read(self.fh): 169 out.write(l.line+"\n")
170
171 - def purgeFile(self):
172 """Undo all the manipulations done by PyFOAM 173 174 Goes through the file and removes all lines that were added""" 175 rmExp= re.compile("^"+self.removedString+"(.*)$") 176 addExp=re.compile("^(.*)"+self.addedString+"$") 177 178 l=LineReader() 179 self.openFile() 180 181 (fh,fn)=self.makeTemp() 182 183 while l.read(self.fh): 184 toPrint=l.line 185 186 m=addExp.match(l.line) 187 if m!=None: 188 continue 189 190 m=rmExp.match(l.line) 191 if m!=None: 192 toPrint=m.group(1) 193 194 fh.write(toPrint+"\n") 195 196 self.closeFile() 197 fh.close() 198 os.rename(fn,self.name)
199
200 -class FileBasisBackup(FileBasis):
201 """A file with a backup-copy""" 202
203 - def __init__(self,name,backup=False):
204 """@param name: The name of the parameter file 205 @type name: str 206 @param backup: create a backup-copy of the file 207 @type backup: boolean""" 208 209 FileBasis.__init__(self,name) 210 211 if backup: 212 self.backupName=self.name+".backup" 213 self.execute("cp "+self.name+" "+self.backupName) 214 else: 215 self.backupName=None
216
217 - def restore(self):
218 """if a backup-copy was made the file is restored from this""" 219 if self.backupName!=None: 220 self.execute("cp "+self.backupName+" "+self.name) 221 self.execute("rm "+self.backupName)
222