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