summaryrefslogtreecommitdiffstats
path: root/retiolum/scripts/adv_graphgen/BackwardsReader.py
blob: 6bdbf43cdfc6773f3deb08d749592b73b0339cb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import sys
import os
import string

class BackwardsReader:
  """ Stripped and stolen from : http://code.activestate.com/recipes/120686-read-a-text-file-backwards/ """
  def readline(self):
    while len(self.data) == 1 and ((self.blkcount * self.blksize) < self.size):
      self.blkcount = self.blkcount + 1
      line = self.data[0]
      try:
        self.f.seek(-self.blksize * self.blkcount, 2) 
        self.data = string.split(self.f.read(self.blksize) + line, '\n')
      except IOError:  
        self.f.seek(0)
        self.data = string.split(self.f.read(self.size - (self.blksize * (self.blkcount-1))) + line, '\n')

    if len(self.data) == 0:
      return ""

    line = self.data[-1]
    self.data = self.data[:-1]
    return line + '\n'

  def __init__(self, file, blksize=4096):
    """initialize the internal structures"""
    self.size = os.stat(file)[6]
    self.blksize = blksize
    self.blkcount = 1
    self.f = open(file, 'rb')
    if self.size > self.blksize:
      self.f.seek(-self.blksize * self.blkcount, 2) 
    self.data = string.split(self.f.read(self.blksize), '\n')
    if not self.data[-1]:
      self.data = self.data[:-1]