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