MCPcopy Create free account
hub / github.com/modelscope/ms-agent / _extract_from_import

Method _extract_from_import

ms_agent/utils/parser_utils.py:81–121  ·  view source on GitHub ↗

Extract 'from ... import ...' statement

(self, match,
                             code_content)

Source from the content-addressed store, hash-verified

79 return imports
80
81 def _extract_from_import(self, match,
82 code_content) -> Optional[ImportInfo]:
83 """Extract 'from ... import ...' statement"""
84 module_path = match.group(1)
85 # Group 2 is parenthesized multi-line imports, group 3 is single-line imports
86 imports_str = (match.group(2) or match.group(3)).strip()
87
88 # Remove inline comments
89 lines = imports_str.split('\n')
90 cleaned_items = []
91 for line in lines:
92 if '#' in line:
93 line = line[:line.index('#')]
94 cleaned_items.append(line.strip())
95 imports_str = ','.join(cleaned_items)
96
97 # Parse imported items
98 imported_items = []
99 for item in imports_str.split(','):
100 item = item.strip()
101 if not item:
102 continue
103 if ' as ' in item:
104 imported_items.append(item.split(' as ')[0].strip())
105 elif item != '*':
106 imported_items.append(item)
107 elif item == '*':
108 imported_items = ['*']
109 break
110
111 # Resolve file path
112 file_path = self._resolve_python_path(module_path)
113 # If file not found, use module_path as source_file (could be stdlib or external package)
114 if not file_path:
115 file_path = module_path
116
117 return ImportInfo(
118 source_file=file_path,
119 raw_statement=match.group(0),
120 imported_items=imported_items,
121 import_type='namespace' if '*' in imported_items else 'named')
122
123 def _extract_simple_import(self, match) -> List[ImportInfo]:
124 """Extract 'import ...' statement"""

Callers 1

parseMethod · 0.95

Calls 2

_resolve_python_pathMethod · 0.95
ImportInfoClass · 0.85

Tested by

no test coverage detected