-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconverter.py
440 lines (377 loc) · 15 KB
/
converter.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import re
import ast
from copy import deepcopy
tkinter_widgets = [ "Button", "Canvas", "Checkbutton",
"Entry", "Label", "Menubutton",
"Message", "Radiobutton", "Scale",
"Scrollbar", "Text", "Toplevel",
"Treeview", "Frame", "Progressbar",
"Separator"]
ctk_widgets = ["CTk" + x for x in tkinter_widgets]
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
def remove_resolution_from_ctkslider(content: str) -> str:
"""
Remove the 'resolution' parameter from calls to ctk.CTkSlider or CTkSlider
in the provided Python code.
Args:
content (str): The Python code content.
Returns:
str: The modified Python code without the 'resolution' parameter.
"""
tree = parsetree(content)
slidernodes = []
for node in ast.walk(tree):
if isinstance(node, ast.Call):
string_name = ast.unparse(node.func)
if string_name == "ctk.CTkSlider" or string_name == "CTkSlider":
n = deepcopy(node)
slidernodes.append(node)
for node in slidernodes:
remove_parameter_from_call(node, "resolution")
return ast.unparse(tree)
def remove_parameter_from_call(node: ast.Call, parameter: str) -> None:
"""
Remove the specified parameter from the given AST call node.
Args:
node (ast.Call): The AST call node.
parameter (str): The name of the parameter to remove.
Returns:
None
"""
node.keywords = [kw for kw in node.keywords if kw.arg != parameter]
def parsetree(source: str) -> ast.AST:
try:
tree = ast.parse(source, type_comments=True)
except:
tree = ast.parse(source)
finally:
return tree
class WidgetReplacer:
"""
A class to replace widgets in a script and add custom constants.
Attributes:
source (str): The path to the source script file.
output (str): The path to the output script file.
findables (dict): A dictionary containing findable patterns and their replacements.
constants (list): A list of existing tkinter constants found in the script.
used_constants (list): A list of constants used in the script.
"""
tkinter_constants = [
"ACTIVE",
"ALL",
"ANCHOR",
"ARC",
"BASELINE",
"BEVEL",
"BOTH",
"BOTTOM",
"BROWSE",
"BUTT",
"CASCADE",
"CENTER",
"CHAR",
"CHECKBUTTON",
"CHORD",
"COMMAND",
"DISABLED",
"E",
"END",
"EW",
"EXCEPTION",
"EXTENDED",
"FALSE",
"FIRST",
"FLAT",
"GROOVE",
"HIDDEN",
"HORIZONTAL",
"INSERT",
"INSIDE",
"LAST",
"LEFT",
"MITER",
"MULTIPLE",
"N",
"NE",
"NO",
"NONE",
"NORMAL",
"NS",
"NSEW",
"NW",
"OFF",
"ON",
"OUTSIDE",
"PAGES",
"PIESLICE",
"PROJECTING",
"RADIOBUTTON",
"RAISED",
"READABLE",
"RIDGE",
"RIGHT",
"ROUND",
"S",
"SCROLL",
"SE",
"SEL",
"SEL_FIRST",
"SEL_LAST",
"SEPARATOR",
"SINGLE",
"SOLID",
"SUNKEN",
"SW",
"Synchronous",
"SystemButton",
"TOP",
"TRUE",
"UNITS",
"VERTICAL",
"W",
"WORD",
"WRITABLE",
"X",
"Y",
]
def __init__(self, source: str, output: str) -> None:
"""
Initialize the WidgetReplacer instance.
Args:
source (str): The path to the source script file.
output (str): The path to the output script file.
"""
self.source: str = source
self.output: str = output
self.findables: dict[str, str] = {}
self.constants: list[str] = []
self.used_constants: list[str] = []
def set_source(self, source: str):
self.source = source
def set_output(self, output: str):
self.output = output
def add_findable(self, original: str, replacement: str) -> None:
"""
Add a findable pattern and its replacement.
Args:
original (str): The original pattern to find.
replacement (str): The replacement pattern.
"""
self.findables[re.escape(original)] = replacement
def replace_widgets(self):
"""
Replace widgets in the script using findable patterns and replacements.
"""
with open(self.source, "r", encoding="utf-8", errors="ignore") as f:
script_content = f.read()
out = ""
for onst in self.tkinter_constants:
if re.search(onst, script_content):
self.constants.append(onst)
for original, replacement in self.findables.items():
pattern = r"\b{}\b".format(original)
script_content = re.sub(pattern, replacement, script_content)
with open(self.output, "w", errors="ignore") as f:
f.write(script_content)
def double_check(self):
"""
Double-check and add custom tkinter constants to the script.
"""
with open(self.output, "r") as f:
script_content = f.readlines()
out = "import customtkinter as ctk\nfrom customtkinter import "
m = len(self.constants) - 1
for index, constant in enumerate(self.constants):
if index == m:
out += f"{constant}"
else:
out += f"{constant}, "
out += "\n"
with open(self.output, "w") as f:
f.write(out)
for line in script_content:
f.write(line)
def add_constant(self, constant):
"""
Add a custom constant to the list of constants.
Args:
constant (str): The constant to add.
"""
self.constants.append(constant)
class SourceConverter:
def __init__(self) -> None:
self.replacer = WidgetReplacer("None", "None")
for widget in tkinter_widgets:
ctk_widget = f"ctk.CTk{widget}"
self.replacer.add_findable(f" {widget}(", f" {ctk_widget}(")
self.replacer.add_findable(f" {widget}(", f" {ctk_widget}(")
self.replacer.add_findable(f"{widget}(", f"{ctk_widget}(")
self.replacer.add_findable(f"{widget}, ", "")
self.replacer.add_findable(f"{widget},", f"{ctk_widget},")
self.replacer.add_findable(f" = {widget}(", f" = {ctk_widget}(")
self.replacer.add_findable(f"={widget}(", f"={ctk_widget}(")
self.replacer.add_findable(f": {widget} ", f": {ctk_widget} ")
self.replacer.add_findable(f":{widget},", f":{ctk_widget},")
self.replacer.add_findable(f":{widget}", f":{ctk_widget}")
for widg in tkinter_widgets:
widget = "tk." + widg
ctk_widget = f"ctk.CTk{widg}"
self.replacer.add_findable(f"{widget}(", f"{ctk_widget}(")
self.replacer.add_findable(f" {widget}(", f" {ctk_widget}(")
self.replacer.add_findable(f" {widget}(", f" {ctk_widget}(")
self.replacer.add_findable(f"{widget}, ", f"")
self.replacer.add_findable(f"{widget},", f"{ctk_widget},")
self.replacer.add_findable(f" = {widget}(", f" = {ctk_widget}(")
self.replacer.add_findable(f"={widget}(", f"={ctk_widget}(")
self.replacer.add_findable(f": {widget} ", f": {ctk_widget} ")
self.replacer.add_findable(f":{widget},", f":{ctk_widget},")
self.replacer.add_findable(f":{widget}", f":{ctk_widget}")
for widg in tkinter_widgets:
widget: str = "ttk." + widg
ctk_widget: str = f"ctk.CTk{widg}"
self.replacer.add_findable("{0}(".format(widget), "{0}(".format(ctk_widget))
self.replacer.add_findable("{0}, ".format(widget), "")
self.replacer.add_findable(widget + ",", "{0},".format(ctk_widget))
self.replacer.add_findable(" = {0}(".format(widget), " = {0}(".format(ctk_widget))
self.replacer.add_findable("={0}(".format(widget), "={0}(".format(ctk_widget))
self.replacer.add_findable(": {0} ".format(widget), ": {0} ".format(ctk_widget))
self.replacer.add_findable(":{0},".format(widget), ":{0},".format(ctk_widget))
self.replacer.add_findable(":{0}".format(widget), ":{0}".format(ctk_widget))
def from_string(self, string: str) -> str:
script_content = string
self.replacer.constants = []
for onst in self.replacer.tkinter_constants:
if re.search(onst, script_content):
self.replacer.constants.append(onst)
for original, replacement in self.replacer.findables.items():
pattern = r"\b{}\b".format(original)
script_content = re.sub(pattern, replacement, script_content)
out = "import customtkinter as ctk\nfrom customtkinter import "
m = len(self.replacer.constants) - 1
for index, constant in enumerate(self.replacer.constants):
if index == m:
out += f"{constant}"
else:
out += f"{constant}, "
out += "\n\n"
script_content = out + script_content
cont1: str = re.sub(r"\.config\(", ".configure(", script_content)
cont2 = cont1.replace(", bg=", ", bg_color=").replace(", bg =", ", bg_color =")
cont3 = cont2.replace(", fg=", ", fg_color=").replace(", fg =", ", fg_color =")
cont4 = cont3.replace(r"(tk.Tk):", r"(ctk.CTk):")
for index, widget in enumerate(tkinter_widgets):
cont4 = cont4.replace(f"(tk.{widget}):", f"(ctk.{ctk_widgets[index]}):")
cont4 = cont4.replace("(Tk):", "(ctk.CTk):")
cont5 = cont4.replace("Tk()", "ctk.CTk()")
lines = cont5.splitlines()
new_lines = []
for line in lines:
l = re.sub(r"ttk.ctk.", r"ctk.", line)
l2 = re.sub(r"tk.ctk.", r"ctk.", l)
l3 = re.sub(r".CTkText", r".CTkTextbox", l2)
l4 = re.sub(r".CTkRadiobutton", r".CTkRadioButton", l3)
# CTkCheckButton
l5 = re.sub(r".CTkCheckbutton", r".CTkCheckBox", l4)
l6 = re.sub(r".CTkScale", r".CTkSlider", l5)
l7 = l6
new_lines.append(l7)
source = "\n".join(new_lines)
try:
source = change_textvariable_to_variable(source)
except:
source = source
try:
source = change_orient_to_orientation(source)
except:
source = source
try:
source = remove_resolution_from_ctkslider(source)
except:
source = source
return source
def from_file(self, filepath: str) -> str:
with open(filepath, 'r') as f:
content = f.read()
return self.from_string(content)