paul@318 | 1 | #!/usr/bin/env python |
paul@318 | 2 | |
paul@318 | 3 | """ |
paul@318 | 4 | View annotated sources. |
paul@318 | 5 | |
paul@318 | 6 | Copyright (C) 2006, 2007, 2010 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@318 | 22 | from micropython.data import * |
paul@318 | 23 | from compiler.visitor import ASTVisitor |
paul@318 | 24 | import sys |
paul@318 | 25 | import os |
paul@318 | 26 | import textwrap |
paul@318 | 27 | |
paul@318 | 28 | # Classes. |
paul@318 | 29 | |
paul@318 | 30 | # HTML-related output production. |
paul@318 | 31 | |
paul@318 | 32 | html_header = """<?xml version="1.0" encoding="iso-8859-15"?> |
paul@318 | 33 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
paul@318 | 34 | <html xmlns="http://www.w3.org/1999/xhtml"> |
paul@318 | 35 | <head> |
paul@318 | 36 | <title>Module</title> |
paul@318 | 37 | <style type="text/css"> |
paul@318 | 38 | html { |
paul@318 | 39 | background-color: black; color: white; |
paul@318 | 40 | } |
paul@318 | 41 | |
paul@318 | 42 | body { |
paul@318 | 43 | padding-bottom: 4em; |
paul@318 | 44 | font-size: 14pt; font-family: monospace; |
paul@318 | 45 | background-color: black; color: white; |
paul@318 | 46 | } |
paul@318 | 47 | |
paul@318 | 48 | .nowrap { white-space: nowrap; } |
paul@318 | 49 | |
paul@318 | 50 | .class { margin-top: 1em; margin-bottom: 1em; } |
paul@318 | 51 | .function { margin-top: 1em; margin-bottom: 1em; } |
paul@318 | 52 | .body { padding-left: 2em; } |
paul@318 | 53 | .keyword { color: yellow; } |
paul@318 | 54 | .comment { color: blue; } |
paul@318 | 55 | .class-name { color: cyan; } |
paul@318 | 56 | .function-name { color: cyan; } |
paul@318 | 57 | .str { color: #FF00FF; } |
paul@318 | 58 | .doc { color: #FF00FF; margin-top: 1em; margin-bottom: 1em; } |
paul@318 | 59 | .invocation a { color: white; text-decoration: none; } |
paul@318 | 60 | |
paul@318 | 61 | .popup { |
paul@318 | 62 | display: none; z-index: 2; |
paul@318 | 63 | position: absolute; top: 2ex; left: 0; |
paul@318 | 64 | padding: 0.2em; background-color: #000000; color: white; |
paul@318 | 65 | border: 2px solid #dddddd; |
paul@318 | 66 | } |
paul@318 | 67 | |
paul@318 | 68 | .invocations { |
paul@318 | 69 | padding: 0.5em; background-color: #770000; |
paul@318 | 70 | clear: all; |
paul@318 | 71 | } |
paul@318 | 72 | |
paul@318 | 73 | .types { |
paul@318 | 74 | padding: 0.5em; background-color: #0000FF; |
paul@318 | 75 | float: right; |
paul@318 | 76 | } |
paul@318 | 77 | |
paul@318 | 78 | .raises { |
paul@318 | 79 | padding: 0.5em; background-color: #7700FF; |
paul@318 | 80 | float: right; |
paul@318 | 81 | } |
paul@318 | 82 | |
paul@318 | 83 | .scopes { |
paul@318 | 84 | padding: 0.5em; background-color: #007700; |
paul@318 | 85 | float: left; |
paul@318 | 86 | } |
paul@318 | 87 | |
paul@318 | 88 | .non-scopes { |
paul@318 | 89 | padding: 0.5em; background-color: #FF0000; |
paul@318 | 90 | float: left; |
paul@318 | 91 | } |
paul@318 | 92 | |
paul@318 | 93 | .no-types { |
paul@318 | 94 | background-color: #FF0000; |
paul@318 | 95 | } |
paul@318 | 96 | |
paul@318 | 97 | .op, |
paul@318 | 98 | .name, |
paul@318 | 99 | .attr, |
paul@318 | 100 | .conditional, |
paul@318 | 101 | .operator, |
paul@318 | 102 | .iterator, |
paul@318 | 103 | .call, |
paul@318 | 104 | .returns, |
paul@318 | 105 | .failure |
paul@318 | 106 | { |
paul@318 | 107 | position: relative; |
paul@318 | 108 | } |
paul@318 | 109 | |
paul@318 | 110 | .op:hover > .popup, |
paul@318 | 111 | .name:hover > .popup, |
paul@318 | 112 | .attr:hover > .popup, |
paul@318 | 113 | .conditional:hover > .popup, |
paul@318 | 114 | .operator:hover > .popup, |
paul@318 | 115 | .iterator:hover > .popup, |
paul@318 | 116 | .call:hover > .popup, |
paul@318 | 117 | .returns:hover > .popup, |
paul@318 | 118 | .failure:hover > .popup |
paul@318 | 119 | { |
paul@318 | 120 | display: block; |
paul@318 | 121 | } |
paul@318 | 122 | |
paul@318 | 123 | .summary-class { |
paul@318 | 124 | background-color: #004400; |
paul@318 | 125 | } |
paul@318 | 126 | |
paul@318 | 127 | .summary-instance { |
paul@318 | 128 | background-color: #0000FF; |
paul@318 | 129 | } |
paul@318 | 130 | |
paul@318 | 131 | .summary-attr { |
paul@318 | 132 | background-color: #007700; |
paul@318 | 133 | } |
paul@318 | 134 | |
paul@318 | 135 | </style> |
paul@318 | 136 | </head> |
paul@318 | 137 | <body> |
paul@318 | 138 | """ |
paul@318 | 139 | |
paul@318 | 140 | html_footer = """</body> |
paul@318 | 141 | </html> |
paul@318 | 142 | """ |
paul@318 | 143 | |
paul@318 | 144 | # Utility classes. |
paul@318 | 145 | |
paul@318 | 146 | class Writer: |
paul@318 | 147 | |
paul@318 | 148 | "A utility class providing useful HTML output methods." |
paul@318 | 149 | |
paul@318 | 150 | # Methods which return strings. |
paul@318 | 151 | |
paul@318 | 152 | def _text(self, text): |
paul@318 | 153 | return text.replace("&", "&").replace("<", "<").replace(">", ">") |
paul@318 | 154 | |
paul@318 | 155 | def _attr(self, attr): |
paul@318 | 156 | return self._text(attr).replace("'", "'").replace('"', """) |
paul@318 | 157 | |
paul@318 | 158 | def _url(self, url): |
paul@318 | 159 | return self._attr(url).replace("#", "%23").replace("-", "%2d") |
paul@318 | 160 | |
paul@318 | 161 | # Methods which write to the stream. |
paul@318 | 162 | |
paul@318 | 163 | def _comment(self, comment): |
paul@318 | 164 | self.stream.write("<span class='comment'># %s</span>\n" % comment) |
paul@318 | 165 | |
paul@318 | 166 | def _keyword(self, kw): |
paul@318 | 167 | self.stream.write("<span class='keyword'>%s</span> " % kw) |
paul@318 | 168 | |
paul@318 | 169 | def _doc(self, node): |
paul@318 | 170 | if node.doc is not None: |
paul@318 | 171 | self.stream.write("<pre class='doc'>\n") |
paul@318 | 172 | self.stream.write('"""') |
paul@318 | 173 | output = textwrap.dedent(node.doc.replace('"""', '\\"\\"\\"')) |
paul@318 | 174 | self.stream.write(self._text(output)) |
paul@318 | 175 | self.stream.write('"""') |
paul@318 | 176 | self.stream.write("</pre>\n") |
paul@318 | 177 | |
paul@318 | 178 | def _name(self, name): |
paul@318 | 179 | self.stream.write("<span class='name'>%s</span>\n" % name) |
paul@318 | 180 | |
paul@320 | 181 | def _name_link(self, module_name, full_name, name): |
paul@320 | 182 | self.stream.write("<a class='name' href='%s%sxhtml#%s'>%s</a>" % ( |
paul@320 | 183 | module_name, os.path.extsep, self._attr(full_name), self._text(name))) |
paul@320 | 184 | |
paul@320 | 185 | def _summary_link(self, module_name, full_name, name): |
paul@320 | 186 | self.stream.write("<a class='name' href='%s-summary%sxhtml#%s'>%s</a>" % ( |
paul@320 | 187 | module_name, os.path.extsep, self._attr(full_name), self._text(name))) |
paul@320 | 188 | |
paul@320 | 189 | def _object_name_def(self, module, obj): |
paul@320 | 190 | |
paul@320 | 191 | """ |
paul@320 | 192 | Link to the summary for 'module' using 'obj'. |
paul@320 | 193 | """ |
paul@320 | 194 | |
paul@320 | 195 | self._summary_link(module.full_name(), obj.full_name(), obj.name) |
paul@320 | 196 | |
paul@318 | 197 | # Summary classes. |
paul@318 | 198 | |
paul@320 | 199 | class Summary(Writer): |
paul@318 | 200 | |
paul@319 | 201 | "Summarise classes and attributes in modules." |
paul@319 | 202 | |
paul@319 | 203 | def __init__(self, module): |
paul@319 | 204 | self.module = module |
paul@318 | 205 | |
paul@319 | 206 | def to_stream(self, stream): |
paul@319 | 207 | |
paul@319 | 208 | "Write the summary to the given 'stream'." |
paul@319 | 209 | |
paul@319 | 210 | self.stream = stream |
paul@318 | 211 | self._init_details() |
paul@318 | 212 | self.stream.write(html_header) |
paul@319 | 213 | self._write_classes(self.module) |
paul@318 | 214 | self.stream.write(html_footer) |
paul@318 | 215 | |
paul@318 | 216 | def _write_classes(self, module): |
paul@318 | 217 | self.stream.write("<table cellspacing='5' cellpadding='5'>\n") |
paul@318 | 218 | |
paul@318 | 219 | all_classes = {} |
paul@318 | 220 | |
paul@318 | 221 | for obj in self.module.all_objects: |
paul@318 | 222 | if isinstance(obj, Class): |
paul@318 | 223 | all_classes[obj.name] = obj |
paul@318 | 224 | |
paul@318 | 225 | all_class_names = all_classes.keys() |
paul@318 | 226 | all_class_names.sort() |
paul@318 | 227 | |
paul@318 | 228 | for name in all_class_names: |
paul@318 | 229 | self._write_class(all_classes[name]) |
paul@318 | 230 | |
paul@318 | 231 | self.stream.write("</table>\n") |
paul@318 | 232 | |
paul@318 | 233 | def _write_class(self, obj): |
paul@318 | 234 | |
paul@318 | 235 | # Write the class... |
paul@318 | 236 | |
paul@318 | 237 | self.stream.write("<tbody class='class'>\n") |
paul@318 | 238 | self.stream.write("<tr>\n") |
paul@318 | 239 | self.stream.write("<th class='summary-class' id='%s'>\n" % obj.full_name()) |
paul@318 | 240 | self._keyword("class") |
paul@318 | 241 | self.stream.write(obj.name) |
paul@318 | 242 | self.stream.write("</th>\n") |
paul@318 | 243 | |
paul@318 | 244 | # ...and all known attribute names. |
paul@318 | 245 | |
paul@318 | 246 | obj_attributes = obj.all_attribute_names() |
paul@318 | 247 | |
paul@318 | 248 | for name in self.attribute_names: |
paul@318 | 249 | if name in obj_attributes: |
paul@318 | 250 | self.stream.write("<th class='summary-attr'>%s</th>\n" % self._text(name)) |
paul@318 | 251 | else: |
paul@318 | 252 | self.stream.write("<th></th>\n") |
paul@318 | 253 | self.stream.write("</tr>\n") |
paul@318 | 254 | |
paul@318 | 255 | self.stream.write("</tbody>\n") |
paul@318 | 256 | |
paul@318 | 257 | def _init_details(self): |
paul@318 | 258 | names = set() |
paul@318 | 259 | |
paul@318 | 260 | # Visit all classes. |
paul@318 | 261 | |
paul@318 | 262 | for obj in self.module.all_objects: |
paul@318 | 263 | if isinstance(obj, Class): |
paul@318 | 264 | names.update(obj.all_attribute_names()) |
paul@318 | 265 | |
paul@318 | 266 | self.attribute_names = list(names) |
paul@318 | 267 | self.attribute_names.sort() |
paul@318 | 268 | |
paul@320 | 269 | # Source code classes. |
paul@320 | 270 | |
paul@320 | 271 | class AnnotatedSource(ASTVisitor, Writer): |
paul@320 | 272 | |
paul@320 | 273 | "A module source code browser." |
paul@320 | 274 | |
paul@320 | 275 | def __init__(self, module): |
paul@320 | 276 | ASTVisitor.__init__(self) |
paul@320 | 277 | self.visitor = self |
paul@320 | 278 | self.module = module |
paul@320 | 279 | |
paul@320 | 280 | def to_stream(self, stream): |
paul@320 | 281 | |
paul@320 | 282 | "Write the annotated code to the given 'stream'." |
paul@320 | 283 | |
paul@320 | 284 | self.stream = stream |
paul@320 | 285 | self.stream.write(html_header) |
paul@320 | 286 | self.dispatch(self.module.module) |
paul@320 | 287 | self.stream.write(html_footer) |
paul@320 | 288 | |
paul@320 | 289 | def visitModule(self, node): |
paul@320 | 290 | self.default(node) |
paul@320 | 291 | |
paul@320 | 292 | # Statements. |
paul@320 | 293 | |
paul@320 | 294 | def visitAssert(self, node): |
paul@320 | 295 | self.stream.write("<div class='assert nowrap'>\n") |
paul@320 | 296 | self._keyword("assert") |
paul@320 | 297 | self.dispatch(node.test) |
paul@320 | 298 | if node.fail: |
paul@320 | 299 | self.stream.write(", ") |
paul@320 | 300 | self.dispatch(node.fail) |
paul@320 | 301 | self.stream.write("</div>\n") |
paul@320 | 302 | |
paul@320 | 303 | def visitAssign(self, node): |
paul@320 | 304 | self.stream.write("<div class='assign nowrap'>\n") |
paul@320 | 305 | for lvalue in node.nodes: |
paul@320 | 306 | self.dispatch(lvalue) |
paul@320 | 307 | self.stream.write("= ") |
paul@320 | 308 | self.dispatch(node.expr) |
paul@320 | 309 | self.stream.write("</div>\n") |
paul@320 | 310 | |
paul@320 | 311 | def visitAugAssign(self, node): |
paul@320 | 312 | self.stream.write("<div class='augassign nowrap'>\n") |
paul@320 | 313 | self.dispatch(node.node) |
paul@320 | 314 | self.stream.write("%s " % node.op) |
paul@320 | 315 | self.dispatch(node.expr) |
paul@320 | 316 | self.stream.write("</div>\n") |
paul@320 | 317 | |
paul@320 | 318 | def visitBreak(self, node): |
paul@320 | 319 | self.stream.write("<div class='break nowrap'>\n") |
paul@320 | 320 | self._keyword("break") |
paul@320 | 321 | self.stream.write("</div>\n") |
paul@320 | 322 | |
paul@320 | 323 | def visitClass(self, node): |
paul@320 | 324 | |
paul@320 | 325 | # Use inspected details where possible. |
paul@320 | 326 | |
paul@320 | 327 | if hasattr(node, "_def"): |
paul@320 | 328 | cls = node._def |
paul@320 | 329 | bases = cls.bases |
paul@320 | 330 | self.stream.write("<div class='class nowrap' id='%s'>\n" % cls.full_name()) |
paul@320 | 331 | else: |
paul@320 | 332 | print "Warning: class %s not recognised!" % node.name |
paul@320 | 333 | return |
paul@320 | 334 | |
paul@320 | 335 | # Write the declaration line. |
paul@320 | 336 | |
paul@320 | 337 | self.stream.write("<div>\n") |
paul@320 | 338 | self._keyword("class") |
paul@320 | 339 | self._object_name_def(self.module, cls) |
paul@320 | 340 | |
paul@320 | 341 | # Suppress the "object" class appearing alone. |
paul@320 | 342 | |
paul@320 | 343 | if bases and not (len(bases) == 1 and bases[0].name == "object"): |
paul@320 | 344 | self.stream.write("(") |
paul@320 | 345 | first = 1 |
paul@320 | 346 | for base in bases: |
paul@320 | 347 | if not first: |
paul@320 | 348 | self.stream.write(",\n") |
paul@320 | 349 | |
paul@320 | 350 | self._object_name_def(base.module, base) |
paul@320 | 351 | |
paul@320 | 352 | first = 0 |
paul@320 | 353 | self.stream.write(")") |
paul@320 | 354 | |
paul@320 | 355 | self.stream.write(":\n") |
paul@320 | 356 | self.stream.write("</div>\n") |
paul@320 | 357 | |
paul@320 | 358 | # Write the docstring and class body. |
paul@320 | 359 | |
paul@320 | 360 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 361 | self._doc(node) |
paul@320 | 362 | self.dispatch(node.code) |
paul@320 | 363 | self.stream.write("</div>\n") |
paul@320 | 364 | self.stream.write("</div>\n") |
paul@320 | 365 | |
paul@320 | 366 | def visitContinue(self, node): |
paul@320 | 367 | self.stream.write("<div class='continue nowrap'>\n") |
paul@320 | 368 | self._keyword("continue") |
paul@320 | 369 | self.stream.write("</div>\n") |
paul@320 | 370 | |
paul@320 | 371 | def visitDiscard(self, node): |
paul@320 | 372 | self.stream.write("<div class='discard nowrap'>\n") |
paul@320 | 373 | self.default(node) |
paul@320 | 374 | self.stream.write("</div>\n") |
paul@320 | 375 | |
paul@320 | 376 | def visitFor(self, node): |
paul@320 | 377 | self.stream.write("<div class='if nowrap'>\n") |
paul@320 | 378 | self.stream.write("<div>\n") |
paul@320 | 379 | self._keyword("for") |
paul@320 | 380 | self.dispatch(node.assign) |
paul@320 | 381 | self._keyword("in") |
paul@320 | 382 | self.dispatch(node.list) |
paul@320 | 383 | self.stream.write(":\n") |
paul@320 | 384 | self.stream.write("</div>\n") |
paul@320 | 385 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 386 | self.dispatch(node.body) |
paul@320 | 387 | self.stream.write("</div>\n") |
paul@320 | 388 | if node.else_ is not None: |
paul@320 | 389 | self.stream.write("<div>\n") |
paul@320 | 390 | self._keyword("else") |
paul@320 | 391 | self.stream.write(":\n") |
paul@320 | 392 | self.stream.write("</div>\n") |
paul@320 | 393 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 394 | self.dispatch(node.else_) |
paul@320 | 395 | self.stream.write("</div>\n") |
paul@320 | 396 | self.stream.write("</div>\n") |
paul@320 | 397 | |
paul@320 | 398 | def visitFrom(self, node): |
paul@320 | 399 | self.stream.write("<div class='from nowrap'>\n") |
paul@320 | 400 | self._keyword("from") |
paul@320 | 401 | self._name(node.modname) |
paul@320 | 402 | self._keyword("import") |
paul@320 | 403 | first = 1 |
paul@320 | 404 | for (name, alias), _name in map(None, node.names, node._names): |
paul@320 | 405 | if not first: |
paul@320 | 406 | self.stream.write(",\n") |
paul@320 | 407 | if alias: |
paul@320 | 408 | self.stream.write(name + " ") |
paul@320 | 409 | self._keyword("as") |
paul@320 | 410 | self._name(alias or name) |
paul@320 | 411 | first = 0 |
paul@320 | 412 | self.stream.write("</div>\n") |
paul@320 | 413 | |
paul@320 | 414 | def visitFunction(self, node): |
paul@320 | 415 | if hasattr(node, "_def"): |
paul@320 | 416 | fn = node._def |
paul@320 | 417 | self.stream.write("<div class='function nowrap' id='%s'>\n" % fn.full_name()) |
paul@320 | 418 | else: |
paul@320 | 419 | print "Warning: function %s not recognised!" % node.name |
paul@320 | 420 | return |
paul@320 | 421 | |
paul@320 | 422 | # Write the declaration line. |
paul@320 | 423 | |
paul@320 | 424 | self.stream.write("<div>\n") |
paul@320 | 425 | self._keyword("def") |
paul@320 | 426 | self._object_name_def(self.module, fn) |
paul@320 | 427 | |
paul@320 | 428 | self.stream.write("(") |
paul@320 | 429 | self._parameters(fn) |
paul@320 | 430 | self.stream.write(")") |
paul@320 | 431 | self.stream.write(":\n") |
paul@320 | 432 | self.stream.write("</div>\n") |
paul@320 | 433 | |
paul@320 | 434 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 435 | self._doc(node) |
paul@320 | 436 | self.dispatch(node.code) |
paul@320 | 437 | self.stream.write("</div>\n") |
paul@320 | 438 | self.stream.write("</div>\n") |
paul@320 | 439 | |
paul@320 | 440 | def visitGlobal(self, node): |
paul@320 | 441 | self.stream.write("<div class='global nowrap'>\n") |
paul@320 | 442 | self._keyword("global") |
paul@320 | 443 | first = 1 |
paul@320 | 444 | for name in node.names: |
paul@320 | 445 | if not first: |
paul@320 | 446 | self.stream.write(",\n") |
paul@320 | 447 | self.stream.write(name) |
paul@320 | 448 | first = 0 |
paul@320 | 449 | self.stream.write("</div>\n") |
paul@320 | 450 | |
paul@320 | 451 | def visitIf(self, node): |
paul@320 | 452 | self.stream.write("<div class='if nowrap'>\n") |
paul@320 | 453 | first = 1 |
paul@320 | 454 | for compare, stmt in node.tests: |
paul@320 | 455 | self.stream.write("<div>\n") |
paul@320 | 456 | if first: |
paul@320 | 457 | self._keyword("if") |
paul@320 | 458 | else: |
paul@320 | 459 | self._keyword("elif") |
paul@320 | 460 | self.dispatch(compare) |
paul@320 | 461 | self.stream.write(":\n") |
paul@320 | 462 | self.stream.write("</div>\n") |
paul@320 | 463 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 464 | self.dispatch(stmt) |
paul@320 | 465 | self.stream.write("</div>\n") |
paul@320 | 466 | first = 0 |
paul@320 | 467 | if node.else_ is not None: |
paul@320 | 468 | self.stream.write("<div>\n") |
paul@320 | 469 | self._keyword("else") |
paul@320 | 470 | self.stream.write(":\n") |
paul@320 | 471 | self.stream.write("</div>\n") |
paul@320 | 472 | self.stream.write("<div class='body nowrap'>\n") |
paul@320 | 473 | self.dispatch(node.else_) |
paul@320 | 474 | self.stream.write("</div>\n") |
paul@320 | 475 | self.stream.write("</div>\n") |
paul@320 | 476 | |
paul@320 | 477 | # Output preparation methods. |
paul@320 | 478 | |
paul@320 | 479 | def _sequence(self, node): |
paul@320 | 480 | first = 1 |
paul@320 | 481 | for n in node.nodes: |
paul@320 | 482 | if not first: |
paul@320 | 483 | self.stream.write(",\n") |
paul@320 | 484 | self.dispatch(n) |
paul@320 | 485 | first = 0 |
paul@320 | 486 | |
paul@320 | 487 | def _mapping(self, node): |
paul@320 | 488 | first = 1 |
paul@320 | 489 | for k, v in node.items: |
paul@320 | 490 | if not first: |
paul@320 | 491 | self.stream.write(",\n") |
paul@320 | 492 | self.dispatch(k) |
paul@320 | 493 | self.stream.write(":\n") |
paul@320 | 494 | self.dispatch(v) |
paul@320 | 495 | first = 0 |
paul@320 | 496 | |
paul@320 | 497 | def _parameters(self, fn): |
paul@320 | 498 | first = 1 |
paul@320 | 499 | nparams = len(fn.positional_names) |
paul@320 | 500 | ndefaults = len(fn.defaults) |
paul@320 | 501 | first_with_default = nparams - ndefaults |
paul@320 | 502 | |
paul@320 | 503 | for n, param in enumerate(fn.positional_names): |
paul@320 | 504 | if not first: |
paul@320 | 505 | self.stream.write(",\n") |
paul@320 | 506 | self._name(param) |
paul@320 | 507 | n_default = n - first_with_default |
paul@320 | 508 | if n_default >= 0: |
paul@320 | 509 | self._default(fn.defaults[n_default]) |
paul@320 | 510 | |
paul@320 | 511 | if fn.has_star: |
paul@320 | 512 | if not first: |
paul@320 | 513 | self.stream.write(", *\n") |
paul@320 | 514 | self._name(fn.star_name) |
paul@320 | 515 | |
paul@320 | 516 | if fn.has_dstar: |
paul@320 | 517 | if not first: |
paul@320 | 518 | self.stream.write(", **\n") |
paul@320 | 519 | self._name(fn.dstar_name) |
paul@320 | 520 | |
paul@320 | 521 | def _default(self, default): |
paul@320 | 522 | self.stream.write("=\n") |
paul@320 | 523 | self.dispatch(default) |
paul@320 | 524 | |
paul@318 | 525 | # Convenience functions. |
paul@318 | 526 | |
paul@320 | 527 | def summarise(module, filename): |
paul@318 | 528 | stream = open(filename, "wb") |
paul@318 | 529 | try: |
paul@320 | 530 | summary = Summary(module) |
paul@320 | 531 | summary.to_stream(stream) |
paul@320 | 532 | finally: |
paul@320 | 533 | stream.close() |
paul@320 | 534 | |
paul@320 | 535 | def annotate(module, filename): |
paul@320 | 536 | stream = open(filename, "wb") |
paul@320 | 537 | try: |
paul@320 | 538 | source = AnnotatedSource(module) |
paul@320 | 539 | source.to_stream(stream) |
paul@318 | 540 | finally: |
paul@318 | 541 | stream.close() |
paul@318 | 542 | |
paul@318 | 543 | # vim: tabstop=4 expandtab shiftwidth=4 |