mikemichez/codebase-genius-agentic-code-documentation-system
0
1python2import ast3from typing import Dict, List4 5def analyze_code(code: str, filename: str) -> Dict:6 """Analyze Python code and extract entities and relationships"""7 try:8 tree = ast.parse(code)9 except:10 return {'entities': [], 'relationships': []}11 12 entities = []13 14 for node in ast.walk(tree):15 if isinstance(node, ast.FunctionDef):16 entities.append({17 'name': node.name,18 'type': 'function',19 'docstring': ast.get_docstring(node) or '',20 'relationships': get_function_relationships(node)21 })22 elif isinstance(node, ast.ClassDef):23 entities.append({24 'name': node.name,25 'type': 'class',26 'docstring': ast.get_docstring(node) or '',27 'relationships': get_class_relationships(node)28 })29 30 return {'entities': entities, 'relationships': []}31 32def get_function_relationships(node: ast.FunctionDef) -> List[str]:33 """Extract function call relationships"""34 relationships = []35 36 for subnode in ast.walk(node):37 if isinstance(subnode, ast.Call) and isinstance(subnode.func, ast.Name):38 relationships.append(f"Calls: {subnode.func.id}")39 40 return relationships41 42def get_class_relationships(node: ast.ClassDef) -> List[str]:43 """Extract class inheritance relationships"""44 relationships = []45 46 for base in node.bases:47 if isinstance(base, ast.Name):48 relationships.append(f"Inherits from: {base.id}")49 50 return relationships51 52</html>