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

Source Code for Module PyFoam.Basics.BasicFile

 1  #  ICE Revision: $Id: BasicFile.py 7581 2007-06-27 15:29:14Z 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
18 - def outputAtStart(self):
19 """A hook for outputting stuff at the beginning of the file""" 20 pass
21
22 - def outputAtEnd(self):
23 """A hook for outputting stuff at the end of the file""" 24 pass
25
26 - def outputAtLineEnd(self):
27 """A hook for outputting stuff at the end of each line""" 28 pass
29
30 - def outputAtLineStart(self):
31 """A hook for outputting stuff at the start of each line""" 32 pass
33
34 - def getHandle(self):
35 """get the file-handle. File is created and opened if it 36 wasn't opened before""" 37 if not self.isOpen: 38 self.handle=open(self.name,"w") 39 self.isOpen=True 40 self.outputAtStart() 41 42 return self.handle
43
44 - def writeLine(self,data):
45 """write a data set 46 47 data - a tuple with the data-set""" 48 fh=self.getHandle() 49 self.outputAtLineStart() 50 first=True 51 for d in data: 52 if not first: 53 fh.write(" \t") 54 else: 55 first=False 56 fh.write(str(d)) 57 self.outputAtLineEnd() 58 fh.write("\n") 59 fh.flush()
60
61 - def close(self):
62 """close the file""" 63 # print "Closing file\n" 64 if self.handle!=None: 65 self.outputAtEnd() 66 self.handle.close() 67 self.handle=None
68