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

Source Code for Module PyFoam.RunDictionary.FileBasis

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