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

Function postExtract

src/resolution/frameworks/python.ts:312–395  ·  view source on GitHub ↗

* Cross-file finalization for prefixes. A router's routes are written * relative to where it is mounted — * * router = APIRouter(prefix="/items") # items.py * api_router.include_router(items.router) # api.py * app.include_router(api_router, prefix="/

(context)

Source from the content-addressed store, hash-verified

310 * `qualifiedName` are preserved, so the pass is idempotent on every sync.
311 */
312 postExtract(context) {
313 interface Mount {
314 fromVar: string;
315 prefix: string;
316 target: { file: string; variable: string };
317 }
318 const own = new Map<string, Map<string, string>>(); // file → router variable → APIRouter(prefix=)
319 const mounts = new Map<string, Mount[]>(); // mounting file → mounts
320 const receivers = new Map<string, Map<number, string>>(); // file → decorator line → router variable
321 for (const file of context.getAllFiles()) {
322 if (!file.endsWith('.py')) continue;
323 const content = context.readFile(file);
324 if (!content || (!content.includes('APIRouter') && !content.includes('include_router'))) continue;
325 const safe = stripCommentsForRegex(content, 'python');
326 const vars = new Map<string, string>();
327 const decl = /\b([A-Za-z_]\w*)\s*=\s*APIRouter\s*\(([^)]*)\)/g;
328 let m: RegExpExecArray | null;
329 while ((m = decl.exec(safe)) !== null) {
330 const p = /\bprefix\s*=\s*['"]([^'"]*)['"]/.exec(m[2]!);
331 vars.set(m[1]!, p ? p[1]! : '');
332 }
333 own.set(file, vars);
334 const byLine = new Map<number, string>();
335 const deco = /@([A-Za-z_]\w*)\.(?:get|post|put|patch|delete|options|head)\s*\(/g;
336 while ((m = deco.exec(safe)) !== null) byLine.set(lineAt(safe, m.index), m[1]!);
337 receivers.set(file, byLine);
338 const inc = /\b([A-Za-z_]\w*)\.include_router\s*\(\s*([A-Za-z_][\w.]*)\s*((?:,[^)]*)?)\)/g;
339 while ((m = inc.exec(safe)) !== null) {
340 const rest = m[3] ?? '';
341 const literal = /\bprefix\s*=\s*['"]([^'"]*)['"]/.exec(rest);
342 if (!literal && /\bprefix\s*=/.test(rest)) continue; // a computed prefix: unknown, not guessed
343 const target = includedRouter(m[2]!, file, vars, context);
344 if (!target) continue;
345 const list = mounts.get(file) ?? [];
346 list.push({ fromVar: m[1]!, prefix: literal ? literal[1]! : '', target });
347 mounts.set(file, list);
348 }
349 }
350 if (mounts.size === 0 && ![...own.values()].some((vars) => [...vars.values()].some((p) => p !== ''))) return [];
351
352 // The include-derived base of each (file, variable): the mounting router's
353 // base, plus its own prefix, plus the mount's — to a fixed point.
354 const key = (file: string, variable: string): string => `${file}\0${variable}`;
355 let base = new Map<string, string>();
356 for (let round = 0; round < 8; round++) {
357 const next = new Map<string, string>();
358 for (const [file, list] of mounts) {
359 for (const mount of list) {
360 const fromBase = base.get(key(file, mount.fromVar)) ?? '';
361 const fromOwn = own.get(file)?.get(mount.fromVar) ?? '';
362 const full = joinPyPaths(joinPyPaths(fromBase, fromOwn), mount.prefix);
363 const k = key(mount.target.file, mount.target.variable);
364 const seen = next.get(k);
365 if (seen !== undefined && seen !== full) next.set(k, '\0'); // two mounts, two paths: ambiguous
366 else next.set(k, full);
367 }
368 }
369 for (const [k, v] of [...next]) if (v === '\0') next.delete(k);

Callers

nothing calls this directly

Calls 13

stripCommentsForRegexFunction · 0.90
lineAtFunction · 0.90
includedRouterFunction · 0.85
joinPyPathsFunction · 0.85
pushMethod · 0.80
keyFunction · 0.70
getAllFilesMethod · 0.65
readFileMethod · 0.65
execMethod · 0.65
getMethod · 0.65
deleteMethod · 0.65
getNodesInFileMethod · 0.65

Tested by

no test coverage detected