def generate_pyast(code):
"""
Parses the code and creates a structure of lists and dicts only.
:param code: the code as a string
:return: a structure of lists and dicts only, representing the ast of code
"""
import ast
def transform_ast(code_ast):
if isinstance(code_ast, ast.AST):
# noinspection PyProtectedMember
node = {to_camelcase(k): transform_ast(getattr(code_ast, k)) for k in code_ast._fields}
node['node_type'] = to_camelcase(code_ast.__class__.__name__)
return node
elif isinstance(code_ast, list):
return [transform_ast(el) for el in code_ast]
else:
return code_ast
return transform_ast(ast.parse(code))
评论列表
文章目录