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

Function extractPythonImports

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

* Extract Python import mappings

(content: string)

Source from the content-addressed store, hash-verified

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