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@61 | 44 | self.dispatch_only(node) |
paulb@57 | 45 | ASTVisitor.dispatch(self, node) |
paulb@57 | 46 | |
paulb@61 | 47 | def dispatch_only(self, node, every_time=0): |
paulb@61 | 48 | self.print_line(getattr(node, "filename", None), getattr(node, "lineno", None), every_time) |
paulb@61 | 49 | |
paulb@61 | 50 | def print_line(self, filename, lineno, every_time): |
paulb@57 | 51 | last_printed = self.printed_lines.get(filename, 0) |
paulb@61 | 52 | if lineno > last_printed or every_time: |
paulb@57 | 53 | self.stream.write(self.get_line(filename, lineno)) |
paulb@57 | 54 | self.printed_lines[filename] = lineno |
paulb@57 | 55 | |
paulb@57 | 56 | def get_line(self, filename, lineno): |
paulb@57 | 57 | if filename is None or lineno is None: |
paulb@57 | 58 | return "" |
paulb@57 | 59 | |
paulb@57 | 60 | if self.cached_files.has_key(filename): |
paulb@57 | 61 | lines = self.cached_files[filename] |
paulb@57 | 62 | else: |
paulb@57 | 63 | f = open(filename) |
paulb@57 | 64 | try: |
paulb@57 | 65 | self.cached_files[filename] = lines = f.readlines() |
paulb@57 | 66 | finally: |
paulb@57 | 67 | f.close() |
paulb@57 | 68 | |
paulb@57 | 69 | try: |
paulb@57 | 70 | return lines[lineno - 1] |
paulb@57 | 71 | except IndexError: |
paulb@57 | 72 | return "" |
paulb@57 | 73 | |
paulb@61 | 74 | def report(self, exc): |
paulb@61 | 75 | self.stream.write("Exception was:\n\n" + str(exc.exc) + "\n\n") |
paulb@61 | 76 | self.stream.write("Nodes:\n\n") |
paulb@61 | 77 | for node in exc.nodes: |
paulb@61 | 78 | self.stream.write(repr(node) + "\n") |
paulb@61 | 79 | self.dispatch_only(node.original, every_time=1) |
paulb@61 | 80 | self.stream.write("\nOriginal node was:\n\n" + repr(exc.nodes[0].original) + "\n") |
paulb@61 | 81 | self.stream.write("\nSimplified node was:\n\n") |
paulb@61 | 82 | exc.nodes[0].pprint(stream=self.stream) |
paulb@61 | 83 | |
paulb@57 | 84 | # Convenience functions. |
paulb@57 | 85 | |
paulb@57 | 86 | def view(module, stream=None): |
paulb@57 | 87 | viewer = Viewer(stream or sys.stdout) |
paulb@57 | 88 | viewer.process(module.original) |
paulb@57 | 89 | |
paulb@57 | 90 | # vim: tabstop=4 expandtab shiftwidth=4 |