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

Function postExtract

src/resolution/frameworks/python.ts:311–394  ·  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

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

Callers

nothing calls this directly

Calls 12

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

Tested by

no test coverage detected