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

Function extractPythonImports

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

* Extract Python import mappings

(content: string)

Source from the content-addressed store, hash-verified

717 * Extract Python import mappings
718 */
719function extractPythonImports(content: string): ImportMapping[] {
720 const mappings: ImportMapping[] = [];
721
722 // from X import Y
723 const fromImportRegex = /from\s+([\w.]+)\s+import\s+([^#\n]+)/g;
724 let match;
725
726 while ((match = fromImportRegex.exec(content)) !== null) {
727 const [, source, imports] = match;
728 const names = imports!.split(',').map((s) => s.trim());
729
730 for (const name of names) {
731 const aliasMatch = name.match(/(\w+)\s+as\s+(\w+)/);
732 if (aliasMatch) {
733 mappings.push({
734 localName: aliasMatch[2]!,
735 exportedName: aliasMatch[1]!,
736 source: source!,
737 isDefault: false,
738 isNamespace: false,
739 });
740 } else if (name && name !== '*') {
741 mappings.push({
742 localName: name,
743 exportedName: name,
744 source: source!,
745 isDefault: false,
746 isNamespace: false,
747 });
748 }
749 }
750 }
751
752 // import X
753 const importRegex = /^import\s+([\w.]+)(?:\s+as\s+(\w+))?/gm;
754 while ((match = importRegex.exec(content)) !== null) {
755 const [, source, alias] = match;
756 const localName = alias || source!.split('.').pop()!;
757 mappings.push({
758 localName,
759 exportedName: '*',
760 source: source!,
761 isDefault: false,
762 isNamespace: true,
763 });
764 }
765
766 return mappings;
767}
768
769/**
770 * Extract Go import mappings

Callers 1

extractImportMappingsFunction · 0.85

Calls 1

execMethod · 0.65

Tested by

no test coverage detected