Files
manual_slop/scripts/fix_recursion_final.py
T
2026-05-09 12:43:49 -04:00

63 lines
2.2 KiB
Python

from pathlib import Path
import re
# Fix AppController
file_ac = Path('src/app_controller.py')
code_ac = file_ac.read_text(encoding='utf-8')
# Robust non-recursive __getattr__
new_getattr_ac = """ def __getattr__(self, name: str) -> Any:
if name == '_app':
raise AttributeError(name)
# Check if _app exists in self without triggering __getattr__
try:
app = object.__getattribute__(self, '_app')
except AttributeError:
raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
if app is not None and name in app.__dict__:
return getattr(app, name)
raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
"""
# Replace all instances of __getattr__ in AppController (in case of duplication)
code_ac = re.sub(r'def __getattr__\(self, name: str\) -> Any:.*?raise AttributeError\(.*?\)', new_getattr_ac, code_ac, flags=re.DOTALL)
file_ac.write_text(code_ac, encoding='utf-8')
# Fix App
file_gui = Path('src/gui_2.py')
code_gui = file_gui.read_text(encoding='utf-8')
new_getattr_gui = """ def __getattr__(self, name: str) -> Any:
if name == 'controller':
raise AttributeError(name)
try:
ctrl = object.__getattribute__(self, 'controller')
except AttributeError:
raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
if ctrl is not None and name in ctrl.__dict__:
return getattr(ctrl, name)
raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
"""
code_gui = re.sub(r'def __getattr__\(self, name: str\) -> Any:.*?raise AttributeError\(.*?\)', new_getattr_gui, code_gui, flags=re.DOTALL)
new_setattr_gui = """ def __setattr__(self, name: str, value: Any) -> None:
if name == 'controller':
object.__setattr__(self, name, value)
return
try:
ctrl = object.__getattribute__(self, 'controller')
except AttributeError:
ctrl = None
if ctrl is not None and name in ctrl.__dict__:
setattr(ctrl, name, value)
else:
object.__setattr__(self, name, value)
"""
code_gui = re.sub(r'def __setattr__\(self, name: str, value: Any\) -> None:.*?object\.__setattr__\(self, name, value\)', new_setattr_gui, code_gui, flags=re.DOTALL)
file_gui.write_text(code_gui, encoding='utf-8')
print('Delegation fix complete.')