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