Package PyFoam :: Package Basics :: Module BasicFile
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Basics.BasicFile

 1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/Basics/BasicFile.py 5789 2009-11-06T12:32:54.086322Z bgschaid  $  
 2  """Basic file output""" 
 3   
4 -class BasicFile(object):
5 """File for data output 6 7 The format of the file is: one data-set per line 8 Values are separated by tabs 9 10 The file is created the first time it is written""" 11
12 - def __init__(self,name):
13 """name - name of the file""" 14 self.name=name 15 self.isOpen=False 16 self.handle=None 17 self.append=False
18
19 - def outputAtStart(self):
20 """A hook for outputting stuff at the beginning of the file""" 21 pass
22
23 - def outputAtEnd(self):
24 """A hook for outputting stuff at the end of the file""" 25 pass
26
27 - def outputAtLineEnd(self):
28 """A hook for outputting stuff at the end of each line""" 29 pass
30
31 - def outputAtLineStart(self):
32 """A hook for outputting stuff at the start of each line""" 33 pass
34
35 - def callAtOpen(self):
36 """A hook that gets called when the file is opened""" 37 pass
38
39 - def callAtClose(self):
40 """A hook that gets called when the file is closed""" 41 pass
42
43 - def getHandle(self):
44 """get the file-handle. File is created and opened if it 45 wasn't opened before""" 46 if not self.isOpen: 47 mode="w" 48 if self.append: 49 mode="a" 50 self.handle=open(self.name,mode) 51 self.isOpen=True 52 if not self.append: 53 self.outputAtStart() 54 self.callAtOpen() 55 56 return self.handle
57
58 - def writeLine(self,data):
59 """write a data set 60 61 data - a tuple with the data-set""" 62 fh=self.getHandle() 63 self.outputAtLineStart() 64 first=True 65 for d in data: 66 if not first: 67 fh.write(" \t") 68 else: 69 first=False 70 fh.write(str(d)) 71 self.outputAtLineEnd() 72 fh.write("\n") 73 fh.flush()
74
75 - def close(self,temporary=False):
76 """close the file 77 @param temporary: only close the file temporary (to be appended on later)""" 78 # print "Closing file\n" 79 if self.handle!=None: 80 self.callAtClose() 81 if not temporary: 82 self.outputAtEnd() 83 else: 84 self.append=True 85 self.handle.close() 86 self.handle=None 87 self.isOpen=False
88