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@79 | 112 | position: absolute; top: 1em; left: 6.5em; |
paulb@78 | 113 | padding: 0.5em; background-color: #0000FF; |
paulb@78 | 114 | } |
paulb@78 | 115 | |
paulb@79 | 116 | .scopes { |
paulb@79 | 117 | display: none; z-index: 2; |
paulb@79 | 118 | position: absolute; top: 1em; left: 0.5em; |
paulb@79 | 119 | padding: 0.5em; background-color: #007700; |
paulb@79 | 120 | } |
paulb@79 | 121 | |
paulb@79 | 122 | .name:hover > .types, |
paulb@79 | 123 | .name:hover > .scopes |
paulb@78 | 124 | { |
paulb@78 | 125 | display: block; |
paulb@78 | 126 | } |
paulb@78 | 127 | |
paulb@78 | 128 | </style> |
paulb@78 | 129 | </head> |
paulb@78 | 130 | <body> |
paulb@78 | 131 | """ |
paulb@78 | 132 | |
paulb@78 | 133 | html_footer = """</body> |
paulb@78 | 134 | </html> |
paulb@78 | 135 | """ |
paulb@78 | 136 | |
paulb@78 | 137 | class Browser(ASTVisitor): |
paulb@78 | 138 | |
paulb@78 | 139 | """ |
paulb@78 | 140 | A browsing visitor for AST nodes. |
paulb@78 | 141 | """ |
paulb@78 | 142 | |
paulb@78 | 143 | def __init__(self, stream): |
paulb@78 | 144 | ASTVisitor.__init__(self) |
paulb@78 | 145 | self.visitor = self |
paulb@78 | 146 | self.stream = stream |
paulb@78 | 147 | |
paulb@78 | 148 | def process(self, module): |
paulb@78 | 149 | self.stream.write(html_header) |
paulb@78 | 150 | self.dispatch(module) |
paulb@78 | 151 | self.stream.write(html_footer) |
paulb@78 | 152 | |
paulb@78 | 153 | def visitModule(self, node): |
paulb@78 | 154 | self.default(node) |
paulb@78 | 155 | |
paulb@79 | 156 | # Statements. |
paulb@79 | 157 | |
paulb@78 | 158 | def visitPass(self, node): |
paulb@78 | 159 | self._keyword("pass") |
paulb@78 | 160 | |
paulb@78 | 161 | def visitClass(self, node): |
paulb@79 | 162 | definition = node._node |
paulb@79 | 163 | structure = definition.expr.types[0].type |
paulb@79 | 164 | self.stream.write("<div class='class' id='%s'>\n" % self._url(structure.full_name())) |
paulb@78 | 165 | self.stream.write("<p>\n") |
paulb@78 | 166 | self._keyword("class") |
paulb@79 | 167 | self._name_start(structure) |
paulb@79 | 168 | self._scopes(definition) |
paulb@79 | 169 | self._name_end() |
paulb@78 | 170 | bases = structure.bases |
paulb@78 | 171 | if bases: |
paulb@79 | 172 | self.stream.write("(") |
paulb@78 | 173 | first = 1 |
paulb@78 | 174 | for base in bases: |
paulb@78 | 175 | if not first: |
paulb@78 | 176 | self.stream.write(",\n") |
paulb@79 | 177 | self._name_start(base) |
paulb@79 | 178 | self._types(base) |
paulb@79 | 179 | self._scopes(base) |
paulb@79 | 180 | self._name_end() |
paulb@78 | 181 | first = 0 |
paulb@78 | 182 | self.stream.write(")") |
paulb@78 | 183 | self.stream.write(":\n") |
paulb@79 | 184 | self._comment(self._text(structure.full_name())) |
paulb@78 | 185 | self.stream.write("</p>\n") |
paulb@78 | 186 | |
paulb@78 | 187 | self.stream.write("<div class='body'>\n") |
paulb@79 | 188 | self._doc(node) |
paulb@79 | 189 | self.dispatch(node.code) |
paulb@78 | 190 | self.stream.write("</div>\n") |
paulb@78 | 191 | self.stream.write("</div>\n") |
paulb@78 | 192 | |
paulb@79 | 193 | def visitAssign(self, node): |
paulb@79 | 194 | self.stream.write("<div class='assign'>\n") |
paulb@79 | 195 | for lvalue in node.nodes: |
paulb@79 | 196 | self.dispatch(lvalue) |
paulb@79 | 197 | self.stream.write("=\n") |
paulb@79 | 198 | self.dispatch(node.expr) |
paulb@79 | 199 | self.stream.write("</div>\n") |
paulb@79 | 200 | |
paulb@79 | 201 | # Expressions. |
paulb@79 | 202 | |
paulb@79 | 203 | def visitTuple(self, node): |
paulb@79 | 204 | self.stream.write("<span class='tuple'>\n") |
paulb@79 | 205 | self.stream.write("(") |
paulb@79 | 206 | self._sequence(node) |
paulb@79 | 207 | self.stream.write(")\n") |
paulb@79 | 208 | self.stream.write("</span>\n") |
paulb@79 | 209 | |
paulb@79 | 210 | visitAssTuple = visitTuple |
paulb@79 | 211 | |
paulb@79 | 212 | def visitList(self, node): |
paulb@79 | 213 | self.stream.write("<span class='list'>\n") |
paulb@79 | 214 | self.stream.write("[") |
paulb@79 | 215 | self._sequence(node) |
paulb@79 | 216 | self.stream.write("]\n") |
paulb@79 | 217 | self.stream.write("</span>\n") |
paulb@79 | 218 | |
paulb@79 | 219 | visitAssList = visitList |
paulb@79 | 220 | |
paulb@79 | 221 | def visitName(self, node): |
paulb@79 | 222 | self._name_start(node._node) |
paulb@79 | 223 | self._types(node._node) |
paulb@79 | 224 | self._scopes(node._node) |
paulb@79 | 225 | self._name_end() |
paulb@79 | 226 | |
paulb@79 | 227 | def visitAssName(self, node): |
paulb@79 | 228 | self._name_start(node._node) |
paulb@79 | 229 | self._types(node._node.expr) |
paulb@79 | 230 | self._scopes(node._node) |
paulb@79 | 231 | self._name_end() |
paulb@79 | 232 | |
paulb@79 | 233 | def visitConst(self, node): |
paulb@79 | 234 | self.stream.write(repr(node.value)) |
paulb@79 | 235 | |
paulb@78 | 236 | # Output preparation methods. |
paulb@78 | 237 | |
paulb@78 | 238 | def _text(self, text): |
paulb@78 | 239 | return text.replace("&", "&").replace("<", "<").replace(">", ">") |
paulb@78 | 240 | |
paulb@78 | 241 | def _attr(self, attr): |
paulb@78 | 242 | return self._text(attr).replace("'", "'").replace('"', """) |
paulb@78 | 243 | |
paulb@78 | 244 | def _url(self, url): |
paulb@78 | 245 | return self._attr(url).replace("#", "%23").replace("-", "%2d") |
paulb@78 | 246 | |
paulb@78 | 247 | def _comment(self, comment): |
paulb@78 | 248 | self.stream.write("<span class='comment'># %s</span>\n" % comment) |
paulb@78 | 249 | |
paulb@78 | 250 | def _keyword(self, kw): |
paulb@78 | 251 | self.stream.write("<span class='keyword'>%s</span> " % kw) |
paulb@78 | 252 | |
paulb@79 | 253 | def _doc(self, node): |
paulb@79 | 254 | if node.doc is not None: |
paulb@79 | 255 | self.stream.write("<div class='doc'>%s</div>\n" % self._text(node.doc)) |
paulb@79 | 256 | |
paulb@79 | 257 | def _sequence(self, node): |
paulb@79 | 258 | first = 1 |
paulb@79 | 259 | for n in node.nodes: |
paulb@79 | 260 | if not first: |
paulb@79 | 261 | self.stream.write(",\n") |
paulb@79 | 262 | self.dispatch(n) |
paulb@79 | 263 | first = 0 |
paulb@78 | 264 | |
paulb@79 | 265 | def _name(self, node): |
paulb@79 | 266 | self.stream.write("<span class='name'>%s</span>\n" % node.name) |
paulb@79 | 267 | |
paulb@79 | 268 | def _name_start(self, node): |
paulb@79 | 269 | self.stream.write("<span class='name'>%s\n" % node.name) |
paulb@79 | 270 | |
paulb@79 | 271 | def _name_end(self): |
paulb@79 | 272 | self.stream.write("</span>\n") |
paulb@79 | 273 | |
paulb@79 | 274 | def _types(self, node): |
paulb@79 | 275 | self.stream.write("<div class='types'>\n") |
paulb@78 | 276 | for type in node.types: |
paulb@79 | 277 | fn = type.type.full_name() |
paulb@79 | 278 | self.stream.write("<div class='type'>") |
paulb@78 | 279 | self.stream.write(self._text(fn)) |
paulb@78 | 280 | self.stream.write("</div>\n") |
paulb@79 | 281 | self.stream.write("</div>\n") |
paulb@79 | 282 | |
paulb@79 | 283 | def _scopes(self, node): |
paulb@79 | 284 | self.stream.write("<div class='scopes'>\n") |
paulb@79 | 285 | if not hasattr(node, "writes") and not hasattr(node, "accesses"): |
paulb@79 | 286 | raise AttributeError, node |
paulb@79 | 287 | for ref in getattr(node, "writes", getattr(node, "accesses", {})).keys(): |
paulb@79 | 288 | fn = ref.full_name() |
paulb@79 | 289 | self.stream.write("<div class='scope'>") |
paulb@79 | 290 | self.stream.write(self._text(fn)) |
paulb@79 | 291 | self.stream.write("</div>\n") |
paulb@79 | 292 | self.stream.write("</div>\n") |
paulb@78 | 293 | |
paulb@57 | 294 | # Convenience functions. |
paulb@57 | 295 | |
paulb@57 | 296 | def view(module, stream=None): |
paulb@57 | 297 | viewer = Viewer(stream or sys.stdout) |
paulb@57 | 298 | viewer.process(module.original) |
paulb@57 | 299 | |
paulb@78 | 300 | def browse(module, stream=None): |
paulb@78 | 301 | browser = Browser(stream or sys.stdout) |
paulb@78 | 302 | browser.process(module.original) |
paulb@78 | 303 | |
paulb@78 | 304 | def makedoc(module, filename): |
paulb@78 | 305 | stream = open(filename, "wb") |
paulb@78 | 306 | try: |
paulb@78 | 307 | browser = Browser(stream) |
paulb@78 | 308 | browser.process(module.original) |
paulb@78 | 309 | finally: |
paulb@78 | 310 | stream.close() |
paulb@78 | 311 | |
paulb@57 | 312 | # vim: tabstop=4 expandtab shiftwidth=4 |