1234567891011121314151617181920212223242526272829 |
- def main(args):
- parser = optparse.OptionParser(usage="astvisualizer.py [options] [string]")
- parser.add_option("-f", "--file", action="store",
- help="Read a code snippet from the specified file")
- parser.add_option("-l", "--label", action="store",
- help="The label for the visualization")
- options, args = parser.parse_args(args)
- if options.file:
- with open(options.file) as instream:
- code = instream.read()
- label = options.file
- elif len(args) == 2:
- code = args[1]
- label = "<code read from command line parameter>"
- else:
- print("Expecting Python code on stdin...")
- code = sys.stdin.read()
- label = "<code read from stdin>"
- if options.label:
- label = options.label
- code_ast = ast.parse(code)
- transformed_ast = transform_ast(code_ast)
- renderer = GraphRenderer()
- renderer.render(transformed_ast, label=label)
|