Spaces:
Sleeping
Sleeping
File size: 5,246 Bytes
80f6bb8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | """
Code parser using the standard `ast` module.
Extracts classes, functions, docstrings, imports, and calls.
"""
import ast
import os
from pathlib import Path
from dataclasses import dataclass, field
from typing import List, Dict, Optional, Set
from config import SUPPORTED_EXTENSIONS, IGNORE_DIRS, MAX_FILE_SIZE_KB
@dataclass
class ParsedFunction:
name: str
lineno: int
end_lineno: int
docstring: Optional[str]
args: List[str]
calls: Set[str] = field(default_factory=set)
@dataclass
class ParsedClass:
name: str
lineno: int
end_lineno: int
docstring: Optional[str]
bases: List[str]
methods: List[ParsedFunction] = field(default_factory=list)
@dataclass
class ParsedModule:
filepath: str
imports: Set[str] = field(default_factory=set)
classes: List[ParsedClass] = field(default_factory=list)
functions: List[ParsedFunction] = field(default_factory=list)
class CodeVisitor(ast.NodeVisitor):
def __init__(self):
self.imports = set()
self.classes = []
self.functions = []
self.current_class = None
self.current_function = None
def visit_Import(self, node):
for alias in node.names:
self.imports.add(alias.name)
self.generic_visit(node)
def visit_ImportFrom(self, node):
if node.module:
self.imports.add(node.module)
self.generic_visit(node)
def visit_Call(self, node):
if self.current_function:
# Try to extract the name of the called function
if isinstance(node.func, ast.Name):
self.current_function.calls.add(node.func.id)
elif isinstance(node.func, ast.Attribute):
self.current_function.calls.add(node.func.attr)
self.generic_visit(node)
def visit_ClassDef(self, node):
bases = []
for base in node.bases:
if isinstance(base, ast.Name):
bases.append(base.id)
elif isinstance(base, ast.Attribute):
bases.append(base.attr)
parsed_class = ParsedClass(
name=node.name,
lineno=node.lineno,
end_lineno=node.end_lineno or node.lineno,
docstring=ast.get_docstring(node),
bases=bases
)
self.classes.append(parsed_class)
self.current_class = parsed_class
self.generic_visit(node)
self.current_class = None
def visit_FunctionDef(self, node):
# Handle async functions as well
self._handle_function(node)
def visit_AsyncFunctionDef(self, node):
self._handle_function(node)
def _handle_function(self, node):
args = [arg.arg for arg in node.args.args]
if node.args.vararg:
args.append(f"*{node.args.vararg.arg}")
if node.args.kwarg:
args.append(f"**{node.args.kwarg.arg}")
parsed_func = ParsedFunction(
name=node.name,
lineno=node.lineno,
end_lineno=node.end_lineno or node.lineno,
docstring=ast.get_docstring(node),
args=args
)
if self.current_class:
self.current_class.methods.append(parsed_func)
else:
self.functions.append(parsed_func)
prev_func = self.current_function
self.current_function = parsed_func
self.generic_visit(node)
self.current_function = prev_func
class ProjectParser:
def __init__(self, root_dir: str):
self.root_dir = Path(root_dir)
def should_parse(self, filepath: Path) -> bool:
if filepath.suffix not in SUPPORTED_EXTENSIONS:
return False
# Check if in ignore dir
for part in filepath.parts:
if part in IGNORE_DIRS:
return False
# Check size
if filepath.stat().st_size > MAX_FILE_SIZE_KB * 1024:
return False
return True
def parse_file(self, filepath: Path) -> Optional[ParsedModule]:
try:
with open(filepath, "r", encoding="utf-8") as f:
content = f.read()
tree = ast.parse(content, filename=str(filepath))
visitor = CodeVisitor()
visitor.visit(tree)
# Use relative path for cleaner graph nodes
rel_path = str(filepath.relative_to(self.root_dir))
return ParsedModule(
filepath=rel_path,
imports=visitor.imports,
classes=visitor.classes,
functions=visitor.functions
)
except Exception as e:
print(f"Error parsing {filepath}: {e}")
return None
def parse_project(self) -> List[ParsedModule]:
modules = []
for root, _, files in os.walk(self.root_dir):
for file in files:
filepath = Path(root) / file
if self.should_parse(filepath):
module = self.parse_file(filepath)
if module:
modules.append(module)
return modules
|