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@78 | 84 | # HTML-related output production. |
paulb@78 | 85 | |
paulb@78 | 86 | html_header = """<?xml version="1.0" encoding="iso-8859-15"?> |
paulb@78 | 87 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> |
paulb@78 | 88 | <html xmlns="http://www.w3.org/1999/xhtml"> |
paulb@78 | 89 | <head> |
paulb@78 | 90 | <title>Module</title> |
paulb@78 | 91 | <style type="text/css"> |
paulb@78 | 92 | body { |
paulb@78 | 93 | padding-top: 4em; padding-bottom: 4em; |
paulb@78 | 94 | font-size: 14pt; font-family: monospace; |
paulb@78 | 95 | background-color: black; color: white; |
paulb@78 | 96 | } |
paulb@78 | 97 | |
paulb@78 | 98 | .class { margin-bottom: 1em; } |
paulb@78 | 99 | .function { margin-bottom: 1em; } |
paulb@78 | 100 | .body { padding-left: 2em; } |
paulb@78 | 101 | .keyword { color: yellow; } |
paulb@78 | 102 | .comment { color: blue; } |
paulb@78 | 103 | .ref { color: cyan; } |
paulb@78 | 104 | .ref a { color: cyan; text-decoration: none; } |
paulb@78 | 105 | |
paulb@78 | 106 | .name { |
paulb@78 | 107 | position: relative; |
paulb@78 | 108 | } |
paulb@78 | 109 | |
paulb@78 | 110 | .types { |
paulb@78 | 111 | display: none; z-index: 2; |
paulb@78 | 112 | position: absolute; top: 3em; left: 0.5em; |
paulb@78 | 113 | padding: 0.5em; background-color: #0000FF; |
paulb@78 | 114 | } |
paulb@78 | 115 | |
paulb@78 | 116 | .name:hover > .types |
paulb@78 | 117 | { |
paulb@78 | 118 | display: block; |
paulb@78 | 119 | } |
paulb@78 | 120 | |
paulb@78 | 121 | </style> |
paulb@78 | 122 | </head> |
paulb@78 | 123 | <body> |
paulb@78 | 124 | """ |
paulb@78 | 125 | |
paulb@78 | 126 | html_footer = """</body> |
paulb@78 | 127 | </html> |
paulb@78 | 128 | """ |
paulb@78 | 129 | |
paulb@78 | 130 | class Browser(ASTVisitor): |
paulb@78 | 131 | |
paulb@78 | 132 | """ |
paulb@78 | 133 | A browsing visitor for AST nodes. |
paulb@78 | 134 | """ |
paulb@78 | 135 | |
paulb@78 | 136 | def __init__(self, stream): |
paulb@78 | 137 | ASTVisitor.__init__(self) |
paulb@78 | 138 | self.visitor = self |
paulb@78 | 139 | self.stream = stream |
paulb@78 | 140 | |
paulb@78 | 141 | def process(self, module): |
paulb@78 | 142 | self.stream.write(html_header) |
paulb@78 | 143 | self.dispatch(module) |
paulb@78 | 144 | self.stream.write(html_footer) |
paulb@78 | 145 | |
paulb@78 | 146 | def visitModule(self, node): |
paulb@78 | 147 | self.default(node) |
paulb@78 | 148 | |
paulb@78 | 149 | def visitPass(self, node): |
paulb@78 | 150 | self._keyword("pass") |
paulb@78 | 151 | |
paulb@78 | 152 | def visitClass(self, node): |
paulb@78 | 153 | structure = node._node.expr.types[0].type |
paulb@78 | 154 | self.stream.write("<div class='class' id='%s'>\n" % self._url(structure.full_name)) |
paulb@78 | 155 | self.stream.write("<p>\n") |
paulb@78 | 156 | self._keyword("class") |
paulb@78 | 157 | self._name(structure) |
paulb@78 | 158 | bases = structure.bases |
paulb@78 | 159 | if bases: |
paulb@78 | 160 | self.stream.write("(\n") |
paulb@78 | 161 | first = 1 |
paulb@78 | 162 | for base in bases: |
paulb@78 | 163 | if not first: |
paulb@78 | 164 | self.stream.write(",\n") |
paulb@78 | 165 | self._name_and_types(base) |
paulb@78 | 166 | first = 0 |
paulb@78 | 167 | self.stream.write(")") |
paulb@78 | 168 | self.stream.write(":\n") |
paulb@78 | 169 | self._comment(self._text(structure.full_name)) |
paulb@78 | 170 | self.stream.write("</p>\n") |
paulb@78 | 171 | |
paulb@78 | 172 | self.stream.write("<div class='body'>\n") |
paulb@78 | 173 | self.default(node) |
paulb@78 | 174 | self.stream.write("</div>\n") |
paulb@78 | 175 | self.stream.write("</div>\n") |
paulb@78 | 176 | |
paulb@78 | 177 | # Output preparation methods. |
paulb@78 | 178 | |
paulb@78 | 179 | def _text(self, text): |
paulb@78 | 180 | return text.replace("&", "&").replace("<", "<").replace(">", ">") |
paulb@78 | 181 | |
paulb@78 | 182 | def _attr(self, attr): |
paulb@78 | 183 | return self._text(attr).replace("'", "'").replace('"', """) |
paulb@78 | 184 | |
paulb@78 | 185 | def _url(self, url): |
paulb@78 | 186 | return self._attr(url).replace("#", "%23").replace("-", "%2d") |
paulb@78 | 187 | |
paulb@78 | 188 | def _comment(self, comment): |
paulb@78 | 189 | self.stream.write("<span class='comment'># %s</span>\n" % comment) |
paulb@78 | 190 | |
paulb@78 | 191 | def _keyword(self, kw): |
paulb@78 | 192 | self.stream.write("<span class='keyword'>%s</span> " % kw) |
paulb@78 | 193 | |
paulb@78 | 194 | def _name(self, node): |
paulb@78 | 195 | self.stream.write("<span class='name'>%s</span> " % node.name) |
paulb@78 | 196 | |
paulb@78 | 197 | def _name_and_types(self, node): |
paulb@78 | 198 | self.stream.write("<span class='name'>%s\n<div class='types'>\n" % node.name) |
paulb@78 | 199 | for type in node.types: |
paulb@78 | 200 | fn = type.type.full_name |
paulb@78 | 201 | self.stream.write("<div class='ref'>\n") |
paulb@78 | 202 | self.stream.write(self._text(fn)) |
paulb@78 | 203 | self.stream.write("</div>\n") |
paulb@78 | 204 | self.stream.write("</div>\n</span>\n") |
paulb@78 | 205 | |
paulb@57 | 206 | # Convenience functions. |
paulb@57 | 207 | |
paulb@57 | 208 | def view(module, stream=None): |
paulb@57 | 209 | viewer = Viewer(stream or sys.stdout) |
paulb@57 | 210 | viewer.process(module.original) |
paulb@57 | 211 | |
paulb@78 | 212 | def browse(module, stream=None): |
paulb@78 | 213 | browser = Browser(stream or sys.stdout) |
paulb@78 | 214 | browser.process(module.original) |
paulb@78 | 215 | |
paulb@78 | 216 | def makedoc(module, filename): |
paulb@78 | 217 | stream = open(filename, "wb") |
paulb@78 | 218 | try: |
paulb@78 | 219 | browser = Browser(stream) |
paulb@78 | 220 | browser.process(module.original) |
paulb@78 | 221 | finally: |
paulb@78 | 222 | stream.close() |
paulb@78 | 223 | |
paulb@57 | 224 | # vim: tabstop=4 expandtab shiftwidth=4 |