2021-06-01 04:55:07 +00:00
|
|
|
"""
|
|
|
|
A (toy) tool for emitting Python ASTs as YAML formatted data.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import ast
|
|
|
|
import optparse
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
def propnames(node):
|
2021-08-30 04:35:10 +00:00
|
|
|
"""return names of attributes specific for the current node"""
|
2021-06-01 04:55:07 +00:00
|
|
|
|
2021-08-30 04:35:10 +00:00
|
|
|
props = {x for x in dir(node) if not x.startswith("_")}
|
2021-06-01 04:55:07 +00:00
|
|
|
|
2021-08-30 04:35:10 +00:00
|
|
|
if isinstance(node, ast.Module):
|
|
|
|
props -= {"body"}
|
2021-06-01 04:55:07 +00:00
|
|
|
|
2021-08-30 04:35:10 +00:00
|
|
|
if isinstance(node, (ast.Expr, ast.Attribute)):
|
|
|
|
props -= {"value"}
|
2021-06-01 04:55:07 +00:00
|
|
|
|
2021-08-30 04:35:10 +00:00
|
|
|
if isinstance(node, ast.Constant):
|
|
|
|
props -= {"n", "s"}
|
2021-06-01 04:55:07 +00:00
|
|
|
|
2021-08-30 04:35:10 +00:00
|
|
|
if isinstance(node, ast.ClassDef):
|
|
|
|
props -= {"body"}
|
2021-06-01 04:55:07 +00:00
|
|
|
|
2021-08-30 04:35:10 +00:00
|
|
|
return props
|
2021-06-01 04:55:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Note that ast.NodeTransformer exists for mutations.
|
|
|
|
# This is just for reads.
|
|
|
|
class TreeDumper(ast.NodeVisitor):
|
2021-08-30 04:35:10 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self._stack = []
|
2021-06-01 04:55:07 +00:00
|
|
|
|
2021-08-30 04:35:10 +00:00
|
|
|
def dump(self, node):
|
|
|
|
self.visit(node)
|
2021-06-01 04:55:07 +00:00
|
|
|
|
2021-08-30 04:35:10 +00:00
|
|
|
def visit(self, node):
|
2021-08-30 07:06:21 +00:00
|
|
|
# nodetype = type(node)
|
2021-08-30 04:35:10 +00:00
|
|
|
nodename = node.__class__.__name__
|
|
|
|
indent = " " * len(self._stack) * 2
|
|
|
|
print(indent + nodename)
|
|
|
|
for n in propnames(node):
|
|
|
|
print(indent + "%s: %s" % (n, node.__dict__[n]))
|
2021-06-01 04:55:07 +00:00
|
|
|
|
2021-08-30 04:35:10 +00:00
|
|
|
self._stack.append(node)
|
|
|
|
self.generic_visit(node)
|
|
|
|
self._stack.pop()
|
2021-06-01 04:55:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class YAMLTreeDumper(ast.NodeVisitor):
|
2021-08-30 04:35:10 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self._stack = []
|
|
|
|
|
|
|
|
def node2yml(self, node):
|
|
|
|
try:
|
2021-08-30 07:06:21 +00:00
|
|
|
# nodetype = type(node)
|
2021-08-30 04:35:10 +00:00
|
|
|
nodename = node.__class__.__name__
|
|
|
|
return {
|
|
|
|
"op": nodename,
|
|
|
|
"props": {n: node.__dict__[n] for n in propnames(node)},
|
|
|
|
"children": [],
|
|
|
|
}
|
2021-08-30 07:06:21 +00:00
|
|
|
except Exception:
|
2021-08-30 04:35:10 +00:00
|
|
|
print(repr(node), propnames(node), dir(node))
|
|
|
|
|
|
|
|
def visit(self, node):
|
|
|
|
yml_node = self.node2yml(node)
|
|
|
|
self._stack.append(yml_node)
|
|
|
|
old_stack = self._stack
|
|
|
|
self._stack = yml_node["children"]
|
|
|
|
self.generic_visit(node)
|
|
|
|
self._stack = old_stack
|
|
|
|
return yml_node
|
2021-06-01 04:55:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-08-30 04:35:10 +00:00
|
|
|
parser = optparse.OptionParser(usage="%prog [options] <filename.py>")
|
|
|
|
opts, args = parser.parse_args()
|
2021-06-01 04:55:07 +00:00
|
|
|
|
2021-08-30 04:35:10 +00:00
|
|
|
if len(args) == 0:
|
|
|
|
parser.print_help()
|
|
|
|
sys.exit(-1)
|
|
|
|
filename = args[0]
|
2021-06-01 04:55:07 +00:00
|
|
|
|
2021-08-30 04:35:10 +00:00
|
|
|
with open(filename) as f:
|
|
|
|
root = ast.parse(f.read(), filename)
|
2021-06-01 04:55:07 +00:00
|
|
|
|
2021-09-03 04:10:35 +00:00
|
|
|
print(
|
|
|
|
yaml.dump(
|
|
|
|
YAMLTreeDumper().visit(root), default_flow_style=False, sort_keys=False
|
|
|
|
)
|
|
|
|
)
|