feat(cli): add option to run python parser
This commit is contained in:
@@ -3,6 +3,9 @@ from typing import Optional, TextIO
|
||||
|
||||
import click
|
||||
|
||||
from midas.ast.printer import PythonAstPrinter
|
||||
from midas.parser.python import PythonParser
|
||||
|
||||
|
||||
@click.group()
|
||||
def midas():
|
||||
@@ -22,11 +25,28 @@ def utils():
|
||||
|
||||
@utils.command()
|
||||
@click.option("-o", "--output", type=click.File("w"))
|
||||
@click.option("-p", "--parse", is_flag=True)
|
||||
@click.argument("file", type=click.File("r"))
|
||||
def dump_ast(output: Optional[TextIO], file: TextIO):
|
||||
def dump_ast(output: Optional[TextIO], parse: bool, file: TextIO):
|
||||
source: str = file.read()
|
||||
tree: ast.Module = ast.parse(source, filename=file.name)
|
||||
dump: str = ast.dump(tree, indent=4)
|
||||
dump: str
|
||||
|
||||
if parse:
|
||||
parser = PythonParser()
|
||||
parser.visit(tree)
|
||||
printer = PythonAstPrinter()
|
||||
dump = ""
|
||||
for name, annotation in parser.annotations:
|
||||
dump += f"{name} = "
|
||||
if annotation is None:
|
||||
dump += "None"
|
||||
else:
|
||||
dump += printer.print(annotation)
|
||||
dump += "\n"
|
||||
else:
|
||||
dump = ast.dump(tree, indent=4)
|
||||
|
||||
if output is None:
|
||||
click.echo(dump)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user