-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcode_refactor_helper.py
More file actions
97 lines (82 loc) · 3.37 KB
/
code_refactor_helper.py
File metadata and controls
97 lines (82 loc) · 3.37 KB
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
#!python {0}
import re, os, sys
from typing import Callable
def refactor_0(code: str) -> str:
'''
Replaces the 'Initialize(title, category)' call from CreateNode with properties.
'''
search_for = r"Initialize\((.*?), (.*?)\);"
add_after = r"public class (.*?) \{"
remove_line = r"\s+Initialize\((.*?)\);"
# Find title and category
search_result = re.search(search_for, code)
if search_result is None:
return code
title = search_result.group(1)
category = search_result.group(2)
added_code = f'''
protected override string Title => {title};
protected override NodeCategory Category => {category};
'''
add_after_result = re.search(add_after, code)
add_after_position = add_after_result.end()
# Remove Initialize()
remove_line_result = re.search(remove_line, code)
remove_line_start = remove_line_result.start()
remove_line_end = remove_line_result.end()
code_without_removed_line_start = code[:remove_line_start].strip()
code_without_removed_line_end = code[remove_line_end:].strip()
code_without_removed_line = code_without_removed_line_start + '\n ' + code_without_removed_line_end
return code_without_removed_line[:add_after_position].strip() + added_code.rstrip() + '\n\n ' + code_without_removed_line[add_after_position:].strip()
def refactor_1(code: str) -> str:
'''
Changes the access modifiers of CreateNode and BindPorts to protected
'''
create_re = r'public override void CreateNode'
bind_re = r'public override void BindPorts'
replaced_create = re.sub(create_re, 'protected override void CreateNode', code)
replaced_bind = re.sub(bind_re, 'protected override void BindPorts', replaced_create)
return replaced_bind
def refactor_2(code: str) -> str:
'''
Changes the access modifiers of GetNodeData and SetNodeData to protected internal
'''
create_re = r'public override JObject GetNodeData'
bind_re = r'public override void SetNodeData'
replaced_create = re.sub(create_re, 'protected internal override JObject GetNodeData', code)
replaced_bind = re.sub(bind_re, 'protected internal override void SetNodeData', replaced_create)
return replaced_bind
def refactor_file(refactor_func: Callable[[str], str], file_path: str) -> None:
with open(file_path, 'r') as f:
code = f.read()
refactored_code = refactor_func(code)
with open(file_path, 'w') as f:
f.write(refactored_code)
def refactor_folder_recursive(refactor_func: Callable[[str], str], folder_path: str) -> None:
for file_name in os.listdir(folder_path):
file_path = os.path.join(folder_path, file_name)
if os.path.isdir(file_path):
refactor_folder_recursive(refactor_func, file_path)
elif file_name.endswith(".cs"):
refactor_file(refactor_func, file_path)
refactor_versions = {
0: refactor_0,
1: refactor_1,
2: refactor_2,
}
def main() -> None:
version = sys.argv[1]
refactor_function = refactor_versions[int(version)]
path = sys.argv[2]
if len(sys.argv) > 3:
mode = sys.argv[3]
else:
mode = '--file'
if mode == '--file':
refactor_file(refactor_function, path)
elif mode == '--folder':
refactor_folder_recursive(refactor_function, path)
else:
print(f'Invalid mode "{mode}"')
if __name__ == '__main__':
main()