summaryrefslogtreecommitdiff
path: root/test/test_readers/test_python/showast
diff options
context:
space:
mode:
authorwiemann <wiemann@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2006-01-09 20:44:25 +0000
committerwiemann <wiemann@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2006-01-09 20:44:25 +0000
commitd77fdfef70e08114f57cbef5d91707df8717ea9f (patch)
tree49444e3486c0c333cb7b33dfa721296c08ee4ece /test/test_readers/test_python/showast
parent53cd16ca6ca5f638cbe5956988e88f9339e355cf (diff)
parent3993c4097756e9885bcfbd07cb1cc1e4e95e50e4 (diff)
downloaddocutils-0.4.tar.gz
Release 0.4: tagging released revisiondocutils-0.4
git-svn-id: http://svn.code.sf.net/p/docutils/code/tags/docutils-0.4@4268 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'test/test_readers/test_python/showast')
-rwxr-xr-xtest/test_readers/test_python/showast57
1 files changed, 57 insertions, 0 deletions
diff --git a/test/test_readers/test_python/showast b/test/test_readers/test_python/showast
new file mode 100755
index 000000000..e7d846307
--- /dev/null
+++ b/test/test_readers/test_python/showast
@@ -0,0 +1,57 @@
+#! /usr/bin/env python
+
+"""
+This is a tool for exploring abstract syntax trees generated by
+``compiler.parse()`` from test data in
+docutils/test/test_readers/test_python/test_parser or stdin.
+
+Usage::
+
+ showast <key> <index>
+
+ showast < <module.py>
+
+Where ``<key>`` is the key to the ``totest`` dictionary, and ``<index>`` is
+the index of the list ``totest[key]``. If no arguments are given, stdin is
+used for input.
+"""
+
+import sys
+import compiler
+from compiler.ast import Node
+import test_parser
+
+def pformat(ast, indent=' ', level=0):
+ assert isinstance(ast, Node), 'ast is not a Node: %r' % (ast,)
+ atts = {}
+ for name, value in vars(ast).items():
+ if not value or isinstance(value, Node):
+ continue
+ if isinstance(value, list):
+ if isinstance(value[0], Node):
+ continue
+ if isinstance(value[0], tuple) and value[0] \
+ and isinstance(value[0][0], Node):
+ continue
+ atts[name] = str(value).encode('unicode-escape')
+ attlist = atts.items()
+ attlist.sort()
+ parts = [ast.__class__.__name__]
+ for name, value in attlist:
+ parts.append('%s="%s"' % (name, value))
+ result = ['%s<%s>\n' % (indent * level, ' '.join(parts))]
+ for node in ast.getChildNodes():
+ result.extend(pformat(node, level=level+1))
+ return result
+
+if len(sys.argv) > 1:
+ key, caseno = sys.argv[1:]
+ print 'totest["%s"][%s][0]:\n' % (key, caseno)
+ input_text = test_parser.totest[key][int(caseno)][0]
+else:
+ input_text = sys.stdin.read()
+print input_text
+module = compiler.parse(input_text)
+print module
+print
+print ''.join(pformat(module)),