Skip to content

Commit

Permalink
document public api #63
Browse files Browse the repository at this point in the history
  • Loading branch information
masonproffitt committed Jan 26, 2024
1 parent e1a0cf5 commit 5719c9c
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 6 deletions.
9 changes: 3 additions & 6 deletions qastle/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from .ast_util import *
from .columns_util import *
from .linq_util import *
from .parse import *
from .transform import *
from .translate import *
from .ast_util import unwrap_ast, wrap_ast
from .linq_util import insert_linq_nodes, remove_linq_nodes
from .translate import python_ast_to_text_ast, text_ast_to_python_ast
26 changes: 26 additions & 0 deletions qastle/ast_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@


def unwrap_ast(module_node):
"""
Extract a Python AST from an ast.Module node
Parameters
----------
python_ast : ast.AST
Python AST wrapped in an ast.Module
Returns
-------
ast.AST
Bare Python AST without an ast.Module
"""
if len(module_node.body) == 0:
return None
else:
Expand All @@ -11,6 +24,19 @@ def unwrap_ast(module_node):


def wrap_ast(node=None):
"""
Insert a Python AST into an ast.Module node
Parameters
----------
python_ast : ast.AST
Python AST to be wrapped
Returns
-------
ast.AST
Python AST wrapped in an ast.Module
"""
if node is None:
body_list = []
else:
Expand Down
26 changes: 26 additions & 0 deletions qastle/linq_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,19 @@ def visit_Call(self, node):


def insert_linq_nodes(python_ast):
"""
Insert LINQ nodes into a Python AST
Parameters
----------
python_ast : ast.AST
Python AST without LINQ nodes
Returns
-------
ast.AST
Python AST with LINQ nodes inserted
"""
return InsertLINQNodesTransformer().visit(python_ast)


Expand All @@ -250,4 +263,17 @@ class RemoveLINQNodesTransformer(ast.NodeTransformer):


def remove_linq_nodes(python_ast):
"""
Remove LINQ nodes from a Python AST
Parameters
----------
python_ast : ast.AST
Python AST with LINQ nodes
Returns
-------
ast.AST
Python AST without LINQ nodes
"""
return RemoveLINQNodesTransformer().visit(python_ast)
26 changes: 26 additions & 0 deletions qastle/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,35 @@ def python_source_to_text_ast(python_source):


def python_ast_to_text_ast(python_ast):
"""
Create a qastle text AST from a native Python AST
Parameters
----------
python_ast : ast.AST
Python AST to translate
Returns
-------
str
Translated qastle AST as a text string
"""
return PythonASTToTextASTTransformer().visit(python_ast)


def text_ast_to_python_ast(text_ast):
"""
Create a native Python AST from a qastle text AST
Parameters
----------
test_ast : str
qastle AST to translate
Returns
-------
ast.AST
Translated Python AST
"""
tree = parse(text_ast)
return TextASTToPythonASTTransformer().transform(tree)
2 changes: 2 additions & 0 deletions tests/test_ast_language.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .testing_util import *

from qastle import *
from qastle.linq_util import *
from qastle.translate import *

import ast

Expand Down
2 changes: 2 additions & 0 deletions tests/test_columns_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .testing_util import *

from qastle import *
from qastle.columns_util import *
from qastle.linq_util import *

import ast

Expand Down
1 change: 1 addition & 0 deletions tests/test_linq_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .testing_util import *

from qastle import *
from qastle.linq_util import *

import ast
import copy
Expand Down

0 comments on commit 5719c9c

Please sign in to comment.