84 lines
2.1 KiB
Python
84 lines
2.1 KiB
Python
#!/usr/bin/env python
|
|
#
|
|
# tictac.py
|
|
# http://mornie.org/code/browser/misc/tictac.py
|
|
#
|
|
# Copyright (C) 2007 Daniele Tricoli aka Eriol <eriol@mornie.org>
|
|
#
|
|
# This program is free software; you can redistribute it and/or
|
|
# modify it under the terms of the GNU General Public License
|
|
# as published by the Free Software Foundation; either version 2
|
|
# of the License, or (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, write to the Free Software
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
#
|
|
#
|
|
# A present for crash ;)
|
|
##
|
|
|
|
import atexit
|
|
import os.path
|
|
import StringIO
|
|
|
|
from optparse import OptionParser
|
|
from sys import stdin, stdout
|
|
|
|
def backread(afile, buffer_size=1024):
|
|
|
|
afile.seek(0, 2)
|
|
eof = afile.tell()
|
|
|
|
seek_positon = range(eof, 0, -buffer_size) + [0]
|
|
blocks_size = [buffer_size for _ in range(len(seek_positon) - 1)]
|
|
blocks_size.append(seek_positon[-2])
|
|
|
|
blocks = zip(seek_positon, blocks_size)
|
|
|
|
for pos, size in blocks:
|
|
afile.seek(pos)
|
|
data = afile.read(size)
|
|
yield ''.join(reversed(data))
|
|
|
|
def _print(data):
|
|
for l in backread(data):
|
|
stdout.write(l)
|
|
stdout.write('\n')
|
|
|
|
def main():
|
|
interactive = False
|
|
data = StringIO.StringIO()
|
|
usage = '''usage: %prog [FILE]
|
|
|
|
With no FILE read standard input
|
|
'''
|
|
parser = OptionParser(usage)
|
|
(options, args) = parser.parse_args()
|
|
|
|
if len(args) < 1:
|
|
interactive = True
|
|
elif not os.path.isfile(args[0]):
|
|
parser.error('No such file')
|
|
|
|
if interactive:
|
|
while True:
|
|
line = stdin.readline()
|
|
if not line: break
|
|
data.write(line)
|
|
atexit.register(_print, data)
|
|
else:
|
|
fin = open(args[0], 'r')
|
|
try:
|
|
_print(fin)
|
|
finally:
|
|
fin.close()
|
|
|
|
if __name__ == '__main__':
|
|
main()
|