sites/irclogs.qmsk.net/log_source.py
branchsites
changeset 43 fc11c4e86a82
parent 41 9585441a4bfb
equal deleted inserted replaced
42:5a72c00c4ae4 43:fc11c4e86a82
    61         # seek to end of file
    61         # seek to end of file
    62         self.file.seek(0, os.SEEK_END)
    62         self.file.seek(0, os.SEEK_END)
    63 
    63 
    64         # read offset
    64         # read offset
    65         # XXX; why -2 ?
    65         # XXX; why -2 ?
    66         offset = self.file.tell() - 2
    66         size = offset = self.file.tell() - 2
    67 
    67 
    68         # use this blocksize
    68         # use this blocksize
    69         BLOCKSIZE = 1024
    69         BLOCKSIZE = 1024
    70 
    70 
    71         # trailing data
    71         # trailing data
    72         buf = ''
    72         buf = ''
    73 
    73 
    74         # read a block at a time, backwards
    74         # read a block at a time, backwards
    75         while  count > 0 and offset >= 0:
    75         while  count > 0 and offset >= 0:
    76             # update offset
    76             # update offset back one block
    77             offset -= BLOCKSIZE
    77             offset -= BLOCKSIZE
    78 
    78 
    79             # normalize to zero
    79             # normalize to zero
    80             if offset < 0 :
    80             if offset < 0 :
    81                 offset = 0
    81                 offset = 0
    82 
    82 
    83             # seek backwards one block
    83             # seek to offset
    84             self.file.seek(offset)
    84             self.file.seek(offset)
    85 
    85 
    86             # add the new block to our buffer
    86             # add the new block to our buffer
    87             read_buf = self.file.read(BLOCKSIZE)
    87             read_buf = self.file.read(BLOCKSIZE)
    88 
    88 
       
    89             # XXX: trim off extra...
       
    90             if len(read_buf) > BLOCKSIZE :
       
    91                 read_buf = read_buf[:BLOCKSIZE]
       
    92 
    89             # make sure we got the right amount of data
    93             # make sure we got the right amount of data
    90             assert len(read_buf) == BLOCKSIZE, "read(%d) -> %d" % (BLOCKSIZE, len(read_buf))
    94             assert len(read_buf) == BLOCKSIZE, "read(%d) @ %d/%d -> %d" % (BLOCKSIZE, offset, size, len(read_buf))
    91 
    95 
    92             # add in our previous buf
    96             # add in our previous buf
    93             buf = read_buf + buf
    97             buf = read_buf + buf
    94             
    98             
    95             # split out lines
    99             # split out lines
    97 
   101 
    98             # keep the first one as our buffer, as it's incomplete
   102             # keep the first one as our buffer, as it's incomplete
    99             buf = buf_lines[0]
   103             buf = buf_lines[0]
   100 
   104 
   101             # add up to count lines to our lines buffer
   105             # add up to count lines to our lines buffer
   102             lines = buf_lines[1:count + 1] + lines
   106             lines = buf_lines[-min(count, len(buf_lines) - 1):] + lines
   103 
   107 
   104             # update count
   108             # update count
   105             count -= (len(buf_lines) - 1)
   109             count -= (len(buf_lines) - 1)
   106 
   110 
   107         # return the line list
   111         # return the line list