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

Source Code for Module PyFoam.Basics.LineReader

 1  """Read a file line by line""" 
 2   
 3  from PyFoam.Infrastructure.Logging import foamLogger 
 4   
5 -class LineReader(object):
6 """Read a line from a file 7 8 The line is stripped of whitespaces at the start and the end of 9 the line and stored in a variable self.line""" 10
11 - def __init__(self):
12 self.line="" 13 self.goOn=True 14 self.wasInterupted=False 15 self.bytes=0L
16
17 - def bytesRead(self):
18 """@return: number of bytes that were already read""" 19 return self.bytes
20
21 - def userSaidStop(self):
22 """@return: whether the reader caught a Keyboard-interrupt""" 23 return self.wasInterupted
24
25 - def read(self,fh):
26 """reads the next line 27 28 fh - filehandle to read from 29 30 Return value: False if the end of the file was reached. True 31 otherwise""" 32 33 if not self.goOn: 34 return False 35 36 try: 37 self.line=fh.readline() 38 self.bytes+=len(self.line) 39 except KeyboardInterrupt,e: 40 foamLogger().warning("Keyboard Interrupt") 41 print " Interrupted by the Keyboard" 42 self.wasInterupted=True 43 self.goOn=False 44 self.line="" 45 return False 46 47 if len(self.line)>0: 48 status=True 49 else: 50 status=False 51 self.line=self.line.strip() 52 53 return status
54