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