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