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