paulb@57 | 1 | #!/usr/bin/env python |
paulb@57 | 2 | |
paulb@57 | 3 | """ |
paulb@57 | 4 | View annotated sources. |
paulb@57 | 5 | |
paulb@57 | 6 | Copyright (C) 2006 Paul Boddie <paul@boddie.org.uk> |
paulb@57 | 7 | |
paulb@57 | 8 | This software is free software; you can redistribute it and/or |
paulb@57 | 9 | modify it under the terms of the GNU General Public License as |
paulb@57 | 10 | published by the Free Software Foundation; either version 2 of |
paulb@57 | 11 | the License, or (at your option) any later version. |
paulb@57 | 12 | |
paulb@57 | 13 | This software is distributed in the hope that it will be useful, |
paulb@57 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@57 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paulb@57 | 16 | GNU General Public License for more details. |
paulb@57 | 17 | |
paulb@57 | 18 | You should have received a copy of the GNU General Public |
paulb@57 | 19 | License along with this library; see the file LICENCE.txt |
paulb@57 | 20 | If not, write to the Free Software Foundation, Inc., |
paulb@57 | 21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@57 | 22 | """ |
paulb@57 | 23 | |
paulb@57 | 24 | from compiler.visitor import ASTVisitor |
paulb@57 | 25 | import sys |
paulb@57 | 26 | |
paulb@57 | 27 | class Viewer(ASTVisitor): |
paulb@57 | 28 | |
paulb@57 | 29 | """ |
paulb@57 | 30 | A viewing visitor for AST nodes. |
paulb@57 | 31 | """ |
paulb@57 | 32 | |
paulb@57 | 33 | def __init__(self, stream): |
paulb@57 | 34 | ASTVisitor.__init__(self) |
paulb@57 | 35 | self.cached_files = {} |
paulb@57 | 36 | self.printed_lines = {} |
paulb@57 | 37 | self.visitor = self |
paulb@57 | 38 | self.stream = stream |
paulb@57 | 39 | |
paulb@57 | 40 | def process(self, module): |
paulb@57 | 41 | self.dispatch(module) |
paulb@57 | 42 | |
paulb@57 | 43 | def dispatch(self, node): |
paulb@57 | 44 | self.print_line(getattr(node, "filename", None), getattr(node, "lineno", None)) |
paulb@57 | 45 | ASTVisitor.dispatch(self, node) |
paulb@57 | 46 | |
paulb@57 | 47 | def print_line(self, filename, lineno): |
paulb@57 | 48 | last_printed = self.printed_lines.get(filename, 0) |
paulb@57 | 49 | if lineno > last_printed: |
paulb@57 | 50 | self.stream.write(self.get_line(filename, lineno)) |
paulb@57 | 51 | self.printed_lines[filename] = lineno |
paulb@57 | 52 | |
paulb@57 | 53 | def get_line(self, filename, lineno): |
paulb@57 | 54 | if filename is None or lineno is None: |
paulb@57 | 55 | return "" |
paulb@57 | 56 | |
paulb@57 | 57 | if self.cached_files.has_key(filename): |
paulb@57 | 58 | lines = self.cached_files[filename] |
paulb@57 | 59 | else: |
paulb@57 | 60 | f = open(filename) |
paulb@57 | 61 | try: |
paulb@57 | 62 | self.cached_files[filename] = lines = f.readlines() |
paulb@57 | 63 | finally: |
paulb@57 | 64 | f.close() |
paulb@57 | 65 | |
paulb@57 | 66 | try: |
paulb@57 | 67 | return lines[lineno - 1] |
paulb@57 | 68 | except IndexError: |
paulb@57 | 69 | return "" |
paulb@57 | 70 | |
paulb@57 | 71 | # Convenience functions. |
paulb@57 | 72 | |
paulb@57 | 73 | def view(module, stream=None): |
paulb@57 | 74 | viewer = Viewer(stream or sys.stdout) |
paulb@57 | 75 | viewer.process(module.original) |
paulb@57 | 76 | |
paulb@57 | 77 | # vim: tabstop=4 expandtab shiftwidth=4 |