152
def _convert_import_str_to_map(import_str, imports):
153
"""This converts a import string into an import map.
155
This only understands 'import foo, foo.bar, foo.bar.baz as bing'
157
:param import_str: The import string to process
158
:param imports: The current map of all imports
160
assert import_str.startswith('import ')
161
import_str = import_str[len('import '):]
163
for path in import_str.split(','):
165
as_hunks = path.split(' as ')
166
if len(as_hunks) == 2:
167
# We have 'as' so this is a different style of import
168
# 'import foo.bar.baz as bing' creates a local variable
169
# named 'bing' which points to 'foo.bar.baz'
170
name = as_hunks[1].strip()
171
module_path = as_hunks[0].strip().split('.')
172
assert name not in imports
173
# No children available in 'import foo as bar'
174
imports[name] = (module_path, None, {})
176
# Now we need to handle
177
module_path = path.split('.')
178
name = module_path[0]
179
if name not in imports:
180
# This is a new import that we haven't seen before
181
module_def = ([name], None, {})
182
imports[name] = module_def
184
module_def = imports[name]
188
for child in module_path[1:]:
189
cur_path.append(child)
193
next = (cur_path[:], None, {})
198
def _convert_from_str_to_map(from_str, imports):
199
"""This converts a 'from foo import bar' string into an import map.
201
:param from_str: The import string to process
202
:param imports: The current map of all imports
204
assert from_str.startswith('from ')
205
from_str = from_str[len('from '):]
207
from_module, import_list = from_str.split(' import ')
209
from_module_path = from_module.split('.')
211
for path in import_list.split(','):
213
as_hunks = path.split(' as ')
214
if len(as_hunks) == 2:
215
# We have 'as' so this is a different style of import
216
# 'import foo.bar.baz as bing' creates a local variable
217
# named 'bing' which points to 'foo.bar.baz'
218
name = as_hunks[1].strip()
219
module = as_hunks[0].strip()
222
assert name not in imports
223
imports[name] = (from_module_path, module, {})
226
def _canonicalize_import_strings(text):
227
"""Take a list of imports, and split it into regularized form.
229
This is meant to take regular import text, and convert it to
230
the forms that the rest of the converters prefer.
236
for line in text.split('\n'):
240
line = line[:loc].strip()
245
if line.endswith(')'):
246
out.append(cur + ' ' + line[:-1])
251
if '(' in line and ')' not in line:
252
cur = line.replace('(', '')
254
out.append(line.replace('(', '').replace(')', ''))
152
class ImportProcessor(object):
153
"""Convert text that users input into import requests"""
155
__slots__ = ['imports']
160
def _convert_import_str(self, import_str):
161
"""This converts a import string into an import map.
163
This only understands 'import foo, foo.bar, foo.bar.baz as bing'
165
:param import_str: The import string to process
167
assert import_str.startswith('import ')
168
import_str = import_str[len('import '):]
170
for path in import_str.split(','):
172
as_hunks = path.split(' as ')
173
if len(as_hunks) == 2:
174
# We have 'as' so this is a different style of import
175
# 'import foo.bar.baz as bing' creates a local variable
176
# named 'bing' which points to 'foo.bar.baz'
177
name = as_hunks[1].strip()
178
module_path = as_hunks[0].strip().split('.')
179
assert name not in self.imports
180
# No children available in 'import foo as bar'
181
self.imports[name] = (module_path, None, {})
183
# Now we need to handle
184
module_path = path.split('.')
185
name = module_path[0]
186
if name not in self.imports:
187
# This is a new import that we haven't seen before
188
module_def = ([name], None, {})
189
self.imports[name] = module_def
191
module_def = self.imports[name]
195
for child in module_path[1:]:
196
cur_path.append(child)
200
next = (cur_path[:], None, {})
204
def _convert_from_str(self, from_str):
205
"""This converts a 'from foo import bar' string into an import map.
207
:param from_str: The import string to process
209
assert from_str.startswith('from ')
210
from_str = from_str[len('from '):]
212
from_module, import_list = from_str.split(' import ')
214
from_module_path = from_module.split('.')
216
for path in import_list.split(','):
218
as_hunks = path.split(' as ')
219
if len(as_hunks) == 2:
220
# We have 'as' so this is a different style of import
221
# 'import foo.bar.baz as bing' creates a local variable
222
# named 'bing' which points to 'foo.bar.baz'
223
name = as_hunks[1].strip()
224
module = as_hunks[0].strip()
227
assert name not in self.imports
228
self.imports[name] = (from_module_path, module, {})
231
def _canonicalize_import_strings(self, text):
232
"""Take a list of imports, and split it into regularized form.
234
This is meant to take regular import text, and convert it to
235
the forms that the rest of the converters prefer.
241
for line in text.split('\n'):
245
line = line[:loc].strip()
250
if line.endswith(')'):
251
out.append(cur + ' ' + line[:-1])
256
if '(' in line and ')' not in line:
257
cur = line.replace('(', '')
259
out.append(line.replace('(', '').replace(')', ''))