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