~jelmer/pydoctor/unstable

« back to all changes in this revision

Viewing changes to pydoctor/ast_pp.py

  • Committer: Jelmer Vernooij
  • Date: 2010-03-29 14:44:55 UTC
  • mfrom: (1.1.2)
  • Revision ID: git-v1:9adf00d9819bc97fa35411628c0cec46d6000e05
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# this is stolen from exarkun's sandbox
2
 
import sys
3
 
 
4
 
from StringIO import StringIO
5
 
from pprint import pprint
 
1
"""Convert compiler package AST nodes into Python source code.
 
2
 
 
3
The entry point is the L{pp} function.
 
4
 
 
5
This is mostly used to present certain nodes in the rendered documentation -- for
 
6
example any default values for a function's arguments are rendered using L{pp}.
 
7
 
 
8
The code was stolen from exarkun's svn.twistedmatrix.com sandbox:
 
9
U{http://twistedmatrix.com/trac/browser/sandbox/exarkun/ast/ast_pp.py}
 
10
"""
 
11
 
 
12
 
 
13
from cStringIO import StringIO
6
14
from compiler import parse, walk
7
15
 
8
16
class SourceWriter(object):
318
326
        return self.s.getvalue()
319
327
 
320
328
def pp(ast):
 
329
    """Convert C{ast} to Python source.
 
330
 
 
331
    @param ast: The node to render into Python source.
 
332
    """
321
333
    sw = SourceWriter()
322
334
    walk(ast, sw)
323
335
    return sw.s.getvalue()
324
 
 
325
 
def magic(s):
326
 
    ast = parse(s)
327
 
    sw = SourceWriter()
328
 
    walk(ast, sw)
329
 
    return ast, sw
330
 
 
331
 
if __name__ == '__main__':
332
 
    f = file(__file__, 'r').read()
333
 
    ast, sw = magic(f)
334
 
    print sw
335
 
    print ast