"""Wrap the body of the if (app.active_track or app.active_tickets) and app.node_editor_ctx: block in render_task_dag_panel with a try/except. To do this without manually reindenting ~195 lines, we use a Python script that finds the if line, dedents the body, inserts try/except, and re-indents. """ import re from pathlib import Path GUI_2 = Path(r"C:\projects\manual_slop_tier2\src\gui_2.py") text = GUI_2.read_text(encoding="utf-8") lines = text.splitlines(keepends=True) # Find the function start fn_start = None for i, line in enumerate(lines): if "def render_task_dag_panel" in line: fn_start = i break if fn_start is None: raise RuntimeError("render_task_dag_panel not found") # Find the if (app.active_track or app.active_tickets) and app.node_editor_ctx: line if_start = None for i in range(fn_start, len(lines)): line = lines[i] stripped = line.lstrip() if stripped.startswith("if (app.active_track or app.active_tickets)"): if_start = i break if if_start is None: raise RuntimeError("if not found") # Find the end of the if block (matching 0-indent next line, or next def) if_end = None for i in range(if_start + 1, len(lines)): line = lines[i] if line.strip() == "" or line.startswith(" ") or line.startswith("\t"): continue # Found a non-indented line that's not blank if_end = i break if if_end is None: if_end = len(lines) # Find body bounds (indented lines) body_start = if_start + 1 while body_start < if_end and lines[body_start].strip() == "": body_start += 1 # Find actual body end (last indented line before 0-indent) body_end = body_end = if_end for i in range(if_end - 1, if_start, -1): if lines[i].strip() != "": body_end = i + 1 break # Get the indentation of the if line if_indent = len(lines[if_start]) - len(lines[if_start].lstrip()) # Body indent should be if_indent + 2 (next level) body_indent = if_indent + 2 # Extract body lines (preserve content) body_lines = lines[body_start:body_end] # Detent: strip body_indent spaces from each line detented_body = [] for ln in body_lines: if ln.strip() == "": detented_body.append("\n") elif ln.startswith(" " * body_indent): detented_body.append(" " + ln[body_indent:]) else: detented_body.append(ln) # Re-indent: add body_indent spaces to each line reindented_body = [] for ln in detented_body: if ln.strip() == "": reindented_body.append("\n") else: reindented_body.append(" " * (body_indent - 1) + ln) # Build the new content new_lines = [] new_lines.append(lines[if_start]) # the if line new_lines.append(" " * (if_indent + 1) + "try:\n") for ln in reindented_body: new_lines.append(ln) new_lines.append(" " * (if_indent + 1) + "except Exception as dag_err:\n") new_lines.append(" " * (if_indent + 2) + "# imgui-node-editor can raise ImGui assertions (e.g. Missing PopID) that\n") new_lines.append(" " * (if_indent + 2) + "# would otherwise kill the ImGui main loop and break every subsequent\n") new_lines.append(" " * (if_indent + 2) + "# test in the same xdist worker that shares the live_gui subprocess.\n") new_lines.append(" " * (if_indent + 2) + "if not hasattr(app, '_last_request_errors'): app._last_request_errors = []\n") new_lines.append(" " * (if_indent + 2) + "app._last_request_errors.append(('render_task_dag_panel.dag_error', dag_err))\n") # Lines after the if body (0-indent) for i in range(body_end, if_end): new_lines.append(lines[i]) # Replace new_text = "".join(lines[:if_start]) + "".join(new_lines) + "".join(lines[if_end:]) # Write back GUI_2.write_text(new_text, encoding="utf-8") print(f"OK: modified if block at line {if_start+1}, body lines {body_end - body_start}")