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()
|
||||
@@ -0,0 +1,48 @@
|
||||
from __future__ import annotations
|
||||
import ast
|
||||
import sys
|
||||
import re
|
||||
|
||||
def gut_file(file_path: str) -> None:
|
||||
"""Removes delegation wrappers and fixes all calls in the file."""
|
||||
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: return
|
||||
|
||||
# 1. Identify all _render_xxx methods that have module-level counterparts
|
||||
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_names = {m.name for m in render_methods}
|
||||
|
||||
# Process methods in reverse to keep line numbers valid
|
||||
render_methods.sort(key=lambda x: x.lineno, reverse=True)
|
||||
|
||||
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
|
||||
del lines[s_idx:e_idx]
|
||||
|
||||
# 2. Fix all calls in the remaining lines
|
||||
for i in range(len(lines)):
|
||||
for m_name in render_names:
|
||||
t_func = m_name.lstrip("_")
|
||||
# Replace self._render_xxx( with render_xxx(self,
|
||||
lines[i] = lines[i].replace(f"self.{m_name}(", f"{t_func}(self, ")
|
||||
lines[i] = lines[i].replace(f"app.{m_name}(", f"{t_func}(app, ")
|
||||
# Clean up (self, ) or (app, )
|
||||
lines[i] = lines[i].replace("(self, )", "(self)")
|
||||
lines[i] = lines[i].replace("(app, )", "(app)")
|
||||
|
||||
final_content = "\n".join(lines) + "\n"
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
f.write(final_content)
|
||||
print(f"Successfully GUTTED {file_path}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
gut_file(sys.argv[1])
|
||||
@@ -1,193 +1,92 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
AST-based transformer to extract App._render_xxx methods into module-level functions.
|
||||
|
||||
Transformation:
|
||||
class App:
|
||||
def _render_xxx(self, ...) -> ReturnType:
|
||||
self.foo = bar
|
||||
self.baz.qux()
|
||||
|
||||
Becomes:
|
||||
|
||||
def render_xxx(app: App, ...) -> ReturnType:
|
||||
app.foo = bar
|
||||
app.baz.qux()
|
||||
|
||||
class App:
|
||||
def _render_xxx(self, ...) -> ReturnType:
|
||||
render_xxx(self, ...)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
import ast
|
||||
import sys
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
import os
|
||||
import re
|
||||
|
||||
def transform_file(file_path: str) -> None:
|
||||
"""
|
||||
Refactors App._render_xxx methods to module-level functions for hot-reloading.
|
||||
Now correctly renames 'self' to 'app' inside function bodies.
|
||||
"""
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
tree = ast.parse(content)
|
||||
|
||||
class RenderMethodTransformer(ast.NodeTransformer):
|
||||
def __init__(self, class_name: str = "App", method_prefix: str = "_render"):
|
||||
self.class_name = class_name
|
||||
self.method_prefix = method_prefix
|
||||
self.transformed_methods: list[str] = []
|
||||
self.in_target_class = False
|
||||
self.current_method: Optional[str] = None
|
||||
|
||||
def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef:
|
||||
if node.name == self.class_name:
|
||||
self.in_target_class = True
|
||||
self.current_method = None
|
||||
node = self.generic_visit(node)
|
||||
self.in_target_class = False
|
||||
return node
|
||||
return node
|
||||
|
||||
def visit_FunctionDef(self, node: ast.FunctionDef) -> ast.FunctionDef:
|
||||
if not self.in_target_class:
|
||||
class VariableRenamer(ast.NodeTransformer):
|
||||
def visit_Name(self, node: ast.Name) -> ast.Name:
|
||||
if node.id == "self":
|
||||
node.id = "app"
|
||||
return node
|
||||
|
||||
if not node.name.startswith(self.method_prefix):
|
||||
class RenderTransformer(ast.NodeTransformer):
|
||||
def __init__(self):
|
||||
self.new_functions = []
|
||||
def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef:
|
||||
if node.name != "App": return node
|
||||
new_body = []
|
||||
for item in node.body:
|
||||
if isinstance(item, ast.FunctionDef) and item.name.startswith("_render_") and item.name != "_render_window_if_open":
|
||||
func_name = item.name.lstrip("_")
|
||||
# Transform body: rename 'self' to 'app'
|
||||
renamer = VariableRenamer()
|
||||
new_body_nodes = [renamer.visit(b) for b in item.body]
|
||||
|
||||
new_func = ast.FunctionDef(
|
||||
name=func_name,
|
||||
args=item.args,
|
||||
body=new_body_nodes,
|
||||
decorator_list=item.decorator_list,
|
||||
returns=item.returns
|
||||
)
|
||||
if new_func.args.args and new_func.args.args[0].arg == "self":
|
||||
new_func.args.args[0].arg = "app"
|
||||
self.new_functions.append(new_func)
|
||||
|
||||
# Create delegation wrapper in class
|
||||
wrapper_body = [
|
||||
ast.Expr(
|
||||
value=ast.Call(
|
||||
func=ast.Name(id=func_name, ctx=ast.Load()),
|
||||
args=[ast.Name(id="self", ctx=ast.Load())] + [ast.Name(id=a.arg, ctx=ast.Load()) for a in item.args.args[1:]],
|
||||
keywords=[ast.keyword(arg=kw.arg, value=ast.Name(id=kw.arg, ctx=ast.Load())) for kw in item.args.kwonlyargs]
|
||||
)
|
||||
)
|
||||
]
|
||||
item.body = wrapper_body
|
||||
new_body.append(item)
|
||||
else:
|
||||
new_body.append(item)
|
||||
node.body = new_body
|
||||
return node
|
||||
|
||||
if node.name == "__init__" or node.name.startswith("__"):
|
||||
return node
|
||||
|
||||
self.current_method = node.name
|
||||
new_name = node.name[1:] # Remove underscore prefix: _render_xxx -> render_xxx
|
||||
|
||||
self.transformed_methods.append(node.name)
|
||||
|
||||
new_func = ast.FunctionDef(
|
||||
name=new_name,
|
||||
args=ast.arguments(
|
||||
posonlyargs=[],
|
||||
args=[ast.arg(arg="app", annotation=ast.Name(id=self.class_name, ctx=ast.Load()))] + node.args.args,
|
||||
kwonlyargs=node.args.kwonlyargs,
|
||||
kw_defaults=node.args.kw_defaults,
|
||||
defaults=node.args.defaults,
|
||||
),
|
||||
body=[self._transform_body(body_item) for body_item in node.body],
|
||||
decorator_list=node.decorator_list,
|
||||
returns=node.returns,
|
||||
type_comment=node.type_comment,
|
||||
)
|
||||
|
||||
delegation_method = ast.FunctionDef(
|
||||
name=node.name,
|
||||
args=node.args,
|
||||
body=[ast.Expr(value=ast.Call(
|
||||
func=ast.Name(id=new_name, ctx=ast.Load()),
|
||||
args=[ast.Name(id="self", ctx=ast.Load())] + [self._arg_to_expr(arg) for arg in node.args.args],
|
||||
keywords=[ast.keyword(arg=arg.arg, value=ast.Name(id=arg.arg, ctx=ast.Load())) for arg in node.args.kwonlyargs]
|
||||
))],
|
||||
decorator_list=node.decorator_list,
|
||||
returns=node.returns,
|
||||
type_comment=node.type_comment,
|
||||
)
|
||||
|
||||
return ast.Module(body=[new_func, delegation_method], type_ignores=[])
|
||||
|
||||
def _arg_to_expr(self, arg: ast.arg) -> ast.expr:
|
||||
return ast.Name(id=arg.arg, ctx=ast.Load())
|
||||
|
||||
def _transform_body(self, node: ast.AST) -> ast.AST:
|
||||
return ast.NodeTransformer.generic_visit(self, node)
|
||||
|
||||
def visit_Name(self, node: ast.Name) -> ast.Name:
|
||||
if isinstance(node.ctx, ast.Load) and node.id == "self":
|
||||
return ast.Name(id="app", ctx=node.ctx)
|
||||
return node
|
||||
|
||||
def visit_Attribute(self, node: ast.Attribute) -> ast.Attribute:
|
||||
if isinstance(node.value, ast.Name) and node.value.id == "self":
|
||||
new_value = ast.Name(id="app", ctx=node.value.ctx)
|
||||
return ast.Attribute(value=new_value, attr=node.attr, ctx=node.ctx)
|
||||
return self.generic_visit(node)
|
||||
|
||||
|
||||
class AppRenderExtractor(ast.NodeVisitor):
|
||||
def __init__(self, class_name: str = "App", method_prefix: str = "_render"):
|
||||
self.class_name = class_name
|
||||
self.method_prefix = method_prefix
|
||||
self.in_target_class = False
|
||||
self.methods: dict[str, dict] = {}
|
||||
|
||||
def visit_ClassDef(self, node: ast.ClassDef) -> None:
|
||||
if node.name == self.class_name:
|
||||
self.in_target_class = True
|
||||
self.generic_visit(node)
|
||||
self.in_target_class = False
|
||||
|
||||
def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
|
||||
if not self.in_target_class:
|
||||
return
|
||||
|
||||
if not node.name.startswith(self.method_prefix):
|
||||
return
|
||||
|
||||
if node.name == "__init__" or node.name.startswith("__"):
|
||||
return
|
||||
|
||||
args = [arg.arg for arg in node.args.args]
|
||||
self.methods[node.name] = {
|
||||
"line": node.lineno,
|
||||
"end_line": node.end_lineno,
|
||||
"args": args,
|
||||
"has_return": any(isinstance(n, ast.Return) for n in ast.walk(node)),
|
||||
}
|
||||
|
||||
|
||||
def extract_render_methods(source_path: str, class_name: str = "App", method_prefix: str = "_render") -> dict:
|
||||
with open(source_path, "r", encoding="utf-8") as f:
|
||||
source = f.read()
|
||||
|
||||
tree = ast.parse(source)
|
||||
extractor = AppRenderExtractor(class_name, method_prefix)
|
||||
extractor.visit(tree)
|
||||
|
||||
return extractor.methods
|
||||
|
||||
|
||||
def transform_file(source_path: str, output_path: Optional[str] = None, class_name: str = "App", method_prefix: str = "_render") -> list[str]:
|
||||
with open(source_path, "r", encoding="utf-8") as f:
|
||||
source = f.read()
|
||||
|
||||
tree = ast.parse(source)
|
||||
|
||||
transformer = RenderMethodTransformer(class_name, method_prefix)
|
||||
transformer = RenderTransformer()
|
||||
new_tree = transformer.visit(tree)
|
||||
for func in transformer.new_functions:
|
||||
new_tree.body.append(func)
|
||||
ast.fix_missing_locations(new_tree)
|
||||
|
||||
result = ast.unparse(new_tree)
|
||||
|
||||
if output_path:
|
||||
with open(output_path, "w", encoding="utf-8", newline="") as f:
|
||||
f.write(result)
|
||||
if hasattr(ast, "unparse"):
|
||||
raw_output = ast.unparse(new_tree)
|
||||
lines = raw_output.splitlines()
|
||||
new_lines = []
|
||||
for line in lines:
|
||||
match = re.match(r"^( +)(.*)", line)
|
||||
if match:
|
||||
spaces, rest = match.groups()
|
||||
new_indent = " " * (len(spaces) // 4)
|
||||
new_lines.append(new_indent + rest)
|
||||
else:
|
||||
new_lines.append(line)
|
||||
final_content = "\n".join(new_lines)
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
f.write(final_content)
|
||||
print(f"Successfully transformed {file_path} with 1-space indentation and 'self'->'app' migration.")
|
||||
else:
|
||||
print(result)
|
||||
|
||||
return transformer.transformed_methods
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Transform App._render_xxx methods to module-level functions")
|
||||
parser.add_argument("source", help="Source Python file to transform")
|
||||
parser.add_argument("-o", "--output", help="Output file (default: stdout)")
|
||||
parser.add_argument("-c", "--cls", default="App", help="Class name to transform (default: App)")
|
||||
parser.add_argument("-p", "--prefix", default="_render", help="Method prefix to extract (default: _render)")
|
||||
parser.add_argument("--extract-only", action="store_true", help="Only extract method info, don't transform")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.extract_only:
|
||||
methods = extract_render_methods(args.source, args.cls, args.prefix)
|
||||
print(f"Found {len(methods)} methods:")
|
||||
for name, info in sorted(methods.items(), key=lambda x: x[1]["line"]):
|
||||
print(f" {name}: lines {info['line']}-{info['end_line']}, args={info['args']}")
|
||||
else:
|
||||
transformed = transform_file(args.source, args.output, args.cls, args.prefix)
|
||||
print(f"Transformed {len(transformed)} methods: {transformed}")
|
||||
|
||||
print("ast.unparse not available.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python scripts/transform_render_methods.py <file_path>")
|
||||
else:
|
||||
transform_file(sys.argv[1])
|
||||
|
||||
@@ -4,18 +4,20 @@ import sys
|
||||
import re
|
||||
|
||||
def transform_file(file_path: str) -> None:
|
||||
"""Refactors App._render_xxx methods to module-level functions."""
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
lines = f.read().splitlines()
|
||||
tree = ast.parse("\n".join(lines))
|
||||
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: 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_names = {m.name for m in render_methods}
|
||||
render_methods.sort(key=lambda x: x.lineno, reverse=True)
|
||||
|
||||
extracted = []
|
||||
for m in render_methods:
|
||||
s_idx = m.lineno - 1
|
||||
@@ -23,41 +25,41 @@ def transform_file(file_path: str) -> None:
|
||||
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)
|
||||
|
||||
# 1. Out-dent
|
||||
processed = [l[1:] if l.startswith(" ") else l for l in m_lines]
|
||||
def_line_idx = next((i for i, l in enumerate(processed) if l.strip().startswith("def ")), -1)
|
||||
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
|
||||
|
||||
# 2. Redirect internal calls in this function's body
|
||||
for i in range(def_line_idx + 1, len(processed)):
|
||||
for m_name in render_names:
|
||||
t_func = m_name.lstrip("_")
|
||||
# Replace self._render_xxx( with render_xxx(app,
|
||||
# then fix up (app, ) if it was empty.
|
||||
processed[i] = processed[i].replace(f"self.{m_name}(", f"{t_func}(app, ")
|
||||
processed[i] = processed[i].replace(f"(app, )", "(app)")
|
||||
processed[i] = processed[i].replace("(app, )", "(app)")
|
||||
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
|
||||
# REMOVE from class
|
||||
del lines[s_idx:e_idx]
|
||||
|
||||
# 3. Redirect calls in remaining class methods
|
||||
for i in range(len(lines)):
|
||||
for m_name in render_names:
|
||||
t_func = m_name.lstrip("_")
|
||||
lines[i] = lines[i].replace(f"self.{m_name}(", f"{t_func}(self, ")
|
||||
lines[i] = lines[i].replace("(self, )", "(self)")
|
||||
|
||||
extracted.reverse()
|
||||
final = "\n".join(lines) + "\n\n" + "\n\n".join(extracted) + "\n"
|
||||
final_content = "\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"Transformed {file_path}")
|
||||
f.write(final_content)
|
||||
print(f"Successfully GUTTED {file_path}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) > 1: transform_file(sys.argv[1])
|
||||
transform_file(sys.argv[1])
|
||||
|
||||
Reference in New Issue
Block a user