feat(hot-reload): Complete deep OOP gutting of gui_2.py and perfect 1-space refactor
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import ast
|
||||
import sys
|
||||
import re
|
||||
|
||||
def transform():
|
||||
file_path = "src/gui_2.py"
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
lines = f.read().splitlines()
|
||||
content = "\n".join(lines)
|
||||
tree = ast.parse(content)
|
||||
app_class = next((n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == "App"), None)
|
||||
if not app_class:
|
||||
print("App class not found")
|
||||
return
|
||||
render_methods = [
|
||||
m for m in app_class.body
|
||||
if isinstance(m, ast.FunctionDef) and m.name.startswith("_render_") and m.name != "_render_window_if_open"
|
||||
]
|
||||
render_methods.sort(key=lambda x: x.lineno, reverse=True)
|
||||
extracted = []
|
||||
for m in render_methods:
|
||||
s_idx = m.lineno - 1
|
||||
e_idx = m.end_lineno
|
||||
if m.decorator_list: s_idx = m.decorator_list[0].lineno - 1
|
||||
m_lines = lines[s_idx:e_idx]
|
||||
func_name = m.name.lstrip("_")
|
||||
processed = []
|
||||
for l in m_lines:
|
||||
processed.append(l[1:] if l.startswith(" ") else l)
|
||||
def_line_idx = -1
|
||||
for i, l in enumerate(processed):
|
||||
if l.strip().startswith("def "):
|
||||
def_line_idx = i
|
||||
break
|
||||
if def_line_idx != -1:
|
||||
l = processed[def_line_idx]
|
||||
l = l.replace(f"def {m.name}(", f"def {func_name}(", 1)
|
||||
l = re.sub(r"\bself\b", "app: App", l, count=1)
|
||||
processed[def_line_idx] = l
|
||||
for i in range(def_line_idx + 1, len(processed)):
|
||||
processed[i] = re.sub(r"(?<![a-zA-Z0-9_])self\.", "app.", processed[i])
|
||||
processed[i] = re.sub(r"(?<![a-zA-Z0-9_])self(?![a-zA-Z0-9_:])", "app", processed[i])
|
||||
extracted.append("\n".join(processed))
|
||||
arg_names = [a.arg for a in m.args.args if a.arg != "self"]
|
||||
kw_names = [a.arg for a in m.args.kwonlyargs]
|
||||
call_args = ["self"] + arg_names + [f"{k}={k}" for k in kw_names]
|
||||
orig_def_line = m_lines[0]
|
||||
if m.decorator_list:
|
||||
for l in m_lines:
|
||||
if l.strip().startswith("def "):
|
||||
orig_def_line = l
|
||||
break
|
||||
wrapper = [orig_def_line, f" {func_name}({', '.join(call_args)})"]
|
||||
lines[s_idx:e_idx] = wrapper
|
||||
extracted.reverse()
|
||||
final = "\n".join(lines) + "\n\n" + "\n\n".join(extracted) + "\n"
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
f.write(final)
|
||||
print(f"Successfully transformed {file_path}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
transform()
|
||||
Reference in New Issue
Block a user