-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtree.py
106 lines (84 loc) · 4.17 KB
/
tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import ast
from typing import List, Tuple
from util import parsetree
def change_textvariable_to_variable(source_code: str) -> str:
"""Change occurrences of the parameter 'textvariable' to 'variable' in function calls and class instantiations.
Args:
source_code (str): The source code to modify.
Returns:
str: The modified source code with 'textvariable' replaced by 'variable'.
"""
class TextVariableVisitor(ast.NodeTransformer):
"""AST visitor to modify occurrences of 'textvariable' to 'variable'."""
def visit_Call(self, node: ast.Call) -> ast.Call:
"""Visit function calls and replace 'textvariable' keyword argument with 'variable'."""
if isinstance(node.func, ast.Name):
if node.keywords:
for keyword in node.keywords:
if (
isinstance(keyword, ast.keyword)
and keyword.arg == "textvariable"
):
keyword.arg = "variable"
return node
def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef:
"""Visit class definitions and replace 'textvariable' keyword argument with 'variable'."""
for child in node.body:
if isinstance(child, ast.Expr) and isinstance(child.value, ast.Call):
call = child.value
if isinstance(call.func, ast.Name):
if call.keywords:
for keyword in call.keywords:
if (
isinstance(keyword, ast.keyword)
and keyword.arg == "textvariable"
):
keyword.arg = "variable"
return node
# Parse the source code into an AST
tree = parsetree(source_code)
# Transform the AST with the TextVariableVisitor
transformer = TextVariableVisitor()
transformed_tree = transformer.visit(tree)
# Convert the AST back to source code
modified_source_code = ast.unparse(transformed_tree)
return modified_source_code
def change_orient_to_orientation(source_code: str) -> str:
"""Change occurrences of the parameter 'orient' to 'orientation' in function calls and class instantiations.
Args:
source_code (str): The source code to modify.
Returns:
str: The modified source code with 'orient' replaced by 'orientation'.
"""
class OrientVisitor(ast.NodeTransformer):
"""AST visitor to modify occurrences of 'orient' to 'orientation'."""
def visit_Call(self, node: ast.Call) -> ast.Call:
"""Visit function calls and replace 'orient' keyword argument with 'orientation'."""
if isinstance(node.func, ast.Name):
if node.keywords:
for keyword in node.keywords:
if isinstance(keyword, ast.keyword) and keyword.arg == "orient":
keyword.arg = "orientation"
return node
def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef:
"""Visit class definitions and replace 'orient' keyword argument with 'orientation'."""
for child in node.body:
if isinstance(child, ast.Expr) and isinstance(child.value, ast.Call):
call = child.value
if isinstance(call.func, ast.Name):
if call.keywords:
for keyword in call.keywords:
if (
isinstance(keyword, ast.keyword)
and keyword.arg == "orient"
):
keyword.arg = "orientation"
return node
# Parse the source code into an AST
tree = ast.parse(source_code)
# Transform the AST with the OrientVisitor
transformer = OrientVisitor()
transformed_tree = transformer.visit(tree)
# Convert the AST back to source code
modified_source_code = ast.unparse(transformed_tree)
return modified_source_code