MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / extract

Function extract

src/resolution/frameworks/python.ts:64–141  ·  view source on GitHub ↗
(filePath, content)

Source from the content-addressed store, hash-verified

62 },
63
64 extract(filePath, content) {
65 if (!filePath.endsWith('.py')) return { nodes: [], references: [] };
66
67 const nodes: Node[] = [];
68 const references: UnresolvedRef[] = [];
69 const now = Date.now();
70 const safe = stripCommentsForRegex(content, 'python');
71
72 // path('url', handler, name=...) / re_path(r'...', handler) / url(r'...', handler)
73 // Capture groups: 1=function name, 2=url string, 3=handler expr
74 // Handler expr may contain one balanced () pair (e.g. View.as_view(), include('x.y'))
75 const routeRegex = /\b(path|re_path|url)\s*\(\s*r?['"]([^'"]+)['"]\s*,\s*([\w.]+(?:\s*\([^)]*\))?)/g;
76
77 let match: RegExpExecArray | null;
78 while ((match = routeRegex.exec(safe)) !== null) {
79 const [, _fn, urlPath, handlerExpr] = match;
80 const line = lineAt(safe, match.index);
81
82 const routeNode: Node = {
83 id: `route:${filePath}:${line}:${urlPath}`,
84 kind: 'route',
85 name: urlPath!,
86 qualifiedName: `${filePath}::route:${urlPath}`,
87 filePath,
88 startLine: line,
89 endLine: line,
90 startColumn: 0,
91 endColumn: match[0].length,
92 language: 'python',
93 updatedAt: now,
94 };
95 nodes.push(routeNode);
96
97 const handler = handlerExpr!.trim();
98 const target = resolveHandlerName(handler);
99 if (target) {
100 references.push({
101 fromNodeId: routeNode.id,
102 referenceName: target.name,
103 referenceKind: target.kind,
104 line,
105 column: 0,
106 filePath,
107 language: 'python',
108 });
109 }
110 }
111
112 // DRF router registration: `router.register(r'articles', ArticleViewSet)` →
113 // route → the ViewSet class (the core CRUD endpoints, which path()/url() miss).
114 // The STRING first arg separates this from `admin.site.register(Model, Admin)`
115 // (whose first arg is a model class, not a string); the View/ViewSet suffix on
116 // the 2nd arg keeps it to DRF viewsets.
117 const routerRegex = /\.register\s*\(\s*r?['"]([^'"]+)['"]\s*,\s*([\w.]+)/g;
118 while ((match = routerRegex.exec(safe)) !== null) {
119 const prefix = match[1]!.replace(/^\^|\/?\$$/g, '');
120 const viewset = match[2]!.split('.').pop()!;
121 if (!/View(Set)?$/.test(viewset)) continue;

Callers

nothing calls this directly

Calls 8

stripCommentsForRegexFunction · 0.90
lineAtFunction · 0.90
resolveHandlerNameFunction · 0.85
extractDecoratorRoutesFunction · 0.85
extractFlaskRestfulFunction · 0.85
extractFlaskUrlRulesFunction · 0.85
pushMethod · 0.80
execMethod · 0.65

Tested by

no test coverage detected