summaryrefslogtreecommitdiffstats
path: root/retiolum/scripts/adv_graphgen/tinc_graphs/BackwardsReader.py
diff options
context:
space:
mode:
authormakefu <root@pigstarter.de>2015-09-09 14:41:38 +0200
committermakefu <root@pigstarter.de>2015-09-09 14:41:38 +0200
commit05bf2c8be209e619f8c9e727fd1353198b042642 (patch)
tree4f7a9439137fe78f979b235666a3533f9e249eca /retiolum/scripts/adv_graphgen/tinc_graphs/BackwardsReader.py
parente9e7d6baf986488c43d418887f6cf95f5d896189 (diff)
begin packaging of tinc_stats
Diffstat (limited to 'retiolum/scripts/adv_graphgen/tinc_graphs/BackwardsReader.py')
-rw-r--r--retiolum/scripts/adv_graphgen/tinc_graphs/BackwardsReader.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/retiolum/scripts/adv_graphgen/tinc_graphs/BackwardsReader.py b/retiolum/scripts/adv_graphgen/tinc_graphs/BackwardsReader.py
new file mode 100644
index 00000000..6bdbf43c
--- /dev/null
+++ b/retiolum/scripts/adv_graphgen/tinc_graphs/BackwardsReader.py
@@ -0,0 +1,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]