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

Function extractPythonImports

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

* Extract Python import mappings

(content: string)

Source from the content-addressed store, hash-verified

2364 * Extract Python import mappings
2365 */
2366function extractPythonImports(content: string): ImportMapping[] {
2367 const mappings: ImportMapping[] = [];
2368 const code = stripCommentsForRegex(content, 'python');
2369
2370 // from X import Y [as Z], …
2371 for (const { source, names } of pythonFromImports(code)) {
2372 for (const { name, alias } of names) {
2373 mappings.push({
2374 localName: alias,
2375 exportedName: name,
2376 source,
2377 isDefault: false,
2378 isNamespace: false,
2379 });
2380 }
2381 }
2382
2383 // import X [as Y], … at module level. The mappings are the file's, so a
2384 // function-local import is left to its function: `import a.b as X` there
2385 // binds X in that function only (pythonFunctionModuleAlias).
2386 const importRe = /^import[ \t]+((?:[^\n\\;]|\\\r?\n)+)/gm;
2387 let match: RegExpExecArray | null;
2388 while ((match = importRe.exec(code)) !== null) {
2389 for (const raw of match[1]!.replace(/\\\r?\n/g, ' ').split(',')) {
2390 const im = raw.trim().match(/^([\w.]+)(?:\s+as\s+(\w+))?$/);
2391 if (!im) continue;
2392 mappings.push({
2393 localName: im[2] || im[1]!.split('.').pop()!,
2394 exportedName: '*',
2395 source: im[1]!,
2396 isDefault: false,
2397 isNamespace: true,
2398 });
2399 }
2400 }
2401
2402 return mappings;
2403}
2404
2405/**
2406 * Extract Go import mappings. An import is bound to the name written before

Callers 1

extractImportMappingsFunction · 0.85

Calls 4

stripCommentsForRegexFunction · 0.90
pythonFromImportsFunction · 0.85
pushMethod · 0.80
execMethod · 0.65

Tested by

no test coverage detected