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

Source Code for Module PyFoam.Basics.LineReader

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