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@360 | 47 | margin-top: 2em; |
paul@318 | 48 | } |
paul@318 | 49 | |
paul@353 | 50 | a { |
paul@353 | 51 | text-decoration: none; |
paul@353 | 52 | } |
paul@353 | 53 | |
paul@318 | 54 | .nowrap { white-space: nowrap; } |
paul@354 | 55 | .label { font-size: smaller; } |
paul@318 | 56 | |
paul@318 | 57 | .class { margin-top: 1em; margin-bottom: 1em; } |
paul@318 | 58 | .function { margin-top: 1em; margin-bottom: 1em; } |
paul@318 | 59 | .body { padding-left: 2em; } |
paul@318 | 60 | .keyword { color: yellow; } |
paul@318 | 61 | .comment { color: blue; } |
paul@318 | 62 | .class-name { color: cyan; } |
paul@318 | 63 | .function-name { color: cyan; } |
paul@358 | 64 | .specific-ref { color: #07F; } |
paul@318 | 65 | .str { color: #FF00FF; } |
paul@318 | 66 | .doc { color: #FF00FF; margin-top: 1em; margin-bottom: 1em; } |
paul@353 | 67 | |
paul@353 | 68 | .popup { |
paul@353 | 69 | display: none; |
paul@353 | 70 | position: absolute; |
paul@353 | 71 | top: 3ex; left: 0; |
paul@353 | 72 | color: white; |
paul@354 | 73 | border-bottom: 0.5ex solid #000; |
paul@354 | 74 | border-right: 0.5ex solid #000; |
paul@353 | 75 | z-index: 3; |
paul@353 | 76 | } |
paul@353 | 77 | |
paul@357 | 78 | .types-popup { |
paul@357 | 79 | display: none; |
paul@357 | 80 | position: absolute; |
paul@357 | 81 | bottom: 3ex; left: 0; |
paul@357 | 82 | color: white; |
paul@357 | 83 | border-bottom: 0.5ex solid #000; |
paul@357 | 84 | border-right: 0.5ex solid #000; |
paul@357 | 85 | z-index: 3; |
paul@357 | 86 | } |
paul@357 | 87 | |
paul@357 | 88 | .accessor, |
paul@353 | 89 | .name { |
paul@353 | 90 | position: relative; |
paul@354 | 91 | background-color: #300; |
paul@353 | 92 | color: white; |
paul@353 | 93 | } |
paul@353 | 94 | |
paul@357 | 95 | .accessor:hover, |
paul@353 | 96 | .name:hover { |
paul@354 | 97 | background-color: #500; |
paul@354 | 98 | padding-top: 0.5ex; |
paul@354 | 99 | padding-bottom: 0.5ex; |
paul@353 | 100 | z-index: 2; |
paul@353 | 101 | } |
paul@353 | 102 | |
paul@357 | 103 | .accessor:hover .types-popup, |
paul@353 | 104 | .name:hover .popup { |
paul@353 | 105 | display: block; |
paul@353 | 106 | } |
paul@318 | 107 | |
paul@354 | 108 | .attrnames, |
paul@357 | 109 | .typenames, |
paul@354 | 110 | .scope { |
paul@354 | 111 | padding: 0.5em; |
paul@354 | 112 | background-color: #700; |
paul@354 | 113 | } |
paul@354 | 114 | |
paul@318 | 115 | .summary-class { |
paul@354 | 116 | background-color: #040; |
paul@318 | 117 | } |
paul@318 | 118 | |
paul@318 | 119 | .summary-instance { |
paul@354 | 120 | background-color: #00F; |
paul@318 | 121 | } |
paul@318 | 122 | |
paul@318 | 123 | .summary-attr { |
paul@354 | 124 | background-color: #070; |
paul@318 | 125 | } |
paul@318 | 126 | |
paul@318 | 127 | </style> |
paul@318 | 128 | </head> |
paul@318 | 129 | <body> |
paul@318 | 130 | """ |
paul@318 | 131 | |
paul@318 | 132 | html_footer = """</body> |
paul@318 | 133 | </html> |
paul@318 | 134 | """ |
paul@318 | 135 | |
paul@318 | 136 | # Utility classes. |
paul@318 | 137 | |
paul@318 | 138 | class Writer: |
paul@318 | 139 | |
paul@318 | 140 | "A utility class providing useful HTML output methods." |
paul@318 | 141 | |
paul@318 | 142 | # Methods which return strings. |
paul@318 | 143 | |
paul@318 | 144 | def _text(self, text): |
paul@318 | 145 | return text.replace("&", "&").replace("<", "<").replace(">", ">") |
paul@318 | 146 | |
paul@318 | 147 | def _attr(self, attr): |
paul@318 | 148 | return self._text(attr).replace("'", "'").replace('"', """) |
paul@318 | 149 | |
paul@318 | 150 | def _url(self, url): |
paul@318 | 151 | return self._attr(url).replace("#", "%23").replace("-", "%2d") |
paul@318 | 152 | |
paul@318 | 153 | # Methods which write to the stream. |
paul@318 | 154 | |
paul@355 | 155 | def _span_start(self, classes=None): |
paul@355 | 156 | self.stream.write("<span class='%s'>" % (classes or "")) |
paul@355 | 157 | |
paul@353 | 158 | def _span_end(self): |
paul@353 | 159 | self.stream.write("</span>") |
paul@353 | 160 | |
paul@355 | 161 | def _span(self, value, classes=None): |
paul@355 | 162 | self._span_start(classes) |
paul@355 | 163 | self.stream.write(self._text(value)) |
paul@355 | 164 | self._span_end() |
paul@355 | 165 | |
paul@355 | 166 | def _name_start(self, classes=None): |
paul@355 | 167 | self._span_start(classes or "name") |
paul@355 | 168 | |
paul@355 | 169 | _name_end = _span_end |
paul@355 | 170 | |
paul@355 | 171 | def _name(self, name, classes=None): |
paul@355 | 172 | self._name_start(classes) |
paul@355 | 173 | self.stream.write(self._text(name)) |
paul@355 | 174 | self._name_end() |
paul@355 | 175 | |
paul@357 | 176 | def _popup_start(self, classes=None): |
paul@357 | 177 | self._span_start(classes or "popup") |
paul@355 | 178 | |
paul@355 | 179 | _popup_end = _span_end |
paul@355 | 180 | |
paul@318 | 181 | def _comment(self, comment): |
paul@355 | 182 | self._span("# %s" % comment, "comment") |
paul@355 | 183 | self.stream.write("\n") |
paul@318 | 184 | |
paul@353 | 185 | def _reserved(self, token, classes, leading=0, trailing=1): |
paul@353 | 186 | if leading: |
paul@353 | 187 | self.stream.write(" ") |
paul@355 | 188 | self._span(token, classes) |
paul@353 | 189 | if trailing: |
paul@353 | 190 | self.stream.write(" ") |
paul@353 | 191 | |
paul@353 | 192 | def _keyword(self, kw, leading=0, trailing=1): |
paul@353 | 193 | self._reserved(kw, "keyword", leading, trailing) |
paul@353 | 194 | |
paul@353 | 195 | def _op(self, symbol, leading=0, trailing=1): |
paul@353 | 196 | self._reserved(symbol, "operator", leading, trailing) |
paul@318 | 197 | |
paul@318 | 198 | def _doc(self, node): |
paul@318 | 199 | if node.doc is not None: |
paul@353 | 200 | self.stream.write("<pre class='doc'>") |
paul@318 | 201 | self.stream.write('"""') |
paul@318 | 202 | output = textwrap.dedent(node.doc.replace('"""', '\\"\\"\\"')) |
paul@318 | 203 | self.stream.write(self._text(output)) |
paul@318 | 204 | self.stream.write('"""') |
paul@318 | 205 | self.stream.write("</pre>\n") |
paul@318 | 206 | |
paul@321 | 207 | def _object_name_def(self, module, obj, classes=None): |
paul@320 | 208 | |
paul@320 | 209 | """ |
paul@321 | 210 | Link to the summary for 'module' using 'obj'. The optional 'classes' |
paul@321 | 211 | can be used to customise the CSS classes employed. |
paul@320 | 212 | """ |
paul@320 | 213 | |
paul@321 | 214 | self._summary_link(module.full_name(), obj.full_name(), obj.name, classes) |
paul@320 | 215 | |
paul@358 | 216 | def _object_name_ref(self, module, obj, classes=None): |
paul@358 | 217 | |
paul@358 | 218 | """ |
paul@358 | 219 | Link to the definition for 'module' using 'obj'. The optional 'classes' |
paul@358 | 220 | can be used to customise the CSS classes employed. |
paul@358 | 221 | """ |
paul@358 | 222 | |
paul@358 | 223 | self._name_link(module.full_name(), obj.full_name(), obj.name, classes) |
paul@358 | 224 | |
paul@358 | 225 | def _summary_link(self, module_name, full_name, name, classes=None): |
paul@358 | 226 | self._name_link("%s-summary" % module_name, full_name, name, classes) |
paul@358 | 227 | |
paul@353 | 228 | def _name_link(self, module_name, full_name, name, classes=None): |
paul@353 | 229 | self.stream.write("<a class='%s' href='%s%sxhtml#%s'>%s</a>" % ( |
paul@358 | 230 | classes or "specific-ref", module_name, os.path.extsep, |
paul@353 | 231 | self._attr(full_name), self._text(name))) |
paul@353 | 232 | |
paul@353 | 233 | def _scope(self, scope): |
paul@354 | 234 | self.stream.write("<div class='scope'><span class='label'>scope</span><br />%s</div>\n" % scope) |
paul@354 | 235 | |
paul@354 | 236 | def _assname(self, name, node): |
paul@355 | 237 | self._span_start("assname") |
paul@355 | 238 | if hasattr(node, "_attrnames") and node._attrnames[name]: |
paul@355 | 239 | attrnames = node._attrnames[name] |
paul@354 | 240 | self._name_start() |
paul@354 | 241 | self.stream.write(name) |
paul@354 | 242 | self._popup_start() |
paul@355 | 243 | self._attrnames(attrnames) |
paul@354 | 244 | self._popup_end() |
paul@354 | 245 | self._name_end() |
paul@354 | 246 | else: |
paul@355 | 247 | self._span(name) |
paul@355 | 248 | self._span_end() |
paul@354 | 249 | |
paul@357 | 250 | def _names_list(self, names, label, classes): |
paul@357 | 251 | if not names: |
paul@354 | 252 | return |
paul@357 | 253 | names = list(names) |
paul@357 | 254 | names.sort() |
paul@354 | 255 | |
paul@357 | 256 | self.stream.write("<div class='%s'><span class='label'>%s</span><br />" % (classes, label)) |
paul@354 | 257 | first = 1 |
paul@357 | 258 | for name in names: |
paul@354 | 259 | if not first: |
paul@354 | 260 | self.stream.write("<br />") |
paul@357 | 261 | self.stream.write(name) |
paul@354 | 262 | first = 0 |
paul@354 | 263 | self.stream.write("</div>\n") |
paul@353 | 264 | |
paul@357 | 265 | def _attrnames(self, attrnames): |
paul@357 | 266 | self._names_list(attrnames, "attributes", "attrnames") |
paul@357 | 267 | |
paul@357 | 268 | def _typenames(self, typenames): |
paul@357 | 269 | self._names_list(typenames, "types", "typenames") |
paul@357 | 270 | |
paul@357 | 271 | def _accessor_start(self, target_names): |
paul@357 | 272 | if target_names is not None: |
paul@357 | 273 | self._span_start("accessor") |
paul@357 | 274 | self._popup_start("types-popup") |
paul@357 | 275 | self._typenames(target_names) |
paul@357 | 276 | self._popup_end() |
paul@357 | 277 | |
paul@357 | 278 | def _accessor_end(self, target_names): |
paul@357 | 279 | if target_names is not None: |
paul@357 | 280 | self._span_end() |
paul@357 | 281 | |
paul@318 | 282 | # Summary classes. |
paul@318 | 283 | |
paul@320 | 284 | class Summary(Writer): |
paul@318 | 285 | |
paul@319 | 286 | "Summarise classes and attributes in modules." |
paul@319 | 287 | |
paul@357 | 288 | def __init__(self, module, program): |
paul@319 | 289 | self.module = module |
paul@357 | 290 | self.program = program |
paul@318 | 291 | |
paul@319 | 292 | def to_stream(self, stream): |
paul@319 | 293 | |
paul@319 | 294 | "Write the summary to the given 'stream'." |
paul@319 | 295 | |
paul@319 | 296 | self.stream = stream |
paul@353 | 297 | self.stream.write(html_header % {"full_name" : self.module.full_name()}) |
paul@319 | 298 | self._write_classes(self.module) |
paul@318 | 299 | self.stream.write(html_footer) |
paul@318 | 300 | |
paul@318 | 301 | def _write_classes(self, module): |
paul@318 | 302 | self.stream.write("<table cellspacing='5' cellpadding='5'>\n") |
paul@318 | 303 | |
paul@318 | 304 | all_classes = {} |
paul@318 | 305 | |
paul@318 | 306 | for obj in self.module.all_objects: |
paul@318 | 307 | if isinstance(obj, Class): |
paul@318 | 308 | all_classes[obj.name] = obj |
paul@318 | 309 | |
paul@318 | 310 | all_class_names = all_classes.keys() |
paul@318 | 311 | all_class_names.sort() |
paul@318 | 312 | |
paul@318 | 313 | for name in all_class_names: |
paul@318 | 314 | self._write_class(all_classes[name]) |
paul@318 | 315 | |
paul@318 | 316 | self.stream.write("</table>\n") |
paul@318 | 317 | |
paul@318 | 318 | def _write_class(self, obj): |
paul@318 | 319 | |
paul@318 | 320 | # Write the class... |
paul@318 | 321 | |
paul@318 | 322 | self.stream.write("<tbody class='class'>\n") |
paul@318 | 323 | self.stream.write("<tr>\n") |
paul@318 | 324 | self.stream.write("<th class='summary-class' id='%s'>\n" % obj.full_name()) |
paul@318 | 325 | self._keyword("class") |
paul@318 | 326 | self.stream.write(obj.name) |
paul@318 | 327 | self.stream.write("</th>\n") |
paul@318 | 328 | |
paul@358 | 329 | # ...and instance attribute names in order. |
paul@318 | 330 | |
paul@358 | 331 | attrs = obj.instance_attributes().values() |
paul@358 | 332 | attrs.sort(cmp=lambda x, y: cmp(x.position, y.position)) |
paul@318 | 333 | |
paul@358 | 334 | for attr in attrs: |
paul@358 | 335 | self.stream.write("<th class='summary-attr'>%s</th>\n" % self._text(attr.name)) |
paul@358 | 336 | |
paul@318 | 337 | self.stream.write("</tr>\n") |
paul@318 | 338 | self.stream.write("</tbody>\n") |
paul@318 | 339 | |
paul@320 | 340 | # Source code classes. |
paul@320 | 341 | |
paul@320 | 342 | class AnnotatedSource(ASTVisitor, Writer): |
paul@320 | 343 | |
paul@320 | 344 | "A module source code browser." |
paul@320 | 345 | |
paul@357 | 346 | def __init__(self, module, program): |
paul@320 | 347 | ASTVisitor.__init__(self) |
paul@320 | 348 | self.visitor = self |
paul@320 | 349 | self.module = module |
paul@357 | 350 | self.program = program |
paul@357 | 351 | self.objtable = self.program.get_object_table() |
paul@357 | 352 | self.paramtable = self.program.get_parameter_table() |
paul@320 | 353 | |
paul@320 | 354 | def to_stream(self, stream): |
paul@320 | 355 | |
paul@320 | 356 | "Write the annotated code to the given 'stream'." |
paul@320 | 357 | |
paul@320 | 358 | self.stream = stream |
paul@353 | 359 | self.stream.write(html_header % {"full_name" : self.module.full_name()}) |
paul@353 | 360 | self.dispatch(self.module.astnode) |
paul@320 | 361 | self.stream.write(html_footer) |
paul@320 | 362 | |
paul@320 | 363 | def visitModule(self, node): |
paul@320 | 364 | self.default(node) |
paul@320 | 365 | |
paul@320 | 366 | # Statements. |
paul@320 | 367 | |
paul@320 | 368 | def visitAssert(self, node): |
paul@320 | 369 | self.stream.write("<div class='assert nowrap'>\n") |
paul@320 | 370 | self._keyword("assert") |
paul@320 | 371 | self.dispatch(node.test) |
paul@320 | 372 | if node.fail: |
paul@320 | 373 | self.stream.write(", ") |
paul@320 | 374 | self.dispatch(node.fail) |
paul@320 | 375 | self.stream.write("</div>\n") |
paul@320 | 376 | |
paul@320 | 377 | def visitAssign(self, node): |
paul@320 | 378 | self.stream.write("<div class='assign nowrap'>\n") |
paul@320 | 379 | for lvalue in node.nodes: |
paul@320 | 380 | self.dispatch(lvalue) |
paul@353 | 381 | self.stream.write(" = ") |
paul@320 | 382 | self.dispatch(node.expr) |
paul@320 | 383 | self.stream.write("</div>\n") |
paul@320 | 384 | |
paul@320 | 385 | def visitAugAssign(self, node): |
paul@320 | 386 | self.stream.write("<div class='augassign nowrap'>\n") |
paul@320 | 387 | self.dispatch(node.node) |
paul@353 | 388 | self.stream.write(" %s " % node.op) |
paul@320 | 389 | self.dispatch(node.expr) |
paul@320 | 390 | self.stream.write("</div>\n") |
paul@320 | 391 | |
paul@320 | 392 | def visitBreak(self, node): |
paul@320 | 393 | self.stream.write("<div class='break nowrap'>\n") |
paul@320 | 394 | self._keyword("break") |
paul@320 | 395 | self.stream.write("</div>\n") |
paul@320 | 396 | |
paul@320 | 397 | def visitClass(self, node): |
paul@353 | 398 | if not used_by_unit(node): |
paul@353 | 399 | return |
paul@320 | 400 | |
paul@320 | 401 | # Use inspected details where possible. |
paul@320 | 402 | |
paul@354 | 403 | if hasattr(node, "unit"): |
paul@354 | 404 | cls = node.unit |
paul@320 | 405 | bases = cls.bases |
paul@320 | 406 | self.stream.write("<div class='class nowrap' id='%s'>\n" % cls.full_name()) |
paul@320 | 407 | else: |
paul@320 | 408 | print "Warning: class %s not recognised!" % node.name |
paul@320 | 409 | return |
paul@320 | 410 | |
paul@320 | 411 | # Write the declaration line. |
paul@320 | 412 | |
paul@320 | 413 | self.stream.write("<div>\n") |
paul@320 | 414 | self._keyword("class") |
paul@321 | 415 | self._object_name_def(self.module, cls, "class-name") |
paul@320 | 416 | |
paul@320 | 417 | # Suppress the "object" class appearing alone. |
paul@320 | 418 | |
paul@320 | 419 | if bases and not (len(bases) == 1 and bases[0].name == "object"): |
paul@320 | 420 | self.stream.write("(") |
paul@320 | 421 | first = 1 |
paul@320 | 422 | for base in bases: |
paul@320 | 423 | if not first: |
paul@321 | 424 | self.stream.write(", ") |
paul@320 | 425 | |
paul@358 | 426 | self._object_name_ref(base.module, base) |
paul@320 | 427 | |
paul@320 | 428 | first = 0 |
paul@320 | 429 | self.stream.write(")") |
paul@320 | 430 | |
paul@320 | 431 | self.stream.write(":\n") |
paul@320 | 432 | self.stream.write("</div>\n") |
paul@320 | 433 | |
paul@320 | 434 | # Write the docstring and class body. |
paul@320 | 435 | |
paul@320 | 436 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 437 | self._doc(node) |
paul@358 | 438 | |
paul@358 | 439 | # NOTE: Some streams may not support tell. |
paul@358 | 440 | |
paul@358 | 441 | x = self.stream.tell() |
paul@358 | 442 | |
paul@358 | 443 | self.default(node.code) |
paul@358 | 444 | |
paul@358 | 445 | # Check for no output. |
paul@358 | 446 | |
paul@358 | 447 | if x == self.stream.tell(): |
paul@358 | 448 | self.visitPass(None) |
paul@358 | 449 | |
paul@320 | 450 | self.stream.write("</div>\n") |
paul@320 | 451 | self.stream.write("</div>\n") |
paul@320 | 452 | |
paul@320 | 453 | def visitContinue(self, node): |
paul@320 | 454 | self.stream.write("<div class='continue nowrap'>\n") |
paul@320 | 455 | self._keyword("continue") |
paul@320 | 456 | self.stream.write("</div>\n") |
paul@320 | 457 | |
paul@320 | 458 | def visitDiscard(self, node): |
paul@320 | 459 | self.stream.write("<div class='discard nowrap'>\n") |
paul@320 | 460 | self.default(node) |
paul@320 | 461 | self.stream.write("</div>\n") |
paul@320 | 462 | |
paul@320 | 463 | def visitFor(self, node): |
paul@320 | 464 | self.stream.write("<div class='if nowrap'>\n") |
paul@320 | 465 | self.stream.write("<div>\n") |
paul@320 | 466 | self._keyword("for") |
paul@320 | 467 | self.dispatch(node.assign) |
paul@353 | 468 | self._keyword("in", 1) |
paul@320 | 469 | self.dispatch(node.list) |
paul@320 | 470 | self.stream.write(":\n") |
paul@320 | 471 | self.stream.write("</div>\n") |
paul@320 | 472 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 473 | self.dispatch(node.body) |
paul@320 | 474 | self.stream.write("</div>\n") |
paul@320 | 475 | if node.else_ is not None: |
paul@320 | 476 | self.stream.write("<div>\n") |
paul@353 | 477 | self._keyword("else", trailing=0) |
paul@320 | 478 | self.stream.write(":\n") |
paul@320 | 479 | self.stream.write("</div>\n") |
paul@320 | 480 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 481 | self.dispatch(node.else_) |
paul@320 | 482 | self.stream.write("</div>\n") |
paul@320 | 483 | self.stream.write("</div>\n") |
paul@320 | 484 | |
paul@320 | 485 | def visitFrom(self, node): |
paul@320 | 486 | self.stream.write("<div class='from nowrap'>\n") |
paul@320 | 487 | self._keyword("from") |
paul@320 | 488 | self._name(node.modname) |
paul@353 | 489 | self._keyword("import", 1) |
paul@320 | 490 | first = 1 |
paul@320 | 491 | for (name, alias), _name in map(None, node.names, node._names): |
paul@320 | 492 | if not first: |
paul@321 | 493 | self.stream.write(", ") |
paul@320 | 494 | if alias: |
paul@320 | 495 | self.stream.write(name + " ") |
paul@353 | 496 | self._keyword("as", 1) |
paul@320 | 497 | self._name(alias or name) |
paul@320 | 498 | first = 0 |
paul@320 | 499 | self.stream.write("</div>\n") |
paul@320 | 500 | |
paul@320 | 501 | def visitFunction(self, node): |
paul@353 | 502 | if not used_by_unit(node): |
paul@353 | 503 | return |
paul@353 | 504 | |
paul@354 | 505 | if hasattr(node, "unit"): |
paul@354 | 506 | fn = node.unit |
paul@320 | 507 | self.stream.write("<div class='function nowrap' id='%s'>\n" % fn.full_name()) |
paul@320 | 508 | else: |
paul@320 | 509 | print "Warning: function %s not recognised!" % node.name |
paul@320 | 510 | return |
paul@320 | 511 | |
paul@320 | 512 | # Write the declaration line. |
paul@320 | 513 | |
paul@320 | 514 | self.stream.write("<div>\n") |
paul@320 | 515 | self._keyword("def") |
paul@321 | 516 | self._object_name_def(self.module, fn, "function-name") |
paul@320 | 517 | |
paul@320 | 518 | self.stream.write("(") |
paul@354 | 519 | self._parameters(fn, node) |
paul@320 | 520 | self.stream.write(")") |
paul@320 | 521 | self.stream.write(":\n") |
paul@320 | 522 | self.stream.write("</div>\n") |
paul@320 | 523 | |
paul@320 | 524 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 525 | self._doc(node) |
paul@320 | 526 | self.dispatch(node.code) |
paul@320 | 527 | self.stream.write("</div>\n") |
paul@320 | 528 | self.stream.write("</div>\n") |
paul@320 | 529 | |
paul@320 | 530 | def visitGlobal(self, node): |
paul@320 | 531 | self.stream.write("<div class='global nowrap'>\n") |
paul@320 | 532 | self._keyword("global") |
paul@320 | 533 | first = 1 |
paul@320 | 534 | for name in node.names: |
paul@320 | 535 | if not first: |
paul@321 | 536 | self.stream.write(", ") |
paul@320 | 537 | self.stream.write(name) |
paul@320 | 538 | first = 0 |
paul@320 | 539 | self.stream.write("</div>\n") |
paul@320 | 540 | |
paul@320 | 541 | def visitIf(self, node): |
paul@320 | 542 | self.stream.write("<div class='if nowrap'>\n") |
paul@320 | 543 | first = 1 |
paul@320 | 544 | for compare, stmt in node.tests: |
paul@320 | 545 | self.stream.write("<div>\n") |
paul@320 | 546 | if first: |
paul@320 | 547 | self._keyword("if") |
paul@320 | 548 | else: |
paul@320 | 549 | self._keyword("elif") |
paul@320 | 550 | self.dispatch(compare) |
paul@320 | 551 | self.stream.write(":\n") |
paul@320 | 552 | self.stream.write("</div>\n") |
paul@320 | 553 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 554 | self.dispatch(stmt) |
paul@320 | 555 | self.stream.write("</div>\n") |
paul@320 | 556 | first = 0 |
paul@320 | 557 | if node.else_ is not None: |
paul@320 | 558 | self.stream.write("<div>\n") |
paul@353 | 559 | self._keyword("else", trailing=0) |
paul@353 | 560 | self.stream.write(":\n") |
paul@353 | 561 | self.stream.write("</div>\n") |
paul@353 | 562 | self.stream.write("<div class='body nowrap'>\n") |
paul@353 | 563 | self.dispatch(node.else_) |
paul@353 | 564 | self.stream.write("</div>\n") |
paul@353 | 565 | self.stream.write("</div>\n") |
paul@353 | 566 | |
paul@353 | 567 | def visitImport(self, node): |
paul@353 | 568 | self.stream.write("<div class='import nowrap'>\n") |
paul@353 | 569 | self._keyword("import") |
paul@353 | 570 | first = 1 |
paul@353 | 571 | for name, alias in node.names: |
paul@353 | 572 | if not first: |
paul@353 | 573 | self.stream.write(",\n") |
paul@353 | 574 | if alias: |
paul@353 | 575 | self.stream.write(name + " ") |
paul@353 | 576 | self._keyword("as", 1) |
paul@353 | 577 | self._name(alias or name) |
paul@353 | 578 | first = 0 |
paul@353 | 579 | self.stream.write("</div>\n") |
paul@353 | 580 | |
paul@353 | 581 | def visitPass(self, node): |
paul@353 | 582 | self.stream.write("<div class='pass nowrap'>\n") |
paul@353 | 583 | self._keyword("pass") |
paul@353 | 584 | self.stream.write("</div>\n") |
paul@353 | 585 | |
paul@353 | 586 | def visitPrint(self, node): |
paul@353 | 587 | self.stream.write("<div class='print nowrap'>\n") |
paul@353 | 588 | self._keyword("print") |
paul@353 | 589 | if node.dest is not None: |
paul@353 | 590 | self.stream.write(">>\n") |
paul@353 | 591 | self.dispatch(node.dest) |
paul@353 | 592 | for n in node.nodes: |
paul@353 | 593 | self.dispatch(n) |
paul@353 | 594 | self.stream.write(",\n") |
paul@353 | 595 | self.stream.write("</div>\n") |
paul@353 | 596 | |
paul@353 | 597 | def visitPrintnl(self, node): |
paul@353 | 598 | self.stream.write("<div class='printnl nowrap'>\n") |
paul@353 | 599 | self._keyword("print") |
paul@353 | 600 | if node.dest is not None: |
paul@353 | 601 | self.stream.write(">>\n") |
paul@353 | 602 | self.dispatch(node.dest) |
paul@353 | 603 | first = 1 |
paul@353 | 604 | for n in node.nodes: |
paul@353 | 605 | if not first: |
paul@353 | 606 | self.stream.write(",\n") |
paul@353 | 607 | self.dispatch(n) |
paul@353 | 608 | first = 0 |
paul@353 | 609 | self.stream.write("</div>\n") |
paul@353 | 610 | |
paul@353 | 611 | def visitRaise(self, node): |
paul@353 | 612 | self.stream.write("<div class='raise nowrap'>\n") |
paul@353 | 613 | self._keyword("raise") |
paul@353 | 614 | self.dispatch(node.expr1) |
paul@353 | 615 | if node.expr2 is not None: |
paul@353 | 616 | self.stream.write(",\n") |
paul@353 | 617 | self.dispatch(node.expr2) |
paul@353 | 618 | if node.expr3 is not None: |
paul@353 | 619 | self.stream.write(",\n") |
paul@353 | 620 | self.dispatch(node.expr3) |
paul@353 | 621 | self.stream.write("</div>\n") |
paul@353 | 622 | |
paul@353 | 623 | def visitReturn(self, node): |
paul@353 | 624 | self.stream.write("<div class='return nowrap'>\n") |
paul@353 | 625 | self._keyword("return") |
paul@353 | 626 | self.dispatch(node.value) |
paul@353 | 627 | self.stream.write("</div>\n") |
paul@353 | 628 | |
paul@353 | 629 | def visitStmt(self, node): |
paul@353 | 630 | self.stream.write("<div class='stmt nowrap'>\n") |
paul@353 | 631 | self.default(node) |
paul@353 | 632 | self.stream.write("</div>\n") |
paul@353 | 633 | |
paul@353 | 634 | def visitTryExcept(self, node): |
paul@353 | 635 | self.stream.write("<div class='tryexcept nowrap'>\n") |
paul@353 | 636 | self.stream.write("<div>\n") |
paul@353 | 637 | self._keyword("try", trailing=0) |
paul@353 | 638 | self.stream.write(":\n") |
paul@353 | 639 | self.stream.write("</div>\n") |
paul@353 | 640 | self.stream.write("<div class='body nowrap'>\n") |
paul@353 | 641 | self.dispatch(node.body) |
paul@353 | 642 | self.stream.write("</div>\n") |
paul@353 | 643 | for spec, assign, statement in node.handlers: |
paul@353 | 644 | self.stream.write("<div>\n") |
paul@353 | 645 | self._keyword("except") |
paul@353 | 646 | if spec is not None: |
paul@353 | 647 | self.dispatch(spec) |
paul@353 | 648 | if assign is not None: |
paul@353 | 649 | self.stream.write(",\n") |
paul@353 | 650 | self.dispatch(assign) |
paul@353 | 651 | self.stream.write(":\n") |
paul@353 | 652 | self.stream.write("</div>\n") |
paul@353 | 653 | self.stream.write("<div class='body nowrap'>\n") |
paul@353 | 654 | self.dispatch(statement) |
paul@353 | 655 | self.stream.write("</div>\n") |
paul@353 | 656 | if node.else_ is not None: |
paul@353 | 657 | self.stream.write("<div>\n") |
paul@353 | 658 | self._keyword("else", trailing=0) |
paul@353 | 659 | self.stream.write(":\n") |
paul@353 | 660 | self.stream.write("</div>\n") |
paul@353 | 661 | self.stream.write("<div class='body nowrap'>\n") |
paul@353 | 662 | self.dispatch(node.else_) |
paul@353 | 663 | self.stream.write("</div>\n") |
paul@353 | 664 | self.stream.write("</div>\n") |
paul@353 | 665 | |
paul@353 | 666 | def visitTryFinally(self, node): |
paul@353 | 667 | self.stream.write("<div class='tryfinally nowrap'>\n") |
paul@353 | 668 | self.stream.write("<div>\n") |
paul@353 | 669 | self._keyword("try", trailing=0) |
paul@353 | 670 | self.stream.write(":\n") |
paul@353 | 671 | self.stream.write("</div>\n") |
paul@353 | 672 | self.stream.write("<div class='body nowrap'>\n") |
paul@353 | 673 | self.dispatch(node.body) |
paul@353 | 674 | self.stream.write("</div>\n") |
paul@353 | 675 | self.stream.write("<div>\n") |
paul@353 | 676 | self._keyword("finally", trailing=0) |
paul@353 | 677 | self.stream.write(":\n") |
paul@353 | 678 | self.stream.write("</div>\n") |
paul@353 | 679 | self.stream.write("<div class='body nowrap'>\n") |
paul@353 | 680 | self.dispatch(node.final) |
paul@353 | 681 | self.stream.write("</div>\n") |
paul@353 | 682 | self.stream.write("</div>\n") |
paul@353 | 683 | |
paul@353 | 684 | def visitWhile(self, node): |
paul@353 | 685 | self.stream.write("<div class='while nowrap'>\n") |
paul@353 | 686 | self.stream.write("<div>\n") |
paul@353 | 687 | self._keyword("while") |
paul@353 | 688 | self.dispatch(node.test) |
paul@353 | 689 | self.stream.write(":\n") |
paul@353 | 690 | self.stream.write("</div>\n") |
paul@353 | 691 | self.stream.write("<div class='body nowrap'>\n") |
paul@353 | 692 | self.dispatch(node.body) |
paul@353 | 693 | self.stream.write("</div>\n") |
paul@353 | 694 | if node.else_ is not None: |
paul@353 | 695 | self.stream.write("<div>\n") |
paul@353 | 696 | self._keyword("else", trailing=0) |
paul@320 | 697 | self.stream.write(":\n") |
paul@320 | 698 | self.stream.write("</div>\n") |
paul@320 | 699 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 700 | self.dispatch(node.else_) |
paul@320 | 701 | self.stream.write("</div>\n") |
paul@320 | 702 | self.stream.write("</div>\n") |
paul@320 | 703 | |
paul@353 | 704 | # Expression-related helper methods. |
paul@353 | 705 | |
paul@353 | 706 | def _visitBitBinary(self, node, name, symbol): |
paul@355 | 707 | self._span_start(name) |
paul@353 | 708 | first = 1 |
paul@353 | 709 | for node in node.nodes: |
paul@353 | 710 | if not first: |
paul@353 | 711 | self._op(symbol, 1) |
paul@353 | 712 | self.dispatch(node) |
paul@353 | 713 | first = 0 |
paul@355 | 714 | self._span_end() |
paul@353 | 715 | |
paul@353 | 716 | def _visitBinary(self, node, name, symbol): |
paul@355 | 717 | self._span_start(name) |
paul@353 | 718 | self.dispatch(node.left) |
paul@353 | 719 | self._op(symbol, 1) |
paul@353 | 720 | self.dispatch(node.right) |
paul@355 | 721 | self._span_end() |
paul@353 | 722 | |
paul@353 | 723 | def _visitUnary(self, node, name, symbol): |
paul@355 | 724 | self._span_start(name) |
paul@353 | 725 | self._op(symbol) |
paul@353 | 726 | self.dispatch(node.expr) |
paul@355 | 727 | self._span_end() |
paul@353 | 728 | |
paul@353 | 729 | # Expressions. |
paul@353 | 730 | |
paul@353 | 731 | def visitAdd(self, node): |
paul@353 | 732 | self._visitBinary(node, "add", "+") |
paul@353 | 733 | |
paul@353 | 734 | def visitAnd(self, node): |
paul@355 | 735 | self._span_start("and") |
paul@353 | 736 | first = 1 |
paul@353 | 737 | for n in node.nodes: |
paul@353 | 738 | if not first: |
paul@353 | 739 | self._keyword("and", 1) |
paul@353 | 740 | self.dispatch(n) |
paul@353 | 741 | first = 0 |
paul@355 | 742 | self._span_end() |
paul@353 | 743 | |
paul@353 | 744 | def visitAssAttr(self, node): |
paul@359 | 745 | target_names = self.possible_accessor_types(node) |
paul@355 | 746 | self._span_start("assattr") |
paul@359 | 747 | self._accessor_start(target_names) |
paul@353 | 748 | self.dispatch(node.expr) |
paul@359 | 749 | self._accessor_end(target_names) |
paul@355 | 750 | self._span_start("attr") |
paul@353 | 751 | self.stream.write(".") |
paul@353 | 752 | self._name(node.attrname) |
paul@355 | 753 | self._span_end() |
paul@355 | 754 | self._span_end() |
paul@353 | 755 | |
paul@353 | 756 | def visitAssList(self, node): |
paul@355 | 757 | self._span_start("list") |
paul@353 | 758 | self.stream.write("[") |
paul@353 | 759 | self._sequence(node) |
paul@353 | 760 | self.stream.write("]") |
paul@355 | 761 | self._span_end() |
paul@353 | 762 | |
paul@353 | 763 | def visitAssName(self, node): |
paul@354 | 764 | self._assname(node.name, node) |
paul@353 | 765 | |
paul@353 | 766 | def visitAssTuple(self, node): |
paul@355 | 767 | self._span_start("tuple") |
paul@353 | 768 | self.stream.write("(") |
paul@353 | 769 | self._sequence(node) |
paul@353 | 770 | self.stream.write(")") |
paul@355 | 771 | self._span_end() |
paul@353 | 772 | |
paul@353 | 773 | def visitBitand(self, node): |
paul@353 | 774 | self._visitBitBinary(node, "bitand", "&") |
paul@353 | 775 | |
paul@353 | 776 | def visitCallFunc(self, node): |
paul@355 | 777 | self._span_start("callfunc") |
paul@353 | 778 | self.dispatch(node.node) |
paul@355 | 779 | self._span_start("call") |
paul@353 | 780 | self.stream.write("(") |
paul@353 | 781 | first = 1 |
paul@353 | 782 | for arg in node.args: |
paul@353 | 783 | if not first: |
paul@353 | 784 | self.stream.write(", ") |
paul@353 | 785 | self.dispatch(arg) |
paul@353 | 786 | first = 0 |
paul@353 | 787 | if node.star_args is not None: |
paul@353 | 788 | if not first: |
paul@353 | 789 | self.stream.write(", *") |
paul@353 | 790 | self.dispatch(node.star_args) |
paul@353 | 791 | first = 0 |
paul@353 | 792 | if node.dstar_args is not None: |
paul@353 | 793 | if not first: |
paul@353 | 794 | self.stream.write(", **") |
paul@353 | 795 | self.dispatch(node.dstar_args) |
paul@353 | 796 | first = 0 |
paul@353 | 797 | self.stream.write(")") |
paul@355 | 798 | self._span_end() |
paul@355 | 799 | self._span_end() |
paul@353 | 800 | |
paul@353 | 801 | def visitCompare(self, node): |
paul@355 | 802 | self._span_start("compare") |
paul@353 | 803 | self.dispatch(node.expr) |
paul@353 | 804 | for op_name, expr in node.ops: |
paul@353 | 805 | self._op(op_name, 1) |
paul@353 | 806 | self.dispatch(expr) |
paul@355 | 807 | self._span_end() |
paul@353 | 808 | |
paul@353 | 809 | def visitConst(self, node): |
paul@353 | 810 | if isinstance(node.value, (str, unicode)): |
paul@355 | 811 | self._span_start("str") |
paul@353 | 812 | self.stream.write(self._text(repr(node.value))) |
paul@353 | 813 | if isinstance(node.value, (str, unicode)): |
paul@355 | 814 | self._span_end() |
paul@353 | 815 | |
paul@353 | 816 | def visitDict(self, node): |
paul@355 | 817 | self._span_start("dict") |
paul@353 | 818 | self.stream.write("{") |
paul@353 | 819 | self._mapping(node) |
paul@353 | 820 | self.stream.write("}") |
paul@355 | 821 | self._span_end() |
paul@353 | 822 | |
paul@353 | 823 | def visitDiv(self, node): |
paul@353 | 824 | self._visitBinary(node, "div", "/") |
paul@353 | 825 | |
paul@353 | 826 | def visitFloorDiv(self, node): |
paul@353 | 827 | self._visitBinary(node, "floordiv", "//") |
paul@353 | 828 | |
paul@353 | 829 | def visitGetattr(self, node): |
paul@357 | 830 | target_names = self.possible_accessor_types(node) |
paul@355 | 831 | self._span_start("getattr") |
paul@357 | 832 | self._accessor_start(target_names) |
paul@353 | 833 | self.dispatch(node.expr) |
paul@357 | 834 | self._accessor_end(target_names) |
paul@355 | 835 | self._span_start("attr") |
paul@353 | 836 | self.stream.write(".") |
paul@356 | 837 | self._span(node.attrname, "attrname") |
paul@355 | 838 | self._span_end() |
paul@355 | 839 | self._span_end() |
paul@353 | 840 | |
paul@353 | 841 | def visitKeyword(self, node): |
paul@355 | 842 | self._span_start("keyword-arg") |
paul@353 | 843 | self.stream.write(node.name) |
paul@353 | 844 | self.stream.write("=") |
paul@353 | 845 | self.dispatch(node.expr) |
paul@355 | 846 | self._span_end() |
paul@353 | 847 | |
paul@353 | 848 | def visitLambda(self, node): |
paul@354 | 849 | if hasattr(node, "unit"): |
paul@354 | 850 | fn = node.unit |
paul@353 | 851 | else: |
paul@353 | 852 | print "Warning: function %s not recognised!" % node.name |
paul@353 | 853 | return |
paul@353 | 854 | |
paul@355 | 855 | self._span_start("lambda") |
paul@353 | 856 | self._keyword("lambda") |
paul@354 | 857 | self._parameters(fn, node) |
paul@355 | 858 | self.stream.write(": ") |
paul@355 | 859 | self._span_start("code") |
paul@353 | 860 | self.dispatch(node.code) |
paul@355 | 861 | self._span_end() |
paul@355 | 862 | self._span_end() |
paul@353 | 863 | |
paul@353 | 864 | visitList = visitAssList |
paul@353 | 865 | |
paul@353 | 866 | def visitListComp(self, node): |
paul@355 | 867 | self._span_start("listcomp") |
paul@353 | 868 | self.stream.write("[") |
paul@353 | 869 | self.dispatch(node.expr) |
paul@353 | 870 | for qual in node.quals: |
paul@353 | 871 | self.dispatch(qual) |
paul@353 | 872 | self.stream.write("]") |
paul@355 | 873 | self._span_end() |
paul@353 | 874 | |
paul@353 | 875 | def visitListCompFor(self, node): |
paul@355 | 876 | self._span_start("listcompfor") |
paul@353 | 877 | self._keyword("for") |
paul@355 | 878 | self._span_start("item") |
paul@353 | 879 | self.dispatch(node.assign) |
paul@355 | 880 | self._span_end() |
paul@353 | 881 | self._keyword("in", 1) |
paul@355 | 882 | self._span_start("collection") |
paul@353 | 883 | self.dispatch(node.list) |
paul@355 | 884 | self._span_end() |
paul@353 | 885 | for if_ in node.ifs: |
paul@353 | 886 | self.dispatch(if_) |
paul@355 | 887 | self._span_end() |
paul@353 | 888 | |
paul@353 | 889 | def visitListCompIf(self, node): |
paul@355 | 890 | self._span_start("listcompif") |
paul@355 | 891 | self._span_start("conditional") |
paul@353 | 892 | self._keyword("if", 1) |
paul@353 | 893 | self.dispatch(node.test) |
paul@355 | 894 | self._span_end() |
paul@355 | 895 | self._span_end() |
paul@353 | 896 | |
paul@353 | 897 | def visitMod(self, node): |
paul@353 | 898 | self._visitBinary(node, "mod", "%") |
paul@353 | 899 | |
paul@353 | 900 | def visitMul(self, node): |
paul@353 | 901 | self._visitBinary(node, "mul", "*") |
paul@353 | 902 | |
paul@353 | 903 | def visitName(self, node): |
paul@353 | 904 | if hasattr(node, "_scope"): |
paul@353 | 905 | scope = node._scope |
paul@353 | 906 | self._name_start() |
paul@353 | 907 | self.stream.write(node.name) |
paul@353 | 908 | self._popup_start() |
paul@353 | 909 | self._scope(scope) |
paul@353 | 910 | self._popup_end() |
paul@353 | 911 | self._name_end() |
paul@353 | 912 | else: |
paul@355 | 913 | self._span(node.name) |
paul@353 | 914 | |
paul@358 | 915 | def visitNot(self, node): |
paul@358 | 916 | self._span_start("not") |
paul@358 | 917 | self._keyword("not") |
paul@358 | 918 | self.dispatch(node.expr) |
paul@358 | 919 | self._span_end() |
paul@358 | 920 | |
paul@358 | 921 | def visitOr(self, node): |
paul@358 | 922 | self._span_start("or") |
paul@358 | 923 | first = 1 |
paul@358 | 924 | for n in node.nodes: |
paul@358 | 925 | if not first: |
paul@358 | 926 | self._keyword("or", 1) |
paul@358 | 927 | self.dispatch(n) |
paul@358 | 928 | first = 0 |
paul@358 | 929 | self._span_end() |
paul@358 | 930 | |
paul@358 | 931 | def visitPower(self, node): |
paul@358 | 932 | self._visitBinary(node, "power", "**") |
paul@358 | 933 | |
paul@358 | 934 | def visitSlice(self, node): |
paul@358 | 935 | self._span_start("slice") |
paul@358 | 936 | self.dispatch(node.expr) |
paul@358 | 937 | self.stream.write("[") |
paul@358 | 938 | if node.lower: |
paul@358 | 939 | self.dispatch(node.lower) |
paul@358 | 940 | self.stream.write(":") |
paul@358 | 941 | if node.upper: |
paul@358 | 942 | self.dispatch(node.upper) |
paul@358 | 943 | # NOTE: Step? |
paul@358 | 944 | self.stream.write("]") |
paul@358 | 945 | self._span_end() |
paul@358 | 946 | |
paul@358 | 947 | def visitSliceobj(self, node): |
paul@358 | 948 | self._span_start("sliceobj") |
paul@358 | 949 | first = 1 |
paul@358 | 950 | for n in node.nodes: |
paul@358 | 951 | if not first: |
paul@358 | 952 | self.stream.write(":") |
paul@358 | 953 | self.dispatch(n) |
paul@358 | 954 | self._span_end() |
paul@358 | 955 | |
paul@358 | 956 | def visitSub(self, node): |
paul@358 | 957 | self._visitBinary(node, "sub", "-") |
paul@358 | 958 | |
paul@358 | 959 | def visitSubscript(self, node): |
paul@358 | 960 | self._span_start("subscript") |
paul@358 | 961 | self.dispatch(node.expr) |
paul@358 | 962 | self.stream.write("[") |
paul@358 | 963 | first = 1 |
paul@358 | 964 | for sub in node.subs: |
paul@358 | 965 | if not first: |
paul@358 | 966 | self.stream.write(", ") |
paul@358 | 967 | self.dispatch(sub) |
paul@358 | 968 | first = 0 |
paul@358 | 969 | self.stream.write("]") |
paul@358 | 970 | self._span_end() |
paul@358 | 971 | |
paul@358 | 972 | visitTuple = visitAssTuple |
paul@358 | 973 | |
paul@358 | 974 | def visitUnaryAdd(self, node): |
paul@358 | 975 | self._visitUnary(node, "add", "+") |
paul@358 | 976 | |
paul@358 | 977 | def visitUnarySub(self, node): |
paul@358 | 978 | self._visitUnary(node, "sub", "-") |
paul@358 | 979 | |
paul@320 | 980 | # Output preparation methods. |
paul@320 | 981 | |
paul@320 | 982 | def _sequence(self, node): |
paul@320 | 983 | first = 1 |
paul@320 | 984 | for n in node.nodes: |
paul@320 | 985 | if not first: |
paul@321 | 986 | self.stream.write(", ") |
paul@320 | 987 | self.dispatch(n) |
paul@320 | 988 | first = 0 |
paul@320 | 989 | |
paul@320 | 990 | def _mapping(self, node): |
paul@320 | 991 | first = 1 |
paul@320 | 992 | for k, v in node.items: |
paul@320 | 993 | if not first: |
paul@321 | 994 | self.stream.write(", ") |
paul@320 | 995 | self.dispatch(k) |
paul@321 | 996 | self.stream.write(" : ") |
paul@320 | 997 | self.dispatch(v) |
paul@320 | 998 | first = 0 |
paul@320 | 999 | |
paul@354 | 1000 | def _parameters(self, fn, node): |
paul@320 | 1001 | nparams = len(fn.positional_names) |
paul@320 | 1002 | ndefaults = len(fn.defaults) |
paul@320 | 1003 | first_with_default = nparams - ndefaults |
paul@320 | 1004 | |
paul@321 | 1005 | first = 1 |
paul@320 | 1006 | for n, param in enumerate(fn.positional_names): |
paul@320 | 1007 | if not first: |
paul@321 | 1008 | self.stream.write(", ") |
paul@354 | 1009 | self._assname(param, node) |
paul@320 | 1010 | n_default = n - first_with_default |
paul@320 | 1011 | if n_default >= 0: |
paul@320 | 1012 | self._default(fn.defaults[n_default]) |
paul@321 | 1013 | first = 0 |
paul@320 | 1014 | |
paul@320 | 1015 | if fn.has_star: |
paul@320 | 1016 | if not first: |
paul@321 | 1017 | self.stream.write(", *") |
paul@320 | 1018 | self._name(fn.star_name) |
paul@320 | 1019 | |
paul@320 | 1020 | if fn.has_dstar: |
paul@320 | 1021 | if not first: |
paul@321 | 1022 | self.stream.write(", **") |
paul@320 | 1023 | self._name(fn.dstar_name) |
paul@320 | 1024 | |
paul@320 | 1025 | def _default(self, default): |
paul@321 | 1026 | self.stream.write("=") |
paul@320 | 1027 | self.dispatch(default) |
paul@320 | 1028 | |
paul@318 | 1029 | # Convenience functions. |
paul@318 | 1030 | |
paul@357 | 1031 | def summarise(module, program, filename): |
paul@318 | 1032 | stream = open(filename, "wb") |
paul@318 | 1033 | try: |
paul@357 | 1034 | summary = Summary(module, program) |
paul@320 | 1035 | summary.to_stream(stream) |
paul@320 | 1036 | finally: |
paul@320 | 1037 | stream.close() |
paul@320 | 1038 | |
paul@357 | 1039 | def annotate(module, program, filename): |
paul@320 | 1040 | stream = open(filename, "wb") |
paul@320 | 1041 | try: |
paul@357 | 1042 | source = AnnotatedSource(module, program) |
paul@320 | 1043 | source.to_stream(stream) |
paul@318 | 1044 | finally: |
paul@318 | 1045 | stream.close() |
paul@318 | 1046 | |
paul@357 | 1047 | def report(program, directory): |
paul@353 | 1048 | if not exists(directory): |
paul@353 | 1049 | os.mkdir(directory) |
paul@353 | 1050 | |
paul@357 | 1051 | for module in program.get_importer().get_modules(): |
paul@357 | 1052 | annotate(module, program, join(directory, "%s%sxhtml" % (module.full_name(), extsep))) |
paul@357 | 1053 | summarise(module, program, join(directory, "%s-summary%sxhtml" % (module.full_name(), extsep))) |
paul@353 | 1054 | |
paul@318 | 1055 | # vim: tabstop=4 expandtab shiftwidth=4 |