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

Source Code for Module PyFoam.Basics.BasicFile

 1  """Basic file output""" 
 2   
3 -class BasicFile(object):
4 """File for data output 5 6 The format of the file is: one data-set per line 7 Values are separated by tabs 8 9 The file is created the first time it is written""" 10
11 - def __init__(self,name):
12 """name - name of the file""" 13 self.name=name 14 self.isOpen=False 15 self.handle=None
16
17 - def outputAtStart(self):
18 """A hook for outputting stuff at the beginning of the file""" 19 pass
20
21 - def outputAtEnd(self):
22 """A hook for outputting stuff at the end of the file""" 23 pass
24
25 - def outputAtLineEnd(self):
26 """A hook for outputting stuff at the end of each line""" 27 pass
28
29 - def outputAtLineStart(self):
30 """A hook for outputting stuff at the start of each line""" 31 pass
32
33 - def getHandle(self):
34 """get the file-handle. File is created and opened if it 35 wasn't opened before""" 36 if not self.isOpen: 37 self.handle=open(self.name,"w") 38 self.isOpen=True 39 self.outputAtStart() 40 41 return self.handle
42
43 - def writeLine(self,data):
44 """write a data set 45 46 data - a tuple with the data-set""" 47 fh=self.getHandle() 48 self.outputAtLineStart() 49 first=True 50 for d in data: 51 if not first: 52 fh.write(" \t") 53 else: 54 first=False 55 fh.write(str(d)) 56 self.outputAtLineEnd() 57 fh.write("\n") 58 fh.flush()
59
60 - def close(self):
61 """close the file""" 62 # print "Closing file\n" 63 if self.handle!=None: 64 self.outputAtEnd() 65 self.handle.close() 66 self.handle=None
67