Recurse through a node, pretty-printing it.
(node, level, indent, write)
| 17 | stream.write('\n') |
| 18 | |
| 19 | def rec_node(node, level, indent, write): |
| 20 | "Recurse through a node, pretty-printing it." |
| 21 | pfx = indent * level |
| 22 | if isinstance(node, Node): |
| 23 | write(pfx) |
| 24 | write(node.__class__.__name__) |
| 25 | write('(') |
| 26 | |
| 27 | if any(isinstance(child, Node) for child in node.getChildren()): |
| 28 | for i, child in enumerate(node.getChildren()): |
| 29 | if i != 0: |
| 30 | write(',') |
| 31 | write('\n') |
| 32 | rec_node(child, level+1, indent, write) |
| 33 | write('\n') |
| 34 | write(pfx) |
| 35 | else: |
| 36 | # None of the children as nodes, simply join their repr on a single |
| 37 | # line. |
| 38 | write(', '.join(repr(child) for child in node.getChildren())) |
| 39 | |
| 40 | write(')') |
| 41 | |
| 42 | else: |
| 43 | write(pfx) |
| 44 | write(repr(node)) |
| 45 | |
| 46 | |
| 47 | def main(): |