pvl/syslog/file.py
changeset 114 2e88e1d8e604
child 117 58aebcd35e1a
equal deleted inserted replaced
113:49e13576d77c 114:2e88e1d8e604
       
     1 """
       
     2     Iterate over lines in file-like objects (without buffering lines!), write (flushing output).
       
     3 """
       
     4 
       
     5 import logging; log = logging.getLogger('pvl.syslog.file')
       
     6 
       
     7 class File (object) :
       
     8     """
       
     9         Follow a file-like object, reading lines until no more are available. Never raises EOFError.
       
    10 
       
    11         Works with python file objects that buffer readlines() when using e.g. `tail -f ... | python -u ...`.
       
    12 
       
    13         readline() may block once there is no more input available, or may return None for evermore.
       
    14 
       
    15         There is no fileno(), this is not pollable. At all. Don't even iterate on this with a timeout.
       
    16         XXX: should this really return None? Might really be better to raise EOFError.. except that readlines() should return normally at EOF...
       
    17     """
       
    18 
       
    19     @classmethod
       
    20     def open (cls, path, mode='r', **opts) :
       
    21         return cls(open(path, mode), **opts)
       
    22 
       
    23     EOL = '\n'
       
    24 
       
    25     def __init__ (self, file) :
       
    26         log.debug("%s", file)
       
    27 
       
    28         self.file = file
       
    29         
       
    30     def readline (self) :
       
    31         """
       
    32             Reads a line from the file, without trailing \n.
       
    33 
       
    34             Returns None on EOF.
       
    35         """
       
    36 
       
    37         line = self.file.readline()
       
    38 
       
    39         if not line :
       
    40             line = None
       
    41         else : 
       
    42             line = line.rstrip('\r\n')
       
    43 
       
    44         log.debug("%s", line)
       
    45 
       
    46         return line
       
    47 
       
    48     def readlines (self) :
       
    49         """
       
    50             Reads any available lines from the file.
       
    51         """
       
    52 
       
    53         while True :
       
    54             line = self.readline()
       
    55             
       
    56             if line is None :
       
    57                 return
       
    58             else :
       
    59                 yield line
       
    60 
       
    61     __iter__ = readlines
       
    62 
       
    63     def writeline (self, line, eol=EOL) :
       
    64         """
       
    65             Write out line, flushing.
       
    66         """
       
    67 
       
    68         self.file.write(line)
       
    69         self.file.write(eol)
       
    70         self.file.flush()
       
    71 
       
    72     __call__ = writeline
       
    73