import ast import sys from pathlib import Path ROOT_DIR = Path(__file__).parent.parent class IndentationCorrector(ast.NodeVisitor): def __init__(self, source_lines: list[str]): self.source_lines = source_lines self.new_lines: list[str] = [] self._indentation_stack: list[int] = [0] def visit_Module(self, node: ast.Module): self._process_lines(0, 0) for child in node.body: self._walk_node(child, 0) self.generic_visit(node) def _walk_node(self, node: ast.AST, depth: int): if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)): line_no = node.lineno - 1 actual_indent = self._get_indent(line_no) expected_indent = depth self._process_lines(line_no, expected_indent) self._indentation_stack.append(depth + 1) for child in node.body: self._walk_node(child, depth + 1) self._indentation_stack.pop() elif isinstance(node, ast.If): line_no = node.lineno - 1 self._process_lines(line_no, depth) for child in node.body: self._walk_node(child, depth + 1) if node.orelse: self._walk_node(node.orelse, depth + 1) elif isinstance(node, (ast.For, ast.While, ast.With)): line_no = node.lineno - 1 self._process_lines(line_no, depth) for child in node.body: self._walk_node(child, depth + 1) if isinstance(node, ast.For) and node.orelse: self._walk_node(node.orelse, depth + 1) elif isinstance(node, ast.Try): for child in node.body: self._walk_node(child, depth + 1) for handler in node.handlers: self._walk_node(handler, depth + 1) if node.orelse: self._walk_node(node.orelse, depth + 1) if node.finalbody: self._walk_node(node.finalbody, depth + 1) else: self.generic_visit(node) def _get_indent(self, lineno: int) -> int: if lineno < 0 or lineno >= len(self.source_lines): return 0 line = self.source_lines[lineno] stripped = line.lstrip() return len(line) - len(stripped) def _process_lines(self, end_line: int, expected_indent: int): pass def correct_file(filepath: Path) -> tuple[bool, str]: try: with open(filepath, "r", encoding="utf-8", newline="") as f: source = f.read() source_lines = source.splitlines() tree = ast.parse(source, filename=str(filepath)) corrector = IndentationCorrector(source_lines) corrector.visit(tree) return False, "Not implemented yet" except Exception as e: return False, str(e) def fix_indentation_simple(filepath: Path, base_indent: int = 4, target_indent: int = 1) -> tuple[bool, str]: try: with open(filepath, "r", encoding="utf-8", newline="") as f: lines = f.readlines() new_lines = [] changed = False for line in lines: stripped = line.lstrip() if not stripped: new_lines.append(line) continue leading = len(line) - len(stripped) if leading > 0: old_level = leading // base_indent new_leading = old_level * target_indent if leading != new_leading: new_lines.append(" " * new_leading + stripped + "\n" if not line.endswith("\n") else " " * new_leading + stripped) changed = True else: new_lines.append(line) else: new_lines.append(line) if changed: with open(filepath, "w", encoding="utf-8", newline="") as f: f.writelines(new_lines) return True, "Fixed" return False, "No changes needed" except Exception as e: return False, str(e) def main(): if len(sys.argv) > 2: filepath = Path(sys.argv[2]) base_indent = int(sys.argv[3]) if len(sys.argv) > 3 else 4 target_indent = int(sys.argv[4]) if len(sys.argv) > 4 else 1 changed, msg = fix_indentation_simple(filepath, base_indent, target_indent) print(f"{filepath}: {msg}") return files_to_fix = [ ("src/fuzzy_anchor.py", 4, 1), ("src/patch_modal.py", 2, 1), ("scripts/extract_symbols.py", 4, 1), ("scripts/tasks/download_fonts.py", 4, 1), ] test_files = [ ("tests/test_arch_boundary_phase1.py", 2, 1), ("tests/test_arch_boundary_phase2.py", 2, 1), ("tests/test_arch_boundary_phase3.py", 2, 1), ("tests/test_external_editor.py", 4, 1), ("tests/test_headless_service.py", 4, 1), ("tests/test_history_manager.py", 4, 1), ("tests/test_fuzzy_anchor.py", 4, 1), ("tests/test_gemini_cli_adapter.py", 4, 1), ("tests/test_ai_client_cli.py", 4, 1), ("tests/test_api_events.py", 4, 1), ("tests/test_context_composition_decoupled.py", 4, 1), ("tests/test_context_composition_phase3.py", 4, 1), ("tests/test_context_composition_phase4.py", 4, 1), ("tests/test_diff_viewer.py", 2, 1), ("tests/test_discussion_takes_gui.py", 4, 1), ("tests/test_external_mcp_hitl.py", 4, 1), ("tests/test_gui_discussion_tabs.py", 4, 1), ("tests/test_gui_stress_performance.py", 4, 1), ("tests/test_gui_updates.py", 2, 1), ("tests/test_hot_reloader.py", 4, 1), ("tests/test_mma_dashboard_refresh.py", 4, 1), ("tests/test_mma_node_editor.py", 4, 1), ("tests/test_mma_orchestration_gui.py", 4, 1), ("tests/test_py_struct_tools.py", 4, 1), ("tests/test_thinking_persistence.py", 4, 1), ("tests/test_tier4_interceptor.py", 2, 1), ("tests/test_tiered_aggregation.py", 4, 1), ("tests/test_visual_orchestration.py", 4, 1), ] all_files = files_to_fix + test_files for rel_path, base, target in all_files: filepath = ROOT_DIR / rel_path if filepath.exists(): changed, msg = fix_indentation_simple(filepath, base, target) print(f"{rel_path}: {msg}") if __name__ == "__main__": main()