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

Source Code for Module PyFoam.RunDictionary.FileBasis

  1  #  ICE Revision: $Id: FileBasis.py 7793 2007-08-19 18:55:31Z 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 -class FileBasis(Utilities):
16 """ Base class for the other OpenFOAM--file-classes""" 17 18 removedString="//PyFoamRemoved: " 19 """Comment for lines that were overwritten by PyFoam-routines""" 20 21 addedString="//PyFoamAdded" 22 """Comment for lines that were added by PyFoam-routines""" 23
24 - def __init__(self,name):
25 """@param name: Name of the file. If the field is zipped the .gz is 26 appended""" 27 self.name = path.abspath(name) 28 if path.exists(self.name): 29 self.zipped=False 30 if path.splitext(self.name)[1]==".gz": 31 self.zipped=True 32 else: 33 self.zipped=True 34 self.name+=".gz" 35 36 self.fh=None 37 self.content=None
38
39 - def openFile(self,keepContent=False,mode="r"):
40 """opens the file. To be overloaded by derived classes""" 41 if not keepContent: 42 self.content=None 43 if self.zipped: 44 self.fh=gzip.open(self.name,mode) 45 else: 46 self.fh=open(self.name,mode)
47
48 - def closeFile(self):
49 """ closes the file""" 50 self.fh.close() 51 self.fh=None
52
53 - def readFile(self):
54 """ read the whole File into memory""" 55 self.openFile() 56 self.content=self.parse(self.fh.read()) 57 self.closeFile()
58
59 - def writeFile(self):
60 """ write the whole File from memory""" 61 if self.content!=None: 62 self.openFile(keepContent=True,mode="w") 63 self.fh.write(str(self)) 64 self.closeFile()
65
66 - def parse(self,cnt):
67 """ Parse a string that is to be the content, to be overriden 68 by the sub-classes""" 69 70 return cnt
71
72 - def __str__(self):
73 """Build a string from self.content, to be overriden by sub-classes""" 74 75 return self.content
76
77 - def makeTemp(self):
78 """creates a temporary file""" 79 fn=mktemp(dir=path.dirname(self.name)) 80 if self.zipped: 81 fh=gzip.open(fn,"w") 82 else: 83 fh=open(fn,"w") 84 85 return fh,fn
86
87 - def goTo(self,l,s,out=None,echoLast=False,stop=None):
88 """Read lines until a token is found 89 90 @param l: a LineReader object 91 @param s: the string to look for 92 @param out: filehandle to echo the lines to 93 @param stop: pattern that indicates that exp will never be found (only passed through to goMatch) 94 @param echoLast: echo the line with the string""" 95 exp=re.compile("( |^)"+s+"( |$)") 96 m=self.goMatch(l,exp,out=out,stop=stop) 97 if out!=None and echoLast: 98 out.write(l.line+"\n")
99
100 - def goMatch(self,l,exp,out=None,stop=None):
101 """Read lines until a regular expression is matched 102 103 @param l: a LineReader object 104 @param exp: the expression to look for 105 @param out: filehandle to echo the lines to 106 @param stop: pattern that indicates that exp will never be found 107 @return: match-object if exp is found, the line if stop is found and None if the end of the file is reached""" 108 while l.read(self.fh): 109 m=exp.match(l.line) 110 if m!=None: 111 return m 112 elif stop!=None: 113 if stop.match(l.line): 114 return l.line 115 if out!=None: 116 out.write(l.line+"\n") 117 118 return None
119
120 - def copyRest(self,l,out):
121 """Copy the rest of the file 122 123 @param l: a LineReader object 124 @param out: filehandle to echo the lines to""" 125 while l.read(self.fh): 126 out.write(l.line+"\n")
127
128 - def purgeFile(self):
129 """Undo all the manipulations done by PyFOAM 130 131 Goes through the file and removes all lines that were added""" 132 rmExp= re.compile("^"+self.removedString+"(.*)$") 133 addExp=re.compile("^(.*)"+self.addedString+"$") 134 135 l=LineReader() 136 self.openFile() 137 138 (fh,fn)=self.makeTemp() 139 140 while l.read(self.fh): 141 toPrint=l.line 142 143 m=addExp.match(l.line) 144 if m!=None: 145 continue 146 147 m=rmExp.match(l.line) 148 if m!=None: 149 toPrint=m.group(1) 150 151 fh.write(toPrint+"\n") 152 153 self.closeFile() 154 fh.close() 155 os.rename(fn,self.name)
156
157 -class FileBasisBackup(FileBasis):
158 """A file with a backup-copy""" 159
160 - def __init__(self,name,backup=False):
161 """@param name: The name of the parameter file 162 @type name: str 163 @param backup: create a backup-copy of the file 164 @type backup: boolean""" 165 166 FileBasis.__init__(self,name) 167 168 if backup: 169 self.backupName=self.name+".backup" 170 self.execute("cp "+self.name+" "+self.backupName) 171 else: 172 self.backupName=None
173
174 - def restore(self):
175 """if a backup-copy was made the file is restored from this""" 176 if self.backupName!=None: 177 self.execute("cp "+self.backupName+" "+self.name)
178