changeset 35:40c2d04622b8

The pretty printer now take an file or StringIO object as argument.
author Janus Dam Nielsen <janus.nielsen@alexandra.dk>
date Tue, 26 May 2009 11:34:10 +0200
parents bee72885efaf
children 54af6d026211
files pysmcl/pretty_print.py
diffstat 1 files changed, 8 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/pysmcl/pretty_print.py	Tue May 26 11:33:57 2009 +0200
+++ b/pysmcl/pretty_print.py	Tue May 26 11:34:10 2009 +0200
@@ -116,6 +116,7 @@
     *prec* is the precedence of the surrounding of the expression,
     and determines if parenthesis are neccesary.
     >>> from ast import *
+    >>> import StringIO
     >>> from pysmcl.pretty_print import *
     >>> expr_string(BinOp(op=Mult(), left=BinOp(op=Add(), left=Num(1),
     ... right=Num(4)), right=Num(2)))
@@ -190,7 +191,7 @@
             (expr_string(exp.func, my_precedence),
              ", ".join(args_list))
     elif(isinstance(exp, dict)):
-        print exp
+        print >>output, exp
         assert False, "Type error"
     elif(isinstance(exp, ast.Subscript)):
         my_precedence = 21
@@ -248,6 +249,9 @@
 class PrettyPrinter(ast.NodeVisitor):
     indent = 0
 
+    def __init__(self, output):
+        self.output = output
+
     def print_body(self, body):
         self.indent += 1
         for i in body:
@@ -255,7 +259,7 @@
         self.indent -= 1
 
     def print_indented(self, str):
-        print (" "*(self.indent*4))+str
+        print >>self.output, (" "*(self.indent*4))+str
 
     def visit_ClassDef(self, node):
         for i in node.decorator_list:
@@ -361,8 +365,8 @@
         self.print_body(node.body)
 
 
-def pprint(module):
+def pprint(module, output):
     """Pretty prints the module represented by the ast node in
     ***module***
     """
-    PrettyPrinter().visit(module)
+    PrettyPrinter(output).visit(module)