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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: recipe_engine/third_party/astunparse/printer.py
diff --git a/recipe_engine/third_party/astunparse/printer.py b/recipe_engine/third_party/astunparse/printer.py
new file mode 100644
index 0000000000000000000000000000000000000000..92d64f772bff92956f71aa41b806f9f57d074d04
--- /dev/null
+++ b/recipe_engine/third_party/astunparse/printer.py
@@ -0,0 +1,51 @@
+from __future__ import unicode_literals
+import sys
+import ast
+import six
+
+
+class Printer(ast.NodeVisitor):
+
+ def __init__(self, file=sys.stdout, indent=" "):
+ self.indentation = 0
+ self.indent_with = indent
+ self.f = file
+
+ # overridden to make the API obvious
+ def visit(self, node):
+ super(Printer, self).visit(node)
+
+ def write(self, text):
+ self.f.write(six.text_type(text))
+
+ def generic_visit(self, node):
+
+ if isinstance(node, list):
+ nodestart = "["
+ nodeend = "]"
+ children = [("", child) for child in node]
+ else:
+ nodestart = type(node).__name__ + "("
+ nodeend = ")"
+ children = [(name + "=", value) for name, value in ast.iter_fields(node)]
+
+ if len(children) > 1:
+ self.indentation += 1
+
+ self.write(nodestart)
+ for i, pair in enumerate(children):
+ attr, child = pair
+ if len(children) > 1:
+ self.write("\n" + self.indent_with * self.indentation)
+ if isinstance(child, (ast.AST, list)):
+ self.write(attr)
+ self.visit(child)
+ else:
+ self.write(attr + repr(child))
+
+ if i != len(children) - 1:
+ self.write(",")
+ self.write(nodeend)
+
+ if len(children) > 1:
+ self.indentation -= 1

Powered by Google App Engine
This is Rietveld 408576698