|
25 | 25 | maintaining semantic integrity and performance characteristics. |
26 | 26 | """ |
27 | 27 |
|
| 28 | +import io |
28 | 29 | from astToolkit import ( |
29 | 30 | Be, DOT, Grab, IfThis, IngredientsFunction, IngredientsModule, Make, NodeChanger, NodeTourist, Then, 木) |
30 | 31 | from autoflake import fix_code as autoflake_fix_code |
@@ -255,6 +256,46 @@ def removeUnusedParameters(ingredientsFunction: IngredientsFunction) -> Ingredie |
255 | 256 |
|
256 | 257 | return ingredientsFunction |
257 | 258 |
|
| 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 | + |
258 | 299 | def unparseFindReplace(astTree: 木, mappingFindReplaceNodes: Mapping[ast.AST, ast.AST]) -> 木: |
259 | 300 | """ |
260 | 301 | 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 |
295 | 336 | astTree = deepcopy(newTree) |
296 | 337 | return newTree |
297 | 338 |
|
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: |
299 | 340 | """ |
300 | 341 | Convert an IngredientsModule to Python source code and write it to a file. |
301 | 342 |
|
@@ -332,43 +373,3 @@ def write_astModule(ingredients: IngredientsModule, pathFilename: PathLike[Any] |
332 | 373 | autoflake_additional_imports.append(packageName) |
333 | 374 | 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) |
334 | 375 | 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 |
|
0 commit comments