Skip to content

Commit 2eb8937

Browse files
committed
write_astModule: pathFilename: PathLike[Any] | PurePath | io.TextIOBase
1 parent fec8f65 commit 2eb8937

2 files changed

Lines changed: 44 additions & 43 deletions

File tree

astToolkit/transformationTools.py

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
maintaining semantic integrity and performance characteristics.
2626
"""
2727

28+
import io
2829
from astToolkit import (
2930
Be, DOT, Grab, IfThis, IngredientsFunction, IngredientsModule, Make, NodeChanger, NodeTourist, Then, )
3031
from autoflake import fix_code as autoflake_fix_code
@@ -255,6 +256,46 @@ def removeUnusedParameters(ingredientsFunction: IngredientsFunction) -> Ingredie
255256

256257
return ingredientsFunction
257258

259+
def unjoinBinOP(astAST: ast.AST, operator: type[ast.operator] = ast.operator) -> list[ast.expr]:
260+
"""
261+
Unjoin a binary operation AST node into a list of expressions.
262+
263+
(AI generated docstring)
264+
265+
This function takes an AST node representing a binary operation and recursively
266+
unjoins it into a flat list of expressions. It handles both binary operations
267+
and unary operations, ensuring that all nested expressions are extracted.
268+
269+
Parameters
270+
----------
271+
astAST : ast.AST
272+
The AST node to unjoin.
273+
operator : type[ast.operator] = ast.operator
274+
The type of binary operator to look for in the AST. Defaults to `ast.operator`.
275+
276+
Returns
277+
-------
278+
list[ast.expr]
279+
A list of expressions extracted from the binary operation AST node.
280+
"""
281+
list_ast_expr: list[ast.expr] = []
282+
workbench: list[ast.expr] = []
283+
284+
findThis = Be.BinOp.opIs(lambda this_op: isinstance(this_op, operator))
285+
doThat = Grab.andDoAllOf([Grab.leftAttribute(Then.appendTo(workbench)), Grab.rightAttribute(Then.appendTo(list_ast_expr))])
286+
breakingBinOp = NodeTourist(findThis, doThat)
287+
288+
breakingBinOp.visit(astAST)
289+
290+
while workbench:
291+
ast_expr = workbench.pop()
292+
if isinstance(ast_expr, ast.BinOp):
293+
breakingBinOp.visit(ast_expr)
294+
else:
295+
list_ast_expr.append(ast_expr)
296+
297+
return list_ast_expr
298+
258299
def unparseFindReplace(astTree: , mappingFindReplaceNodes: Mapping[ast.AST, ast.AST]) -> :
259300
"""
260301
Recursively replace AST (Abstract Syntax Tree) nodes based on a mapping of find-replace pairs.
@@ -295,7 +336,7 @@ def unparseFindReplace(astTree: 木, mappingFindReplaceNodes: Mapping[ast.AST, a
295336
astTree = deepcopy(newTree)
296337
return newTree
297338

298-
def write_astModule(ingredients: IngredientsModule, pathFilename: PathLike[Any] | PurePath, packageName: str | None = None) -> None:
339+
def write_astModule(ingredients: IngredientsModule, pathFilename: PathLike[Any] | PurePath | io.TextIOBase, packageName: str | None = None) -> None:
299340
"""
300341
Convert an IngredientsModule to Python source code and write it to a file.
301342
@@ -332,43 +373,3 @@ def write_astModule(ingredients: IngredientsModule, pathFilename: PathLike[Any]
332373
autoflake_additional_imports.append(packageName)
333374
pythonSource = autoflake_fix_code(pythonSource, autoflake_additional_imports, expand_star_imports=False, remove_all_unused_imports=True, remove_duplicate_keys = False, remove_unused_variables = False)
334375
writeStringToHere(pythonSource, pathFilename)
335-
336-
def unjoinBinOP(astAST: ast.AST, operator: type[ast.operator] = ast.operator) -> list[ast.expr]:
337-
"""
338-
Unjoin a binary operation AST node into a list of expressions.
339-
340-
(AI generated docstring)
341-
342-
This function takes an AST node representing a binary operation and recursively
343-
unjoins it into a flat list of expressions. It handles both binary operations
344-
and unary operations, ensuring that all nested expressions are extracted.
345-
346-
Parameters
347-
----------
348-
astAST : ast.AST
349-
The AST node to unjoin.
350-
operator : type[ast.operator] = ast.operator
351-
The type of binary operator to look for in the AST. Defaults to `ast.operator`.
352-
353-
Returns
354-
-------
355-
list[ast.expr]
356-
A list of expressions extracted from the binary operation AST node.
357-
"""
358-
list_ast_expr: list[ast.expr] = []
359-
workbench: list[ast.expr] = []
360-
361-
findThis = Be.BinOp.opIs(lambda this_op: isinstance(this_op, operator))
362-
doThat = Grab.andDoAllOf([Grab.leftAttribute(Then.appendTo(workbench)), Grab.rightAttribute(Then.appendTo(list_ast_expr))])
363-
breakingBinOp = NodeTourist(findThis, doThat)
364-
365-
breakingBinOp.visit(astAST)
366-
367-
while workbench:
368-
ast_expr = workbench.pop()
369-
if isinstance(ast_expr, ast.BinOp):
370-
breakingBinOp.visit(ast_expr)
371-
else:
372-
list_ast_expr.append(ast_expr)
373-
374-
return list_ast_expr

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "astToolkit"
3-
version = "0.7.0"
3+
version = "0.7.1"
44
description = "A powerfully composable, type-safe toolkit for Python abstract syntax tree (AST) manipulation, analysis, transformation, and code generation with a layered architecture designed for building sophisticated code processing assembly-lines."
55
readme = "README.md"
66
requires-python = ">=3.12"
@@ -48,7 +48,7 @@ classifiers = [
4848
urls = { Donate = "https://www.patreon.com/integrated", Homepage = "https://github.com/hunterhogan/astToolkit", Issues = "https://github.com/hunterhogan/astToolkit/issues", Repository = "https://github.com/hunterhogan/astToolkit.git" }
4949
dependencies = [
5050
"autoflake",
51-
"hunterMakesPy",
51+
"hunterMakesPy>=0.2.1",
5252
"typing_extensions>=4.10.0",
5353
]
5454
optional-dependencies = { development = [

0 commit comments

Comments
 (0)