Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: recipe_engine/third_party/astunparse/printer.py

Issue 2387763003: Add initial postprocess unit test thingy. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 from __future__ import unicode_literals
2 import sys
3 import ast
4 import six
5
6
7 class Printer(ast.NodeVisitor):
8
9 def __init__(self, file=sys.stdout, indent=" "):
10 self.indentation = 0
11 self.indent_with = indent
12 self.f = file
13
14 # overridden to make the API obvious
15 def visit(self, node):
16 super(Printer, self).visit(node)
17
18 def write(self, text):
19 self.f.write(six.text_type(text))
20
21 def generic_visit(self, node):
22
23 if isinstance(node, list):
24 nodestart = "["
25 nodeend = "]"
26 children = [("", child) for child in node]
27 else:
28 nodestart = type(node).__name__ + "("
29 nodeend = ")"
30 children = [(name + "=", value) for name, value in ast.iter_fields(n ode)]
31
32 if len(children) > 1:
33 self.indentation += 1
34
35 self.write(nodestart)
36 for i, pair in enumerate(children):
37 attr, child = pair
38 if len(children) > 1:
39 self.write("\n" + self.indent_with * self.indentation)
40 if isinstance(child, (ast.AST, list)):
41 self.write(attr)
42 self.visit(child)
43 else:
44 self.write(attr + repr(child))
45
46 if i != len(children) - 1:
47 self.write(",")
48 self.write(nodeend)
49
50 if len(children) > 1:
51 self.indentation -= 1
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698