paul@318 | 1 | #!/usr/bin/env python |
paul@318 | 2 | |
paul@318 | 3 | """ |
paul@318 | 4 | View annotated sources. |
paul@318 | 5 | |
paul@318 | 6 | Copyright (C) 2006, 2007, 2010 Paul Boddie <paul@boddie.org.uk> |
paul@318 | 7 | |
paul@318 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@318 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@318 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@318 | 11 | version. |
paul@318 | 12 | |
paul@318 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@318 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@318 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@318 | 16 | details. |
paul@318 | 17 | |
paul@318 | 18 | You should have received a copy of the GNU General Public License along with |
paul@318 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@318 | 20 | """ |
paul@318 | 21 | |
paul@353 | 22 | from micropython.common import * |
paul@318 | 23 | from micropython.data import * |
paul@353 | 24 | from os.path import exists, extsep, join |
paul@318 | 25 | import sys |
paul@318 | 26 | import os |
paul@318 | 27 | import textwrap |
paul@318 | 28 | |
paul@318 | 29 | # Classes. |
paul@318 | 30 | |
paul@318 | 31 | # HTML-related output production. |
paul@318 | 32 | |
paul@318 | 33 | html_header = """<?xml version="1.0" encoding="iso-8859-15"?> |
paul@318 | 34 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
paul@318 | 35 | <html xmlns="http://www.w3.org/1999/xhtml"> |
paul@318 | 36 | <head> |
paul@353 | 37 | <title>Module: %(full_name)s</title> |
paul@318 | 38 | <style type="text/css"> |
paul@318 | 39 | html { |
paul@318 | 40 | background-color: black; color: white; |
paul@318 | 41 | } |
paul@318 | 42 | |
paul@318 | 43 | body { |
paul@318 | 44 | padding-bottom: 4em; |
paul@318 | 45 | font-size: 14pt; font-family: monospace; |
paul@318 | 46 | background-color: black; color: white; |
paul@318 | 47 | } |
paul@318 | 48 | |
paul@353 | 49 | a { |
paul@353 | 50 | text-decoration: none; |
paul@353 | 51 | } |
paul@353 | 52 | |
paul@318 | 53 | .nowrap { white-space: nowrap; } |
paul@354 | 54 | .label { font-size: smaller; } |
paul@318 | 55 | |
paul@318 | 56 | .class { margin-top: 1em; margin-bottom: 1em; } |
paul@318 | 57 | .function { margin-top: 1em; margin-bottom: 1em; } |
paul@318 | 58 | .body { padding-left: 2em; } |
paul@318 | 59 | .keyword { color: yellow; } |
paul@318 | 60 | .comment { color: blue; } |
paul@318 | 61 | .class-name { color: cyan; } |
paul@318 | 62 | .function-name { color: cyan; } |
paul@318 | 63 | .str { color: #FF00FF; } |
paul@318 | 64 | .doc { color: #FF00FF; margin-top: 1em; margin-bottom: 1em; } |
paul@353 | 65 | |
paul@353 | 66 | .popup { |
paul@353 | 67 | display: none; |
paul@353 | 68 | position: absolute; |
paul@353 | 69 | top: 3ex; left: 0; |
paul@353 | 70 | color: white; |
paul@354 | 71 | border-bottom: 0.5ex solid #000; |
paul@354 | 72 | border-right: 0.5ex solid #000; |
paul@353 | 73 | z-index: 3; |
paul@353 | 74 | } |
paul@353 | 75 | |
paul@353 | 76 | .name { |
paul@353 | 77 | position: relative; |
paul@354 | 78 | background-color: #300; |
paul@353 | 79 | color: white; |
paul@353 | 80 | } |
paul@353 | 81 | |
paul@353 | 82 | .name:hover { |
paul@354 | 83 | background-color: #500; |
paul@354 | 84 | padding-top: 0.5ex; |
paul@354 | 85 | padding-bottom: 0.5ex; |
paul@353 | 86 | z-index: 2; |
paul@353 | 87 | } |
paul@353 | 88 | |
paul@353 | 89 | .name:hover .popup { |
paul@353 | 90 | display: block; |
paul@353 | 91 | } |
paul@318 | 92 | |
paul@354 | 93 | .attrnames, |
paul@354 | 94 | .scope { |
paul@354 | 95 | padding: 0.5em; |
paul@354 | 96 | background-color: #700; |
paul@354 | 97 | } |
paul@354 | 98 | |
paul@318 | 99 | .summary-class { |
paul@354 | 100 | background-color: #040; |
paul@318 | 101 | } |
paul@318 | 102 | |
paul@318 | 103 | .summary-instance { |
paul@354 | 104 | background-color: #00F; |
paul@318 | 105 | } |
paul@318 | 106 | |
paul@318 | 107 | .summary-attr { |
paul@354 | 108 | background-color: #070; |
paul@318 | 109 | } |
paul@318 | 110 | |
paul@318 | 111 | </style> |
paul@318 | 112 | </head> |
paul@318 | 113 | <body> |
paul@318 | 114 | """ |
paul@318 | 115 | |
paul@318 | 116 | html_footer = """</body> |
paul@318 | 117 | </html> |
paul@318 | 118 | """ |
paul@318 | 119 | |
paul@318 | 120 | # Utility classes. |
paul@318 | 121 | |
paul@318 | 122 | class Writer: |
paul@318 | 123 | |
paul@318 | 124 | "A utility class providing useful HTML output methods." |
paul@318 | 125 | |
paul@318 | 126 | # Methods which return strings. |
paul@318 | 127 | |
paul@318 | 128 | def _text(self, text): |
paul@318 | 129 | return text.replace("&", "&").replace("<", "<").replace(">", ">") |
paul@318 | 130 | |
paul@318 | 131 | def _attr(self, attr): |
paul@318 | 132 | return self._text(attr).replace("'", "'").replace('"', """) |
paul@318 | 133 | |
paul@318 | 134 | def _url(self, url): |
paul@318 | 135 | return self._attr(url).replace("#", "%23").replace("-", "%2d") |
paul@318 | 136 | |
paul@318 | 137 | # Methods which write to the stream. |
paul@318 | 138 | |
paul@355 | 139 | def _span_start(self, classes=None): |
paul@355 | 140 | self.stream.write("<span class='%s'>" % (classes or "")) |
paul@355 | 141 | |
paul@353 | 142 | def _span_end(self): |
paul@353 | 143 | self.stream.write("</span>") |
paul@353 | 144 | |
paul@355 | 145 | def _span(self, value, classes=None): |
paul@355 | 146 | self._span_start(classes) |
paul@355 | 147 | self.stream.write(self._text(value)) |
paul@355 | 148 | self._span_end() |
paul@355 | 149 | |
paul@355 | 150 | def _name_start(self, classes=None): |
paul@355 | 151 | self._span_start(classes or "name") |
paul@355 | 152 | |
paul@355 | 153 | _name_end = _span_end |
paul@355 | 154 | |
paul@355 | 155 | def _name(self, name, classes=None): |
paul@355 | 156 | self._name_start(classes) |
paul@355 | 157 | self.stream.write(self._text(name)) |
paul@355 | 158 | self._name_end() |
paul@355 | 159 | |
paul@355 | 160 | def _popup_start(self): |
paul@355 | 161 | self._span_start("popup") |
paul@355 | 162 | |
paul@355 | 163 | _popup_end = _span_end |
paul@355 | 164 | |
paul@318 | 165 | def _comment(self, comment): |
paul@355 | 166 | self._span("# %s" % comment, "comment") |
paul@355 | 167 | self.stream.write("\n") |
paul@318 | 168 | |
paul@353 | 169 | def _reserved(self, token, classes, leading=0, trailing=1): |
paul@353 | 170 | if leading: |
paul@353 | 171 | self.stream.write(" ") |
paul@355 | 172 | self._span(token, classes) |
paul@353 | 173 | if trailing: |
paul@353 | 174 | self.stream.write(" ") |
paul@353 | 175 | |
paul@353 | 176 | def _keyword(self, kw, leading=0, trailing=1): |
paul@353 | 177 | self._reserved(kw, "keyword", leading, trailing) |
paul@353 | 178 | |
paul@353 | 179 | def _op(self, symbol, leading=0, trailing=1): |
paul@353 | 180 | self._reserved(symbol, "operator", leading, trailing) |
paul@318 | 181 | |
paul@318 | 182 | def _doc(self, node): |
paul@318 | 183 | if node.doc is not None: |
paul@353 | 184 | self.stream.write("<pre class='doc'>") |
paul@318 | 185 | self.stream.write('"""') |
paul@318 | 186 | output = textwrap.dedent(node.doc.replace('"""', '\\"\\"\\"')) |
paul@318 | 187 | self.stream.write(self._text(output)) |
paul@318 | 188 | self.stream.write('"""') |
paul@318 | 189 | self.stream.write("</pre>\n") |
paul@318 | 190 | |
paul@321 | 191 | def _summary_link(self, module_name, full_name, name, classes=None): |
paul@321 | 192 | self._name_link("%s-summary" % module_name, full_name, name, classes) |
paul@320 | 193 | |
paul@321 | 194 | def _object_name_def(self, module, obj, classes=None): |
paul@320 | 195 | |
paul@320 | 196 | """ |
paul@321 | 197 | Link to the summary for 'module' using 'obj'. The optional 'classes' |
paul@321 | 198 | can be used to customise the CSS classes employed. |
paul@320 | 199 | """ |
paul@320 | 200 | |
paul@321 | 201 | self._summary_link(module.full_name(), obj.full_name(), obj.name, classes) |
paul@320 | 202 | |
paul@353 | 203 | def _name_link(self, module_name, full_name, name, classes=None): |
paul@353 | 204 | self.stream.write("<a class='%s' href='%s%sxhtml#%s'>%s</a>" % ( |
paul@353 | 205 | classes or "name", module_name, os.path.extsep, |
paul@353 | 206 | self._attr(full_name), self._text(name))) |
paul@353 | 207 | |
paul@353 | 208 | def _scope(self, scope): |
paul@354 | 209 | self.stream.write("<div class='scope'><span class='label'>scope</span><br />%s</div>\n" % scope) |
paul@354 | 210 | |
paul@354 | 211 | def _assname(self, name, node): |
paul@355 | 212 | self._span_start("assname") |
paul@355 | 213 | if hasattr(node, "_attrnames") and node._attrnames[name]: |
paul@355 | 214 | attrnames = node._attrnames[name] |
paul@354 | 215 | self._name_start() |
paul@354 | 216 | self.stream.write(name) |
paul@354 | 217 | self._popup_start() |
paul@355 | 218 | self._attrnames(attrnames) |
paul@354 | 219 | self._popup_end() |
paul@354 | 220 | self._name_end() |
paul@354 | 221 | else: |
paul@355 | 222 | self._span(name) |
paul@355 | 223 | self._span_end() |
paul@354 | 224 | |
paul@354 | 225 | def _attrnames(self, attrnames): |
paul@354 | 226 | if not attrnames: |
paul@354 | 227 | return |
paul@354 | 228 | |
paul@354 | 229 | self.stream.write("<div class='attrnames'><span class='label'>attributes</span><br />") |
paul@354 | 230 | first = 1 |
paul@354 | 231 | for attrname in attrnames: |
paul@354 | 232 | if not first: |
paul@354 | 233 | self.stream.write("<br />") |
paul@354 | 234 | self.stream.write(attrname) |
paul@354 | 235 | first = 0 |
paul@354 | 236 | self.stream.write("</div>\n") |
paul@353 | 237 | |
paul@318 | 238 | # Summary classes. |
paul@318 | 239 | |
paul@320 | 240 | class Summary(Writer): |
paul@318 | 241 | |
paul@319 | 242 | "Summarise classes and attributes in modules." |
paul@319 | 243 | |
paul@319 | 244 | def __init__(self, module): |
paul@319 | 245 | self.module = module |
paul@318 | 246 | |
paul@319 | 247 | def to_stream(self, stream): |
paul@319 | 248 | |
paul@319 | 249 | "Write the summary to the given 'stream'." |
paul@319 | 250 | |
paul@319 | 251 | self.stream = stream |
paul@318 | 252 | self._init_details() |
paul@353 | 253 | self.stream.write(html_header % {"full_name" : self.module.full_name()}) |
paul@319 | 254 | self._write_classes(self.module) |
paul@318 | 255 | self.stream.write(html_footer) |
paul@318 | 256 | |
paul@318 | 257 | def _write_classes(self, module): |
paul@318 | 258 | self.stream.write("<table cellspacing='5' cellpadding='5'>\n") |
paul@318 | 259 | |
paul@318 | 260 | all_classes = {} |
paul@318 | 261 | |
paul@318 | 262 | for obj in self.module.all_objects: |
paul@318 | 263 | if isinstance(obj, Class): |
paul@318 | 264 | all_classes[obj.name] = obj |
paul@318 | 265 | |
paul@318 | 266 | all_class_names = all_classes.keys() |
paul@318 | 267 | all_class_names.sort() |
paul@318 | 268 | |
paul@318 | 269 | for name in all_class_names: |
paul@318 | 270 | self._write_class(all_classes[name]) |
paul@318 | 271 | |
paul@318 | 272 | self.stream.write("</table>\n") |
paul@318 | 273 | |
paul@318 | 274 | def _write_class(self, obj): |
paul@318 | 275 | |
paul@318 | 276 | # Write the class... |
paul@318 | 277 | |
paul@318 | 278 | self.stream.write("<tbody class='class'>\n") |
paul@318 | 279 | self.stream.write("<tr>\n") |
paul@318 | 280 | self.stream.write("<th class='summary-class' id='%s'>\n" % obj.full_name()) |
paul@318 | 281 | self._keyword("class") |
paul@318 | 282 | self.stream.write(obj.name) |
paul@318 | 283 | self.stream.write("</th>\n") |
paul@318 | 284 | |
paul@318 | 285 | # ...and all known attribute names. |
paul@318 | 286 | |
paul@318 | 287 | obj_attributes = obj.all_attribute_names() |
paul@318 | 288 | |
paul@318 | 289 | for name in self.attribute_names: |
paul@318 | 290 | if name in obj_attributes: |
paul@318 | 291 | self.stream.write("<th class='summary-attr'>%s</th>\n" % self._text(name)) |
paul@318 | 292 | else: |
paul@318 | 293 | self.stream.write("<th></th>\n") |
paul@318 | 294 | self.stream.write("</tr>\n") |
paul@318 | 295 | |
paul@318 | 296 | self.stream.write("</tbody>\n") |
paul@318 | 297 | |
paul@318 | 298 | def _init_details(self): |
paul@318 | 299 | names = set() |
paul@318 | 300 | |
paul@318 | 301 | # Visit all classes. |
paul@318 | 302 | |
paul@318 | 303 | for obj in self.module.all_objects: |
paul@318 | 304 | if isinstance(obj, Class): |
paul@318 | 305 | names.update(obj.all_attribute_names()) |
paul@318 | 306 | |
paul@318 | 307 | self.attribute_names = list(names) |
paul@318 | 308 | self.attribute_names.sort() |
paul@318 | 309 | |
paul@320 | 310 | # Source code classes. |
paul@320 | 311 | |
paul@320 | 312 | class AnnotatedSource(ASTVisitor, Writer): |
paul@320 | 313 | |
paul@320 | 314 | "A module source code browser." |
paul@320 | 315 | |
paul@320 | 316 | def __init__(self, module): |
paul@320 | 317 | ASTVisitor.__init__(self) |
paul@320 | 318 | self.visitor = self |
paul@320 | 319 | self.module = module |
paul@320 | 320 | |
paul@320 | 321 | def to_stream(self, stream): |
paul@320 | 322 | |
paul@320 | 323 | "Write the annotated code to the given 'stream'." |
paul@320 | 324 | |
paul@320 | 325 | self.stream = stream |
paul@353 | 326 | self.stream.write(html_header % {"full_name" : self.module.full_name()}) |
paul@353 | 327 | self.dispatch(self.module.astnode) |
paul@320 | 328 | self.stream.write(html_footer) |
paul@320 | 329 | |
paul@320 | 330 | def visitModule(self, node): |
paul@320 | 331 | self.default(node) |
paul@320 | 332 | |
paul@320 | 333 | # Statements. |
paul@320 | 334 | |
paul@320 | 335 | def visitAssert(self, node): |
paul@320 | 336 | self.stream.write("<div class='assert nowrap'>\n") |
paul@320 | 337 | self._keyword("assert") |
paul@320 | 338 | self.dispatch(node.test) |
paul@320 | 339 | if node.fail: |
paul@320 | 340 | self.stream.write(", ") |
paul@320 | 341 | self.dispatch(node.fail) |
paul@320 | 342 | self.stream.write("</div>\n") |
paul@320 | 343 | |
paul@320 | 344 | def visitAssign(self, node): |
paul@320 | 345 | self.stream.write("<div class='assign nowrap'>\n") |
paul@320 | 346 | for lvalue in node.nodes: |
paul@320 | 347 | self.dispatch(lvalue) |
paul@353 | 348 | self.stream.write(" = ") |
paul@320 | 349 | self.dispatch(node.expr) |
paul@320 | 350 | self.stream.write("</div>\n") |
paul@320 | 351 | |
paul@320 | 352 | def visitAugAssign(self, node): |
paul@320 | 353 | self.stream.write("<div class='augassign nowrap'>\n") |
paul@320 | 354 | self.dispatch(node.node) |
paul@353 | 355 | self.stream.write(" %s " % node.op) |
paul@320 | 356 | self.dispatch(node.expr) |
paul@320 | 357 | self.stream.write("</div>\n") |
paul@320 | 358 | |
paul@320 | 359 | def visitBreak(self, node): |
paul@320 | 360 | self.stream.write("<div class='break nowrap'>\n") |
paul@320 | 361 | self._keyword("break") |
paul@320 | 362 | self.stream.write("</div>\n") |
paul@320 | 363 | |
paul@320 | 364 | def visitClass(self, node): |
paul@353 | 365 | if not used_by_unit(node): |
paul@353 | 366 | return |
paul@320 | 367 | |
paul@320 | 368 | # Use inspected details where possible. |
paul@320 | 369 | |
paul@354 | 370 | if hasattr(node, "unit"): |
paul@354 | 371 | cls = node.unit |
paul@320 | 372 | bases = cls.bases |
paul@320 | 373 | self.stream.write("<div class='class nowrap' id='%s'>\n" % cls.full_name()) |
paul@320 | 374 | else: |
paul@320 | 375 | print "Warning: class %s not recognised!" % node.name |
paul@320 | 376 | return |
paul@320 | 377 | |
paul@320 | 378 | # Write the declaration line. |
paul@320 | 379 | |
paul@320 | 380 | self.stream.write("<div>\n") |
paul@320 | 381 | self._keyword("class") |
paul@321 | 382 | self._object_name_def(self.module, cls, "class-name") |
paul@320 | 383 | |
paul@320 | 384 | # Suppress the "object" class appearing alone. |
paul@320 | 385 | |
paul@320 | 386 | if bases and not (len(bases) == 1 and bases[0].name == "object"): |
paul@320 | 387 | self.stream.write("(") |
paul@320 | 388 | first = 1 |
paul@320 | 389 | for base in bases: |
paul@320 | 390 | if not first: |
paul@321 | 391 | self.stream.write(", ") |
paul@320 | 392 | |
paul@320 | 393 | self._object_name_def(base.module, base) |
paul@320 | 394 | |
paul@320 | 395 | first = 0 |
paul@320 | 396 | self.stream.write(")") |
paul@320 | 397 | |
paul@320 | 398 | self.stream.write(":\n") |
paul@320 | 399 | self.stream.write("</div>\n") |
paul@320 | 400 | |
paul@320 | 401 | # Write the docstring and class body. |
paul@320 | 402 | |
paul@320 | 403 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 404 | self._doc(node) |
paul@320 | 405 | self.dispatch(node.code) |
paul@320 | 406 | self.stream.write("</div>\n") |
paul@320 | 407 | self.stream.write("</div>\n") |
paul@320 | 408 | |
paul@320 | 409 | def visitContinue(self, node): |
paul@320 | 410 | self.stream.write("<div class='continue nowrap'>\n") |
paul@320 | 411 | self._keyword("continue") |
paul@320 | 412 | self.stream.write("</div>\n") |
paul@320 | 413 | |
paul@320 | 414 | def visitDiscard(self, node): |
paul@320 | 415 | self.stream.write("<div class='discard nowrap'>\n") |
paul@320 | 416 | self.default(node) |
paul@320 | 417 | self.stream.write("</div>\n") |
paul@320 | 418 | |
paul@320 | 419 | def visitFor(self, node): |
paul@320 | 420 | self.stream.write("<div class='if nowrap'>\n") |
paul@320 | 421 | self.stream.write("<div>\n") |
paul@320 | 422 | self._keyword("for") |
paul@320 | 423 | self.dispatch(node.assign) |
paul@353 | 424 | self._keyword("in", 1) |
paul@320 | 425 | self.dispatch(node.list) |
paul@320 | 426 | self.stream.write(":\n") |
paul@320 | 427 | self.stream.write("</div>\n") |
paul@320 | 428 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 429 | self.dispatch(node.body) |
paul@320 | 430 | self.stream.write("</div>\n") |
paul@320 | 431 | if node.else_ is not None: |
paul@320 | 432 | self.stream.write("<div>\n") |
paul@353 | 433 | self._keyword("else", trailing=0) |
paul@320 | 434 | self.stream.write(":\n") |
paul@320 | 435 | self.stream.write("</div>\n") |
paul@320 | 436 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 437 | self.dispatch(node.else_) |
paul@320 | 438 | self.stream.write("</div>\n") |
paul@320 | 439 | self.stream.write("</div>\n") |
paul@320 | 440 | |
paul@320 | 441 | def visitFrom(self, node): |
paul@320 | 442 | self.stream.write("<div class='from nowrap'>\n") |
paul@320 | 443 | self._keyword("from") |
paul@320 | 444 | self._name(node.modname) |
paul@353 | 445 | self._keyword("import", 1) |
paul@320 | 446 | first = 1 |
paul@320 | 447 | for (name, alias), _name in map(None, node.names, node._names): |
paul@320 | 448 | if not first: |
paul@321 | 449 | self.stream.write(", ") |
paul@320 | 450 | if alias: |
paul@320 | 451 | self.stream.write(name + " ") |
paul@353 | 452 | self._keyword("as", 1) |
paul@320 | 453 | self._name(alias or name) |
paul@320 | 454 | first = 0 |
paul@320 | 455 | self.stream.write("</div>\n") |
paul@320 | 456 | |
paul@320 | 457 | def visitFunction(self, node): |
paul@353 | 458 | if not used_by_unit(node): |
paul@353 | 459 | return |
paul@353 | 460 | |
paul@354 | 461 | if hasattr(node, "unit"): |
paul@354 | 462 | fn = node.unit |
paul@320 | 463 | self.stream.write("<div class='function nowrap' id='%s'>\n" % fn.full_name()) |
paul@320 | 464 | else: |
paul@320 | 465 | print "Warning: function %s not recognised!" % node.name |
paul@320 | 466 | return |
paul@320 | 467 | |
paul@320 | 468 | # Write the declaration line. |
paul@320 | 469 | |
paul@320 | 470 | self.stream.write("<div>\n") |
paul@320 | 471 | self._keyword("def") |
paul@321 | 472 | self._object_name_def(self.module, fn, "function-name") |
paul@320 | 473 | |
paul@320 | 474 | self.stream.write("(") |
paul@354 | 475 | self._parameters(fn, node) |
paul@320 | 476 | self.stream.write(")") |
paul@320 | 477 | self.stream.write(":\n") |
paul@320 | 478 | self.stream.write("</div>\n") |
paul@320 | 479 | |
paul@320 | 480 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 481 | self._doc(node) |
paul@320 | 482 | self.dispatch(node.code) |
paul@320 | 483 | self.stream.write("</div>\n") |
paul@320 | 484 | self.stream.write("</div>\n") |
paul@320 | 485 | |
paul@320 | 486 | def visitGlobal(self, node): |
paul@320 | 487 | self.stream.write("<div class='global nowrap'>\n") |
paul@320 | 488 | self._keyword("global") |
paul@320 | 489 | first = 1 |
paul@320 | 490 | for name in node.names: |
paul@320 | 491 | if not first: |
paul@321 | 492 | self.stream.write(", ") |
paul@320 | 493 | self.stream.write(name) |
paul@320 | 494 | first = 0 |
paul@320 | 495 | self.stream.write("</div>\n") |
paul@320 | 496 | |
paul@320 | 497 | def visitIf(self, node): |
paul@320 | 498 | self.stream.write("<div class='if nowrap'>\n") |
paul@320 | 499 | first = 1 |
paul@320 | 500 | for compare, stmt in node.tests: |
paul@320 | 501 | self.stream.write("<div>\n") |
paul@320 | 502 | if first: |
paul@320 | 503 | self._keyword("if") |
paul@320 | 504 | else: |
paul@320 | 505 | self._keyword("elif") |
paul@320 | 506 | self.dispatch(compare) |
paul@320 | 507 | self.stream.write(":\n") |
paul@320 | 508 | self.stream.write("</div>\n") |
paul@320 | 509 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 510 | self.dispatch(stmt) |
paul@320 | 511 | self.stream.write("</div>\n") |
paul@320 | 512 | first = 0 |
paul@320 | 513 | if node.else_ is not None: |
paul@320 | 514 | self.stream.write("<div>\n") |
paul@353 | 515 | self._keyword("else", trailing=0) |
paul@353 | 516 | self.stream.write(":\n") |
paul@353 | 517 | self.stream.write("</div>\n") |
paul@353 | 518 | self.stream.write("<div class='body nowrap'>\n") |
paul@353 | 519 | self.dispatch(node.else_) |
paul@353 | 520 | self.stream.write("</div>\n") |
paul@353 | 521 | self.stream.write("</div>\n") |
paul@353 | 522 | |
paul@353 | 523 | def visitImport(self, node): |
paul@353 | 524 | self.stream.write("<div class='import nowrap'>\n") |
paul@353 | 525 | self._keyword("import") |
paul@353 | 526 | first = 1 |
paul@353 | 527 | for name, alias in node.names: |
paul@353 | 528 | if not first: |
paul@353 | 529 | self.stream.write(",\n") |
paul@353 | 530 | if alias: |
paul@353 | 531 | self.stream.write(name + " ") |
paul@353 | 532 | self._keyword("as", 1) |
paul@353 | 533 | self._name(alias or name) |
paul@353 | 534 | first = 0 |
paul@353 | 535 | self.stream.write("</div>\n") |
paul@353 | 536 | |
paul@353 | 537 | def visitPass(self, node): |
paul@353 | 538 | self.stream.write("<div class='pass nowrap'>\n") |
paul@353 | 539 | self._keyword("pass") |
paul@353 | 540 | self.stream.write("</div>\n") |
paul@353 | 541 | |
paul@353 | 542 | def visitPrint(self, node): |
paul@353 | 543 | self.stream.write("<div class='print nowrap'>\n") |
paul@353 | 544 | self._keyword("print") |
paul@353 | 545 | if node.dest is not None: |
paul@353 | 546 | self.stream.write(">>\n") |
paul@353 | 547 | self.dispatch(node.dest) |
paul@353 | 548 | for n in node.nodes: |
paul@353 | 549 | self.dispatch(n) |
paul@353 | 550 | self.stream.write(",\n") |
paul@353 | 551 | self.stream.write("</div>\n") |
paul@353 | 552 | |
paul@353 | 553 | def visitPrintnl(self, node): |
paul@353 | 554 | self.stream.write("<div class='printnl nowrap'>\n") |
paul@353 | 555 | self._keyword("print") |
paul@353 | 556 | if node.dest is not None: |
paul@353 | 557 | self.stream.write(">>\n") |
paul@353 | 558 | self.dispatch(node.dest) |
paul@353 | 559 | first = 1 |
paul@353 | 560 | for n in node.nodes: |
paul@353 | 561 | if not first: |
paul@353 | 562 | self.stream.write(",\n") |
paul@353 | 563 | self.dispatch(n) |
paul@353 | 564 | first = 0 |
paul@353 | 565 | self.stream.write("</div>\n") |
paul@353 | 566 | |
paul@353 | 567 | def visitRaise(self, node): |
paul@353 | 568 | self.stream.write("<div class='raise nowrap'>\n") |
paul@353 | 569 | self._keyword("raise") |
paul@353 | 570 | self.dispatch(node.expr1) |
paul@353 | 571 | if node.expr2 is not None: |
paul@353 | 572 | self.stream.write(",\n") |
paul@353 | 573 | self.dispatch(node.expr2) |
paul@353 | 574 | if node.expr3 is not None: |
paul@353 | 575 | self.stream.write(",\n") |
paul@353 | 576 | self.dispatch(node.expr3) |
paul@353 | 577 | self.stream.write("</div>\n") |
paul@353 | 578 | |
paul@353 | 579 | def visitReturn(self, node): |
paul@353 | 580 | self.stream.write("<div class='return nowrap'>\n") |
paul@353 | 581 | self._keyword("return") |
paul@353 | 582 | self.dispatch(node.value) |
paul@353 | 583 | self.stream.write("</div>\n") |
paul@353 | 584 | |
paul@353 | 585 | def visitStmt(self, node): |
paul@353 | 586 | self.stream.write("<div class='stmt nowrap'>\n") |
paul@353 | 587 | self.default(node) |
paul@353 | 588 | self.stream.write("</div>\n") |
paul@353 | 589 | |
paul@353 | 590 | def visitTryExcept(self, node): |
paul@353 | 591 | self.stream.write("<div class='tryexcept nowrap'>\n") |
paul@353 | 592 | self.stream.write("<div>\n") |
paul@353 | 593 | self._keyword("try", trailing=0) |
paul@353 | 594 | self.stream.write(":\n") |
paul@353 | 595 | self.stream.write("</div>\n") |
paul@353 | 596 | self.stream.write("<div class='body nowrap'>\n") |
paul@353 | 597 | self.dispatch(node.body) |
paul@353 | 598 | self.stream.write("</div>\n") |
paul@353 | 599 | for spec, assign, statement in node.handlers: |
paul@353 | 600 | self.stream.write("<div>\n") |
paul@353 | 601 | self._keyword("except") |
paul@353 | 602 | if spec is not None: |
paul@353 | 603 | self.dispatch(spec) |
paul@353 | 604 | if assign is not None: |
paul@353 | 605 | self.stream.write(",\n") |
paul@353 | 606 | self.dispatch(assign) |
paul@353 | 607 | self.stream.write(":\n") |
paul@353 | 608 | self.stream.write("</div>\n") |
paul@353 | 609 | self.stream.write("<div class='body nowrap'>\n") |
paul@353 | 610 | self.dispatch(statement) |
paul@353 | 611 | self.stream.write("</div>\n") |
paul@353 | 612 | if node.else_ is not None: |
paul@353 | 613 | self.stream.write("<div>\n") |
paul@353 | 614 | self._keyword("else", trailing=0) |
paul@353 | 615 | self.stream.write(":\n") |
paul@353 | 616 | self.stream.write("</div>\n") |
paul@353 | 617 | self.stream.write("<div class='body nowrap'>\n") |
paul@353 | 618 | self.dispatch(node.else_) |
paul@353 | 619 | self.stream.write("</div>\n") |
paul@353 | 620 | self.stream.write("</div>\n") |
paul@353 | 621 | |
paul@353 | 622 | def visitTryFinally(self, node): |
paul@353 | 623 | self.stream.write("<div class='tryfinally nowrap'>\n") |
paul@353 | 624 | self.stream.write("<div>\n") |
paul@353 | 625 | self._keyword("try", trailing=0) |
paul@353 | 626 | self.stream.write(":\n") |
paul@353 | 627 | self.stream.write("</div>\n") |
paul@353 | 628 | self.stream.write("<div class='body nowrap'>\n") |
paul@353 | 629 | self.dispatch(node.body) |
paul@353 | 630 | self.stream.write("</div>\n") |
paul@353 | 631 | self.stream.write("<div>\n") |
paul@353 | 632 | self._keyword("finally", trailing=0) |
paul@353 | 633 | self.stream.write(":\n") |
paul@353 | 634 | self.stream.write("</div>\n") |
paul@353 | 635 | self.stream.write("<div class='body nowrap'>\n") |
paul@353 | 636 | self.dispatch(node.final) |
paul@353 | 637 | self.stream.write("</div>\n") |
paul@353 | 638 | self.stream.write("</div>\n") |
paul@353 | 639 | |
paul@353 | 640 | def visitWhile(self, node): |
paul@353 | 641 | self.stream.write("<div class='while nowrap'>\n") |
paul@353 | 642 | self.stream.write("<div>\n") |
paul@353 | 643 | self._keyword("while") |
paul@353 | 644 | self.dispatch(node.test) |
paul@353 | 645 | self.stream.write(":\n") |
paul@353 | 646 | self.stream.write("</div>\n") |
paul@353 | 647 | self.stream.write("<div class='body nowrap'>\n") |
paul@353 | 648 | self.dispatch(node.body) |
paul@353 | 649 | self.stream.write("</div>\n") |
paul@353 | 650 | if node.else_ is not None: |
paul@353 | 651 | self.stream.write("<div>\n") |
paul@353 | 652 | self._keyword("else", trailing=0) |
paul@320 | 653 | self.stream.write(":\n") |
paul@320 | 654 | self.stream.write("</div>\n") |
paul@320 | 655 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 656 | self.dispatch(node.else_) |
paul@320 | 657 | self.stream.write("</div>\n") |
paul@320 | 658 | self.stream.write("</div>\n") |
paul@320 | 659 | |
paul@353 | 660 | # Expression-related helper methods. |
paul@353 | 661 | |
paul@353 | 662 | def _visitBitBinary(self, node, name, symbol): |
paul@355 | 663 | self._span_start(name) |
paul@353 | 664 | first = 1 |
paul@353 | 665 | for node in node.nodes: |
paul@353 | 666 | if not first: |
paul@353 | 667 | self._op(symbol, 1) |
paul@353 | 668 | self.dispatch(node) |
paul@353 | 669 | first = 0 |
paul@355 | 670 | self._span_end() |
paul@353 | 671 | |
paul@353 | 672 | def _visitBinary(self, node, name, symbol): |
paul@355 | 673 | self._span_start(name) |
paul@353 | 674 | self.dispatch(node.left) |
paul@353 | 675 | self._op(symbol, 1) |
paul@353 | 676 | self.dispatch(node.right) |
paul@355 | 677 | self._span_end() |
paul@353 | 678 | |
paul@353 | 679 | def _visitUnary(self, node, name, symbol): |
paul@355 | 680 | self._span_start(name) |
paul@353 | 681 | self._op(symbol) |
paul@353 | 682 | self.dispatch(node.expr) |
paul@355 | 683 | self._span_end() |
paul@353 | 684 | |
paul@353 | 685 | # Expressions. |
paul@353 | 686 | |
paul@353 | 687 | def visitAdd(self, node): |
paul@353 | 688 | self._visitBinary(node, "add", "+") |
paul@353 | 689 | |
paul@353 | 690 | def visitAnd(self, node): |
paul@355 | 691 | self._span_start("and") |
paul@353 | 692 | first = 1 |
paul@353 | 693 | for n in node.nodes: |
paul@353 | 694 | if not first: |
paul@353 | 695 | self._keyword("and", 1) |
paul@353 | 696 | self.dispatch(n) |
paul@353 | 697 | first = 0 |
paul@355 | 698 | self._span_end() |
paul@353 | 699 | |
paul@353 | 700 | def visitAssAttr(self, node): |
paul@355 | 701 | self._span_start("assattr") |
paul@353 | 702 | self.dispatch(node.expr) |
paul@355 | 703 | self._span_start("attr") |
paul@353 | 704 | self.stream.write(".") |
paul@353 | 705 | self._name(node.attrname) |
paul@355 | 706 | self._span_end() |
paul@355 | 707 | self._span_end() |
paul@353 | 708 | |
paul@353 | 709 | def visitAssList(self, node): |
paul@355 | 710 | self._span_start("list") |
paul@353 | 711 | self.stream.write("[") |
paul@353 | 712 | self._sequence(node) |
paul@353 | 713 | self.stream.write("]") |
paul@355 | 714 | self._span_end() |
paul@353 | 715 | |
paul@353 | 716 | def visitAssName(self, node): |
paul@354 | 717 | self._assname(node.name, node) |
paul@353 | 718 | |
paul@353 | 719 | def visitAssTuple(self, node): |
paul@355 | 720 | self._span_start("tuple") |
paul@353 | 721 | self.stream.write("(") |
paul@353 | 722 | self._sequence(node) |
paul@353 | 723 | self.stream.write(")") |
paul@355 | 724 | self._span_end() |
paul@353 | 725 | |
paul@353 | 726 | def visitBitand(self, node): |
paul@353 | 727 | self._visitBitBinary(node, "bitand", "&") |
paul@353 | 728 | |
paul@353 | 729 | def visitCallFunc(self, node): |
paul@355 | 730 | self._span_start("callfunc") |
paul@353 | 731 | self.dispatch(node.node) |
paul@355 | 732 | self._span_start("call") |
paul@353 | 733 | self.stream.write("(") |
paul@353 | 734 | first = 1 |
paul@353 | 735 | for arg in node.args: |
paul@353 | 736 | if not first: |
paul@353 | 737 | self.stream.write(", ") |
paul@353 | 738 | self.dispatch(arg) |
paul@353 | 739 | first = 0 |
paul@353 | 740 | if node.star_args is not None: |
paul@353 | 741 | if not first: |
paul@353 | 742 | self.stream.write(", *") |
paul@353 | 743 | self.dispatch(node.star_args) |
paul@353 | 744 | first = 0 |
paul@353 | 745 | if node.dstar_args is not None: |
paul@353 | 746 | if not first: |
paul@353 | 747 | self.stream.write(", **") |
paul@353 | 748 | self.dispatch(node.dstar_args) |
paul@353 | 749 | first = 0 |
paul@353 | 750 | self.stream.write(")") |
paul@355 | 751 | self._span_end() |
paul@355 | 752 | self._span_end() |
paul@353 | 753 | |
paul@353 | 754 | def visitCompare(self, node): |
paul@355 | 755 | self._span_start("compare") |
paul@353 | 756 | self.dispatch(node.expr) |
paul@353 | 757 | for op_name, expr in node.ops: |
paul@353 | 758 | self._op(op_name, 1) |
paul@353 | 759 | self.dispatch(expr) |
paul@355 | 760 | self._span_end() |
paul@353 | 761 | |
paul@353 | 762 | def visitConst(self, node): |
paul@353 | 763 | if isinstance(node.value, (str, unicode)): |
paul@355 | 764 | self._span_start("str") |
paul@353 | 765 | self.stream.write(self._text(repr(node.value))) |
paul@353 | 766 | if isinstance(node.value, (str, unicode)): |
paul@355 | 767 | self._span_end() |
paul@353 | 768 | |
paul@353 | 769 | def visitDict(self, node): |
paul@355 | 770 | self._span_start("dict") |
paul@353 | 771 | self.stream.write("{") |
paul@353 | 772 | self._mapping(node) |
paul@353 | 773 | self.stream.write("}") |
paul@355 | 774 | self._span_end() |
paul@353 | 775 | |
paul@353 | 776 | def visitDiv(self, node): |
paul@353 | 777 | self._visitBinary(node, "div", "/") |
paul@353 | 778 | |
paul@353 | 779 | def visitFloorDiv(self, node): |
paul@353 | 780 | self._visitBinary(node, "floordiv", "//") |
paul@353 | 781 | |
paul@353 | 782 | def visitGetattr(self, node): |
paul@355 | 783 | self._span_start("getattr") |
paul@353 | 784 | self.dispatch(node.expr) |
paul@355 | 785 | self._span_start("attr") |
paul@353 | 786 | self.stream.write(".") |
paul@353 | 787 | self._name(node.attrname) |
paul@355 | 788 | self._span_end() |
paul@355 | 789 | self._span_end() |
paul@353 | 790 | |
paul@353 | 791 | def visitKeyword(self, node): |
paul@355 | 792 | self._span_start("keyword-arg") |
paul@353 | 793 | self.stream.write(node.name) |
paul@353 | 794 | self.stream.write("=") |
paul@353 | 795 | self.dispatch(node.expr) |
paul@355 | 796 | self._span_end() |
paul@353 | 797 | |
paul@353 | 798 | def visitLambda(self, node): |
paul@354 | 799 | if hasattr(node, "unit"): |
paul@354 | 800 | fn = node.unit |
paul@353 | 801 | else: |
paul@353 | 802 | print "Warning: function %s not recognised!" % node.name |
paul@353 | 803 | return |
paul@353 | 804 | |
paul@355 | 805 | self._span_start("lambda") |
paul@353 | 806 | self._keyword("lambda") |
paul@354 | 807 | self._parameters(fn, node) |
paul@355 | 808 | self.stream.write(": ") |
paul@355 | 809 | self._span_start("code") |
paul@353 | 810 | self.dispatch(node.code) |
paul@355 | 811 | self._span_end() |
paul@355 | 812 | self._span_end() |
paul@353 | 813 | |
paul@353 | 814 | visitList = visitAssList |
paul@353 | 815 | |
paul@353 | 816 | def visitListComp(self, node): |
paul@355 | 817 | self._span_start("listcomp") |
paul@353 | 818 | self.stream.write("[") |
paul@353 | 819 | self.dispatch(node.expr) |
paul@353 | 820 | for qual in node.quals: |
paul@353 | 821 | self.dispatch(qual) |
paul@353 | 822 | self.stream.write("]") |
paul@355 | 823 | self._span_end() |
paul@353 | 824 | |
paul@353 | 825 | def visitListCompFor(self, node): |
paul@355 | 826 | self._span_start("listcompfor") |
paul@353 | 827 | self._keyword("for") |
paul@355 | 828 | self._span_start("item") |
paul@353 | 829 | self.dispatch(node.assign) |
paul@355 | 830 | self._span_end() |
paul@353 | 831 | self._keyword("in", 1) |
paul@355 | 832 | self._span_start("collection") |
paul@353 | 833 | self.dispatch(node.list) |
paul@355 | 834 | self._span_end() |
paul@353 | 835 | for if_ in node.ifs: |
paul@353 | 836 | self.dispatch(if_) |
paul@355 | 837 | self._span_end() |
paul@353 | 838 | |
paul@353 | 839 | def visitListCompIf(self, node): |
paul@355 | 840 | self._span_start("listcompif") |
paul@355 | 841 | self._span_start("conditional") |
paul@353 | 842 | self._keyword("if", 1) |
paul@353 | 843 | self.dispatch(node.test) |
paul@355 | 844 | self._span_end() |
paul@355 | 845 | self._span_end() |
paul@353 | 846 | |
paul@353 | 847 | def visitMod(self, node): |
paul@353 | 848 | self._visitBinary(node, "mod", "%") |
paul@353 | 849 | |
paul@353 | 850 | def visitMul(self, node): |
paul@353 | 851 | self._visitBinary(node, "mul", "*") |
paul@353 | 852 | |
paul@353 | 853 | def visitName(self, node): |
paul@353 | 854 | if hasattr(node, "_scope"): |
paul@353 | 855 | scope = node._scope |
paul@353 | 856 | self._name_start() |
paul@353 | 857 | self.stream.write(node.name) |
paul@353 | 858 | self._popup_start() |
paul@353 | 859 | self._scope(scope) |
paul@353 | 860 | self._popup_end() |
paul@353 | 861 | self._name_end() |
paul@353 | 862 | else: |
paul@355 | 863 | self._span(node.name) |
paul@353 | 864 | |
paul@320 | 865 | # Output preparation methods. |
paul@320 | 866 | |
paul@320 | 867 | def _sequence(self, node): |
paul@320 | 868 | first = 1 |
paul@320 | 869 | for n in node.nodes: |
paul@320 | 870 | if not first: |
paul@321 | 871 | self.stream.write(", ") |
paul@320 | 872 | self.dispatch(n) |
paul@320 | 873 | first = 0 |
paul@320 | 874 | |
paul@320 | 875 | def _mapping(self, node): |
paul@320 | 876 | first = 1 |
paul@320 | 877 | for k, v in node.items: |
paul@320 | 878 | if not first: |
paul@321 | 879 | self.stream.write(", ") |
paul@320 | 880 | self.dispatch(k) |
paul@321 | 881 | self.stream.write(" : ") |
paul@320 | 882 | self.dispatch(v) |
paul@320 | 883 | first = 0 |
paul@320 | 884 | |
paul@354 | 885 | def _parameters(self, fn, node): |
paul@320 | 886 | nparams = len(fn.positional_names) |
paul@320 | 887 | ndefaults = len(fn.defaults) |
paul@320 | 888 | first_with_default = nparams - ndefaults |
paul@320 | 889 | |
paul@321 | 890 | first = 1 |
paul@320 | 891 | for n, param in enumerate(fn.positional_names): |
paul@320 | 892 | if not first: |
paul@321 | 893 | self.stream.write(", ") |
paul@354 | 894 | self._assname(param, node) |
paul@320 | 895 | n_default = n - first_with_default |
paul@320 | 896 | if n_default >= 0: |
paul@320 | 897 | self._default(fn.defaults[n_default]) |
paul@321 | 898 | first = 0 |
paul@320 | 899 | |
paul@320 | 900 | if fn.has_star: |
paul@320 | 901 | if not first: |
paul@321 | 902 | self.stream.write(", *") |
paul@320 | 903 | self._name(fn.star_name) |
paul@320 | 904 | |
paul@320 | 905 | if fn.has_dstar: |
paul@320 | 906 | if not first: |
paul@321 | 907 | self.stream.write(", **") |
paul@320 | 908 | self._name(fn.dstar_name) |
paul@320 | 909 | |
paul@320 | 910 | def _default(self, default): |
paul@321 | 911 | self.stream.write("=") |
paul@320 | 912 | self.dispatch(default) |
paul@320 | 913 | |
paul@318 | 914 | # Convenience functions. |
paul@318 | 915 | |
paul@320 | 916 | def summarise(module, filename): |
paul@318 | 917 | stream = open(filename, "wb") |
paul@318 | 918 | try: |
paul@320 | 919 | summary = Summary(module) |
paul@320 | 920 | summary.to_stream(stream) |
paul@320 | 921 | finally: |
paul@320 | 922 | stream.close() |
paul@320 | 923 | |
paul@320 | 924 | def annotate(module, filename): |
paul@320 | 925 | stream = open(filename, "wb") |
paul@320 | 926 | try: |
paul@320 | 927 | source = AnnotatedSource(module) |
paul@320 | 928 | source.to_stream(stream) |
paul@318 | 929 | finally: |
paul@318 | 930 | stream.close() |
paul@318 | 931 | |
paul@353 | 932 | def report(importer, directory): |
paul@353 | 933 | if not exists(directory): |
paul@353 | 934 | os.mkdir(directory) |
paul@353 | 935 | |
paul@353 | 936 | for module in importer.get_modules(): |
paul@353 | 937 | annotate(module, join(directory, "%s%sxhtml" % (module.full_name(), extsep))) |
paul@353 | 938 | summarise(module, join(directory, "%s-summary%sxhtml" % (module.full_name(), extsep))) |
paul@353 | 939 | |
paul@318 | 940 | # vim: tabstop=4 expandtab shiftwidth=4 |