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

Function extractFlaskUrlRules

src/resolution/frameworks/python.ts:551–609  ·  view source on GitHub ↗

* Flask's imperative registration: `bp.add_url_rule('/path', 'name', * view_func=View.as_view('name'), methods=[…])`, and a project's own wrapper * around it — any call handing a list of path strings and a `view_func=` * over, like flaskbb's `register_view(auth, routes=["/login"], * view_func=Lo

(filePath: string, safe: string)

Source from the content-addressed store, hash-verified

549 * says; a plain function defaults to GET, as Flask does.
550 */
551function extractFlaskUrlRules(filePath: string, safe: string): FrameworkExtractionResult {
552 const nodes: Node[] = [];
553 const references: UnresolvedRef[] = [];
554 const now = Date.now();
555 const seen = new Set<string>();
556 const opener = /(\.add_url_rule|\b[A-Za-z_]\w*)\s*\(/g;
557 let m: RegExpExecArray | null;
558 while ((m = opener.exec(safe)) !== null) {
559 const open = m.index + m[0].length;
560 const args = balancedArgs(safe, open);
561 if (args === null || !/\bview_func\s*=|\.add_url_rule/.test(m[1]! + args)) continue;
562 const isRule = m[1] === '.add_url_rule';
563 let paths: string[] = [];
564 if (isRule) {
565 const first = /^\s*(?:rule\s*=\s*)?(['"])([^'"]*)\1/.exec(args);
566 if (first) paths = [first[2]!];
567 } else {
568 const list = /(?:^|,)\s*(?:routes|rules|urls|paths)?\s*=?\s*\[((?:\s*(['"])[^'"]*\2\s*,?)+)\]/.exec(args);
569 if (list) paths = (list[1]!.match(/(['"])([^'"]*)\1/g) ?? []).map((q) => q.slice(1, -1));
570 }
571 paths = paths.filter((p) => p.startsWith('/'));
572 if (paths.length === 0) continue;
573 // `view_func=X` / `view_func=X.as_view(…)`, or add_url_rule's third positional argument.
574 const expr = /\bview_func\s*=\s*([A-Za-z_][\w.]*)/.exec(args)?.[1] ??
575 (isRule ? /^\s*(['"])[^'"]*\1\s*,\s*(?:(['"])[^'"]*\2|None)\s*,\s*([A-Za-z_][\w.]*)/.exec(args)?.[3] : undefined);
576 if (!expr) continue;
577 const classView = expr.endsWith('.as_view');
578 const target = classView ? expr.slice(0, -'.as_view'.length) : expr;
579 const listed = /\bmethods\s*=\s*[[(]([^\])]*)[\])]/.exec(args)?.[1];
580 const methods = listed
581 ? (listed.match(/['"](\w+)['"]/g) ?? []).map((q) => q.slice(1, -1).toUpperCase())
582 : [classView ? 'ANY' : 'GET'];
583 const line = safe.slice(0, m.index).split('\n').length;
584 const name = target.split('.').pop()!;
585 for (const routePath of paths) {
586 for (const method of methods) {
587 const id = `route:${filePath}:${line}:${method}:${routePath}`;
588 if (seen.has(id)) continue;
589 seen.add(id);
590 nodes.push({
591 id,
592 kind: 'route',
593 name: `${method} ${routePath}`,
594 qualifiedName: `${filePath}::${method}:${routePath}`,
595 filePath,
596 startLine: line,
597 endLine: line,
598 startColumn: 0,
599 endColumn: 0,
600 language: 'python',
601 updatedAt: now,
602 });
603 references.push({ fromNodeId: id, referenceName: name, referenceKind: 'references', line, column: 0, filePath, language: 'python' });
604 }
605 }
606 opener.lastIndex = open + args.length;
607 }
608 return { nodes, references };

Callers 1

extractFunction · 0.85

Calls 6

balancedArgsFunction · 0.85
mapMethod · 0.80
hasMethod · 0.80
pushMethod · 0.80
execMethod · 0.65
addMethod · 0.45

Tested by

no test coverage detected