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

Function extract

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 7

stripCommentsForRegexFunction · 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