1 """Basis for the handling of OpenFOAM-files"""
2
3 import os,re
4 from os import path
5 from tempfile import mktemp
6
7
8 from PyFoam.Basics.Utilities import Utilities
9 from PyFoam.Basics.LineReader import LineReader
10
12 """ Base class for the other OpenFOAM--file-classes"""
13
14 removedString="//PyFoamRemoved: "
15 """Comment for lines that were overwritten by PyFoam-routines"""
16
17 addedString="//PyFoamAdded"
18 """Comment for lines that were added by PyFoam-routines"""
19
21 """ name - Name of the file"""
22 self.name = path.abspath(name)
23 self.fh=None
24 self.content=None
25
26 - def openFile(self,keepContent=False,mode="r"):
27 """opens the file. To be overloaded by derived classes"""
28 if not keepContent:
29 self.content=None
30 self.fh=open(self.name,mode)
31
33 """ closes the file"""
34 self.fh.close()
35 self.fh=None
36
42
44 """ write the whole File from memory"""
45 if self.content!=None:
46 self.openFile(keepContent=True,mode="w")
47 self.fh.write(self.makeString())
48 self.closeFile()
49
51 """ Parse a string that is to be the content, to be overriden
52 by the sub-classes"""
53
54 return cnt
55
57 """Build a string from self.content, to be overriden by sub-classes"""
58
59 return self.content
60
62 """creates a temporary file"""
63 fn=mktemp(dir=path.dirname(self.name))
64 fh=open(fn,"w")
65
66 return fh,fn
67
68 - def goTo(self,l,s,out=None,echoLast=False,stop=None):
69 """Read lines until a token is found
70
71 @param l: a LineReader object
72 @param s: the string to look for
73 @param out: filehandle to echo the lines to
74 @param stop: pattern that indicates that exp will never be found (only passed through to goMatch)
75 @param echoLast: echo the line with the string"""
76 exp=re.compile("( |^)"+s+"( |$)")
77 m=self.goMatch(l,exp,out=out,stop=stop)
78 if out!=None and echoLast:
79 out.write(l.line+"\n")
80
81 - def goMatch(self,l,exp,out=None,stop=None):
82 """Read lines until a regular expression is matched
83
84 @param l: a LineReader object
85 @param exp: the expression to look for
86 @param out: filehandle to echo the lines to
87 @param stop: pattern that indicates that exp will never be found
88 @return: match-object if exp is found, the line if stop is found and None if the end of the file is reached"""
89 while l.read(self.fh):
90 m=exp.match(l.line)
91 if m!=None:
92 return m
93 elif stop!=None:
94 if stop.match(l.line):
95 return l.line
96 if out!=None:
97 out.write(l.line+"\n")
98
99 return None
100
102 """Copy the rest of the file
103
104 @param l: a LineReader object
105 @param out: filehandle to echo the lines to"""
106 while l.read(self.fh):
107 out.write(l.line+"\n")
108
110 """Undo all the manipulations done by PyFOAM
111
112 Goes through the file and removes all lines that were added"""
113 rmExp= re.compile("^"+self.removedString+"(.*)$")
114 addExp=re.compile("^(.*)"+self.addedString+"$")
115
116 l=LineReader()
117 self.openFile()
118
119 (fh,fn)=self.makeTemp()
120
121 while l.read(self.fh):
122 toPrint=l.line
123
124 m=addExp.match(l.line)
125 if m!=None:
126 continue
127
128 m=rmExp.match(l.line)
129 if m!=None:
130 toPrint=m.group(1)
131
132 fh.write(toPrint+"\n")
133
134 self.closeFile()
135 fh.close()
136 os.rename(fn,self.name)
137
139 """A file with a backup-copy"""
140
142 """@param name: The name of the parameter file
143 @type name: str
144 @param backup: create a backup-copy of the file
145 @type backup: boolean"""
146
147 FileBasis.__init__(self,name)
148
149 if backup:
150 self.backupName=self.name+".backup"
151 self.execute("cp "+self.name+" "+self.backupName)
152 else:
153 self.backupName=None
154
156 """if a backup-copy was made the file is restored from this"""
157 if self.backupName!=None:
158 self.execute("cp "+self.backupName+" "+self.name)
159