| 1 | #!/usr/bin/env python |
|---|
| 2 | # |
|---|
| 3 | # tictac.py |
|---|
| 4 | # http://mornie.org/code/browser/misc/tictac.py |
|---|
| 5 | # |
|---|
| 6 | # Copyright (C) 2007 Daniele Tricoli aka Eriol <eriol@mornie.org> |
|---|
| 7 | # |
|---|
| 8 | # This program is free software; you can redistribute it and/or |
|---|
| 9 | # modify it under the terms of the GNU General Public License |
|---|
| 10 | # as published by the Free Software Foundation; either version 2 |
|---|
| 11 | # of the License, or (at your option) any later version. |
|---|
| 12 | # |
|---|
| 13 | # This program is distributed in the hope that it will be useful, |
|---|
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | # GNU General Public License for more details. |
|---|
| 17 | # |
|---|
| 18 | # You should have received a copy of the GNU General Public License |
|---|
| 19 | # along with this program; if not, write to the Free Software |
|---|
| 20 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 21 | # |
|---|
| 22 | # |
|---|
| 23 | # A present for crash ;) |
|---|
| 24 | ## |
|---|
| 25 | |
|---|
| 26 | import atexit |
|---|
| 27 | import os.path |
|---|
| 28 | import StringIO |
|---|
| 29 | |
|---|
| 30 | from optparse import OptionParser |
|---|
| 31 | from sys import stdin, stdout |
|---|
| 32 | |
|---|
| 33 | def backread(afile, buffer_size=1024): |
|---|
| 34 | |
|---|
| 35 | afile.seek(0, 2) |
|---|
| 36 | eof = afile.tell() |
|---|
| 37 | |
|---|
| 38 | seek_positon = range(eof, 0, -buffer_size) + [0] |
|---|
| 39 | blocks_size = [buffer_size for _ in range(len(seek_positon) - 1)] |
|---|
| 40 | blocks_size.append(seek_positon[-2]) |
|---|
| 41 | |
|---|
| 42 | blocks = zip(seek_positon, blocks_size) |
|---|
| 43 | |
|---|
| 44 | for pos, size in blocks: |
|---|
| 45 | afile.seek(pos) |
|---|
| 46 | data = afile.read(size) |
|---|
| 47 | yield ''.join(reversed(data)) |
|---|
| 48 | |
|---|
| 49 | def _print(data): |
|---|
| 50 | for l in backread(data): |
|---|
| 51 | stdout.write(l) |
|---|
| 52 | stdout.write('\n') |
|---|
| 53 | |
|---|
| 54 | def main(): |
|---|
| 55 | interactive = False |
|---|
| 56 | data = StringIO.StringIO() |
|---|
| 57 | usage = '''usage: %prog [FILE] |
|---|
| 58 | |
|---|
| 59 | With no FILE read standard input |
|---|
| 60 | ''' |
|---|
| 61 | parser = OptionParser(usage) |
|---|
| 62 | (options, args) = parser.parse_args() |
|---|
| 63 | |
|---|
| 64 | if len(args) < 1: |
|---|
| 65 | interactive = True |
|---|
| 66 | elif not os.path.isfile(args[0]): |
|---|
| 67 | parser.error('No such file') |
|---|
| 68 | |
|---|
| 69 | if interactive: |
|---|
| 70 | while True: |
|---|
| 71 | line = stdin.readline() |
|---|
| 72 | if not line: break |
|---|
| 73 | data.write(line) |
|---|
| 74 | atexit.register(_print, data) |
|---|
| 75 | else: |
|---|
| 76 | fin = open(args[0], 'r') |
|---|
| 77 | try: |
|---|
| 78 | _print(fin) |
|---|
| 79 | finally: |
|---|
| 80 | fin.close() |
|---|
| 81 | |
|---|
| 82 | if __name__ == '__main__': |
|---|
| 83 | main() |
|---|