"""AST-based function extraction and splicing for Python files.""" import ast import textwrap from dataclasses import dataclass from pathlib import Path @dataclass class FunctionInfo: """Extracted function metadata and source.""" name: str filepath: str start_line: int # 1-indexed, includes decorators end_line: int # 1-indexed, inclusive source: str # dedented source code indent: str # original indentation class_name: str | None = None # if it's a method def extract_function(filepath: str, func_name: str) -> FunctionInfo: """Extract a function/method from a Python file by name. For class methods, use "ClassName.method_name" notation. Raises: FileNotFoundError: file doesn't exist ValueError: function not found SyntaxError: file has syntax errors """ path = Path(filepath) source = path.read_text() tree = ast.parse(source, filename=filepath) lines = source.splitlines(keepends=True) class_name = None target_name = func_name if "." in func_name: class_name, target_name = func_name.split(".", 1) node = _find_function(tree, target_name, class_name) if node is None: if class_name: raise ValueError(f"Method {class_name}.{target_name} not found in {filepath}") raise ValueError(f"Function {target_name} not found in {filepath}") # Line range: include decorators start = node.decorator_list[0].lineno if node.decorator_list else node.lineno end = node.end_lineno # Extract source lines (1-indexed to 0-indexed) func_lines = lines[start - 1:end] func_source = "".join(func_lines) # Detect indentation from the def/async def line def_line = lines[node.lineno - 1] indent = def_line[: len(def_line) - len(def_line.lstrip())] # Dedent for clean presentation to model dedented = textwrap.dedent(func_source) return FunctionInfo( name=target_name, filepath=filepath, start_line=start, end_line=end, source=dedented.rstrip("\n"), indent=indent, class_name=class_name, ) def splice_function(filepath: str, info: FunctionInfo, new_source: str) -> str: """Replace a function in a file with new source code. Re-indents new_source to match original indentation. Returns the full new file content (does NOT write to disk). """ path = Path(filepath) lines = path.read_text().splitlines(keepends=True) # Re-indent new source to match original reindented = _reindent(new_source, info.indent) # Ensure trailing newline if not reindented.endswith("\n"): reindented += "\n" # Splice: replace start_line..end_line before = lines[: info.start_line - 1] after = lines[info.end_line:] return "".join(before) + reindented + "".join(after) def list_functions(filepath: str) -> list[FunctionInfo]: """List all top-level functions and class methods in a file.""" path = Path(filepath) source = path.read_text() tree = ast.parse(source, filename=filepath) lines = source.splitlines(keepends=True) results = [] for node in ast.iter_child_nodes(tree): if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): results.append(_node_to_info(node, lines, filepath)) elif isinstance(node, ast.ClassDef): for child in ast.iter_child_nodes(node): if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef)): results.append(_node_to_info(child, lines, filepath, node.name)) return results def _find_function(tree: ast.Module, name: str, class_name: str | None) -> ast.FunctionDef | None: """Walk AST to find function/method node.""" for node in ast.iter_child_nodes(tree): if class_name is None and isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): if node.name == name: return node elif isinstance(node, ast.ClassDef) and (class_name is None or node.name == class_name): for child in ast.iter_child_nodes(node): if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef)) and child.name == name: return child return None def _node_to_info(node, lines, filepath, class_name=None): """Convert an AST function node to FunctionInfo.""" start = node.decorator_list[0].lineno if node.decorator_list else node.lineno end = node.end_lineno func_lines = lines[start - 1:end] func_source = "".join(func_lines) def_line = lines[node.lineno - 1] indent = def_line[: len(def_line) - len(def_line.lstrip())] dedented = textwrap.dedent(func_source) return FunctionInfo( name=node.name, filepath=filepath, start_line=start, end_line=end, source=dedented.rstrip("\n"), indent=indent, class_name=class_name, ) def _reindent(source: str, indent: str) -> str: """Re-indent source code to a target indentation level.""" # Dedent fully first, then add target indent dedented = textwrap.dedent(source) result = [] for line in dedented.splitlines(keepends=True): if line.strip(): # non-empty lines get indented result.append(indent + line) else: result.append(line) return "".join(result)