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

Function pythonFromImports

src/resolution/import-resolver.ts:2587–2609  ·  view source on GitHub ↗

* The `from X import …` statements of a Python file, read over comment- and * docstring-blanked source so a usage example in a docstring binds nothing. * Handles the parenthesised multi-line list (`from .x import (\n a,\n b,\n)`, * the usual shape of a package `__init__.py`) and backslash conti

(code: string)

Source from the content-addressed store, hash-verified

2585 * which a single-line match silently dropped.
2586 */
2587function pythonFromImports(code: string): PythonFromImport[] {
2588 const out: PythonFromImport[] = [];
2589 const fromRe = /^[ \t]*from[ \t]+(\.*[\w.]*)[ \t]+import[ \t]*(\([^)]*\)|(?:[^\n\\;]|\\\r?\n)+)/gm;
2590 let m: RegExpExecArray | null;
2591 while ((m = fromRe.exec(code)) !== null) {
2592 const source = m[1]!;
2593 if (!source) continue;
2594 const list = m[2]!.replace(/^\(|\)$/g, '').replace(/\\\r?\n/g, ' ');
2595 const names: PythonFromImport['names'] = [];
2596 let star = false;
2597 for (const raw of list.split(',')) {
2598 const item = raw.trim();
2599 if (item === '*') {
2600 star = true;
2601 continue;
2602 }
2603 const im = item.match(/^(\w+)(?:\s+as\s+(\w+))?$/);
2604 if (im) names.push({ name: im[1]!, alias: im[2] ?? im[1]! });
2605 }
2606 out.push({ source, names, star });
2607 }
2608 return out;
2609}
2610
2611/**
2612 * Extract Python import mappings

Callers 2

extractPythonImportsFunction · 0.85
extractPythonReExportsFunction · 0.85

Calls 2

pushMethod · 0.80
execMethod · 0.65

Tested by

no test coverage detected