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