pyflyby 1.9.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of pyflyby might be problematic. Click here for more details.

Files changed (54) hide show
  1. pyflyby/__init__.py +56 -0
  2. pyflyby/__main__.py +9 -0
  3. pyflyby/_autoimp.py +2114 -0
  4. pyflyby/_cmdline.py +531 -0
  5. pyflyby/_comms.py +221 -0
  6. pyflyby/_dbg.py +1339 -0
  7. pyflyby/_docxref.py +379 -0
  8. pyflyby/_file.py +738 -0
  9. pyflyby/_flags.py +230 -0
  10. pyflyby/_format.py +182 -0
  11. pyflyby/_idents.py +233 -0
  12. pyflyby/_import_sorting.py +165 -0
  13. pyflyby/_importclns.py +642 -0
  14. pyflyby/_importdb.py +588 -0
  15. pyflyby/_imports2s.py +639 -0
  16. pyflyby/_importstmt.py +662 -0
  17. pyflyby/_interactive.py +2605 -0
  18. pyflyby/_livepatch.py +793 -0
  19. pyflyby/_log.py +199 -0
  20. pyflyby/_modules.py +515 -0
  21. pyflyby/_parse.py +1441 -0
  22. pyflyby/_py.py +2078 -0
  23. pyflyby/_util.py +459 -0
  24. pyflyby/_version.py +7 -0
  25. pyflyby/autoimport.py +20 -0
  26. pyflyby/importdb.py +19 -0
  27. pyflyby-1.9.4.data/data/etc/pyflyby/canonical.py +10 -0
  28. pyflyby-1.9.4.data/data/etc/pyflyby/common.py +27 -0
  29. pyflyby-1.9.4.data/data/etc/pyflyby/forget.py +10 -0
  30. pyflyby-1.9.4.data/data/etc/pyflyby/mandatory.py +10 -0
  31. pyflyby-1.9.4.data/data/etc/pyflyby/numpy.py +156 -0
  32. pyflyby-1.9.4.data/data/etc/pyflyby/std.py +335 -0
  33. pyflyby-1.9.4.data/data/libexec/pyflyby/colordiff +34 -0
  34. pyflyby-1.9.4.data/data/libexec/pyflyby/diff-colorize +148 -0
  35. pyflyby-1.9.4.data/data/share/doc/pyflyby/LICENSE.txt +23 -0
  36. pyflyby-1.9.4.data/data/share/doc/pyflyby/TODO.txt +115 -0
  37. pyflyby-1.9.4.data/data/share/doc/pyflyby/testing.txt +13 -0
  38. pyflyby-1.9.4.data/data/share/emacs/site-lisp/pyflyby.el +108 -0
  39. pyflyby-1.9.4.data/scripts/collect-exports +76 -0
  40. pyflyby-1.9.4.data/scripts/collect-imports +58 -0
  41. pyflyby-1.9.4.data/scripts/find-import +38 -0
  42. pyflyby-1.9.4.data/scripts/list-bad-xrefs +34 -0
  43. pyflyby-1.9.4.data/scripts/prune-broken-imports +34 -0
  44. pyflyby-1.9.4.data/scripts/pyflyby-diff +34 -0
  45. pyflyby-1.9.4.data/scripts/reformat-imports +27 -0
  46. pyflyby-1.9.4.data/scripts/replace-star-imports +37 -0
  47. pyflyby-1.9.4.data/scripts/tidy-imports +191 -0
  48. pyflyby-1.9.4.data/scripts/transform-imports +47 -0
  49. pyflyby-1.9.4.dist-info/LICENSE.txt +23 -0
  50. pyflyby-1.9.4.dist-info/METADATA +507 -0
  51. pyflyby-1.9.4.dist-info/RECORD +54 -0
  52. pyflyby-1.9.4.dist-info/WHEEL +5 -0
  53. pyflyby-1.9.4.dist-info/entry_points.txt +3 -0
  54. pyflyby-1.9.4.dist-info/top_level.txt +1 -0
pyflyby/_imports2s.py ADDED
@@ -0,0 +1,639 @@
1
+ # pyflyby/_imports2s.py.
2
+ # Copyright (C) 2011-2018 Karl Chen.
3
+ # License: MIT http://opensource.org/licenses/MIT
4
+
5
+ from pyflyby._autoimp import scan_for_import_issues
6
+ from pyflyby._file import FileText, Filename
7
+ from pyflyby._flags import CompilerFlags
8
+ from pyflyby._importclns import ImportSet, NoSuchImportError
9
+ from pyflyby._importdb import ImportDB
10
+ from pyflyby._importstmt import ImportFormatParams, ImportStatement
11
+ from pyflyby._log import logger
12
+ from pyflyby._parse import PythonBlock
13
+ from pyflyby._util import ImportPathCtx, Inf, NullCtx, memoize
14
+ import re
15
+
16
+ from typing import Union
17
+
18
+ from textwrap import indent
19
+
20
+
21
+ class SourceToSourceTransformationBase(object):
22
+
23
+ input: PythonBlock
24
+
25
+ def __new__(cls, arg):
26
+ if isinstance(arg, cls):
27
+ return arg
28
+ if isinstance(arg, (PythonBlock, FileText, Filename, str)):
29
+ return cls._from_source_code(arg)
30
+ raise TypeError("%s: got unexpected %s"
31
+ % (cls.__name__, type(arg).__name__))
32
+
33
+ @classmethod
34
+ def _from_source_code(cls, codeblock):
35
+ # TODO: don't do that.
36
+ self = object.__new__(cls)
37
+ if isinstance(codeblock, PythonBlock):
38
+ self.input = codeblock
39
+ elif isinstance(codeblock, FileText):
40
+ self.input = PythonBlock(codeblock)
41
+ else:
42
+ if not codeblock.endswith('\n'):
43
+ codeblock += '\n'
44
+ self.input = PythonBlock(codeblock)
45
+ self.preprocess()
46
+ return self
47
+
48
+ def preprocess(self):
49
+ pass
50
+
51
+ def pretty_print(self, params=None):
52
+ raise NotImplementedError
53
+
54
+ def output(self, params=None) -> PythonBlock:
55
+ """
56
+ Pretty-print and return as a `PythonBlock`.
57
+
58
+ :rtype:
59
+ `PythonBlock`
60
+ """
61
+ result = self.pretty_print(params=params)
62
+ result = PythonBlock(result, filename=self.input.filename)
63
+ return result
64
+
65
+ def __repr__(self):
66
+ return f"<{self.__class__.__name__}\n{indent(str(self.pretty_print()),' ')}\n at 0x{hex(id(self))}>"
67
+
68
+
69
+ class SourceToSourceTransformation(SourceToSourceTransformationBase):
70
+
71
+ _output: PythonBlock
72
+
73
+ def preprocess(self):
74
+ assert isinstance(self.input, PythonBlock), self.input
75
+ self._output = self.input
76
+
77
+ def pretty_print(self, params=None):
78
+ return self._output.text
79
+
80
+
81
+ class SourceToSourceImportBlockTransformation(SourceToSourceTransformationBase):
82
+ def preprocess(self):
83
+ self.importset = ImportSet(self.input, ignore_shadowed=True)
84
+
85
+ def pretty_print(self, params=None):
86
+ params = ImportFormatParams(params)
87
+ return self.importset.pretty_print(params)
88
+
89
+ def __repr__(self):
90
+ return f"<SourceToSourceImportBlockTransformation {self.importset!r} @{hex(id(self))}>"
91
+
92
+
93
+ class LineNumberNotFoundError(Exception):
94
+ pass
95
+
96
+ class LineNumberAmbiguousError(Exception):
97
+ pass
98
+
99
+ class NoImportBlockError(Exception):
100
+ pass
101
+
102
+ class ImportAlreadyExistsError(Exception):
103
+ pass
104
+
105
+ class SourceToSourceFileImportsTransformation(SourceToSourceTransformationBase):
106
+ def preprocess(self):
107
+ # Group into blocks of imports and non-imports. Get a sequence of all
108
+ # imports for the transformers to operate on.
109
+ self.blocks = []
110
+ self.import_blocks = []
111
+ for is_imports, subblock in self.input.groupby(lambda ps: ps.is_import):
112
+ if is_imports:
113
+ trans = SourceToSourceImportBlockTransformation(subblock)
114
+ self.import_blocks.append(trans)
115
+ else:
116
+ trans = SourceToSourceTransformation(subblock)
117
+ self.blocks.append(trans)
118
+
119
+ def pretty_print(self, params=None):
120
+ params = ImportFormatParams(params)
121
+ result = [block.pretty_print(params=params) for block in self.blocks]
122
+ return FileText.concatenate(result)
123
+
124
+ def find_import_block_by_lineno(self, lineno):
125
+ """
126
+ Find the import block containing the given line number.
127
+
128
+ :type lineno:
129
+ ``int``
130
+ :rtype:
131
+ `SourceToSourceImportBlockTransformation`
132
+ """
133
+ results = [
134
+ b
135
+ for b in self.import_blocks
136
+ if b.input.startpos.lineno <= lineno <= b.input.endpos.lineno]
137
+ if len(results) == 0:
138
+ raise LineNumberNotFoundError(lineno)
139
+ if len(results) > 1:
140
+ raise LineNumberAmbiguousError(lineno)
141
+ return results[0]
142
+
143
+ def remove_import(self, imp, lineno):
144
+ """
145
+ Remove the given import.
146
+
147
+ :type imp:
148
+ `Import`
149
+ :type lineno:
150
+ ``int``
151
+ """
152
+ block = self.find_import_block_by_lineno(lineno)
153
+ try:
154
+ imports = block.importset.by_import_as[imp.import_as]
155
+ except KeyError:
156
+ raise NoSuchImportError
157
+ assert len(imports)
158
+ if len(imports) > 1:
159
+ raise Exception("Multiple imports to remove: %r" % (imports,))
160
+ imp = imports[0]
161
+ block.importset = block.importset.without_imports([imp])
162
+ return imp
163
+
164
+ def select_import_block_by_closest_prefix_match(self, imp, max_lineno):
165
+ """
166
+ Heuristically pick an import block that ``imp`` "fits" best into. The
167
+ selection is based on the block that contains the import with the
168
+ longest common prefix.
169
+
170
+ :type imp:
171
+ `Import`
172
+ :param max_lineno:
173
+ Only return import blocks earlier than ``max_lineno``.
174
+ :rtype:
175
+ `SourceToSourceImportBlockTransformation`
176
+ """
177
+ # Create a data structure that annotates blocks with data by which
178
+ # we'll sort.
179
+ annotated_blocks = [
180
+ ( (max([0] + [len(imp.prefix_match(oimp))
181
+ for oimp in block.importset.imports]),
182
+ block.input.endpos.lineno),
183
+ block )
184
+ for block in self.import_blocks
185
+ if block.input.endpos.lineno <= max_lineno+1 ]
186
+ if not annotated_blocks:
187
+ raise NoImportBlockError()
188
+ annotated_blocks.sort()
189
+ if imp.split.module_name == '__future__':
190
+ # For __future__ imports, only add to an existing block that
191
+ # already contains __future__ import(s). If there are no existing
192
+ # import blocks containing __future__, don't return any result
193
+ # here, so that we will add a new one at the top.
194
+ if not annotated_blocks[-1][0][0] > 0:
195
+ raise NoImportBlockError
196
+ return annotated_blocks[-1][1]
197
+
198
+ def insert_new_blocks_after_comments(self, blocks):
199
+ blocks = [SourceToSourceTransformationBase(block) for block in blocks]
200
+ if isinstance(self.blocks[0], SourceToSourceImportBlockTransformation):
201
+ # Kludge. We should add an "output" attribute to
202
+ # SourceToSourceImportBlockTransformation and enumerate over that,
203
+ # instead of enumerating over the input below.
204
+ self.blocks[0:0] = blocks
205
+ return
206
+ # Get the "statements" in the first block.
207
+ statements = self.blocks[0].input.statements
208
+ # Find the insertion point.
209
+ for idx, statement in enumerate(statements):
210
+ if not statement.is_comment_or_blank_or_string_literal:
211
+ if idx == 0:
212
+ # First block starts with a noncomment, so insert before
213
+ # it.
214
+ self.blocks[0:0] = blocks
215
+ else:
216
+ # Found a non-comment after comment, so break it up and
217
+ # insert in the middle.
218
+ self.blocks[:1] = (
219
+ [SourceToSourceTransformation(
220
+ PythonBlock.concatenate(statements[:idx]))] +
221
+ blocks +
222
+ [SourceToSourceTransformation(
223
+ PythonBlock.concatenate(statements[idx:]))])
224
+ break
225
+ else:
226
+ # First block is entirely comments, so just insert after it.
227
+ self.blocks[1:1] = blocks
228
+
229
+ def insert_new_import_block(self):
230
+ """
231
+ Adds a new empty imports block. It is added before the first
232
+ non-comment statement. Intended to be used when the input contains no
233
+ import blocks (before uses).
234
+ """
235
+ block = SourceToSourceImportBlockTransformation("")
236
+ sepblock = SourceToSourceTransformation("")
237
+ sepblock._output = PythonBlock("\n")
238
+ self.insert_new_blocks_after_comments([block, sepblock])
239
+ self.import_blocks.insert(0, block)
240
+ return block
241
+
242
+ def add_import(self, imp, lineno=Inf):
243
+ """
244
+ Add the specified import. Picks an existing global import block to
245
+ add to, or if none found, creates a new one near the beginning of the
246
+ module.
247
+
248
+ :type imp:
249
+ `Import`
250
+ :param lineno:
251
+ Line before which to add the import. ``Inf`` means no constraint.
252
+ """
253
+ try:
254
+ block = self.select_import_block_by_closest_prefix_match(
255
+ imp, lineno)
256
+ except NoImportBlockError:
257
+ block = self.insert_new_import_block()
258
+ if imp in block.importset.imports:
259
+ raise ImportAlreadyExistsError(imp)
260
+ block.importset = block.importset.with_imports([imp])
261
+
262
+
263
+ def reformat_import_statements(codeblock, params=None):
264
+ r"""
265
+ Reformat each top-level block of import statements within a block of code.
266
+ Blank lines, comments, etc. are left alone and separate blocks of imports.
267
+
268
+ Parse the entire code block into an ast, group into consecutive import
269
+ statements and other lines. Each import block consists entirely of
270
+ 'import' (or 'from ... import') statements. Other lines, including blanks
271
+ and comment lines, are not touched.
272
+
273
+ >>> print(reformat_import_statements(
274
+ ... 'from foo import bar2 as bar2x, bar1\n'
275
+ ... 'import foo.bar3 as bar3x\n'
276
+ ... 'import foo.bar4\n'
277
+ ... '\n'
278
+ ... 'import foo.bar0 as bar0\n').text.joined)
279
+ import foo.bar4
280
+ from foo import bar1, bar2 as bar2x, bar3 as bar3x
281
+ <BLANKLINE>
282
+ from foo import bar0
283
+ <BLANKLINE>
284
+
285
+ :type codeblock:
286
+ `PythonBlock` or convertible (``str``)
287
+ :type params:
288
+ `ImportFormatParams`
289
+ :rtype:
290
+ `PythonBlock`
291
+ """
292
+ params = ImportFormatParams(params)
293
+ transformer = SourceToSourceFileImportsTransformation(codeblock)
294
+ return transformer.output(params=params)
295
+
296
+
297
+ def ImportPathForRelativeImportsCtx(codeblock):
298
+ """
299
+ Context manager that temporarily modifies ``sys.path`` so that relative
300
+ imports for the given ``codeblock`` work as expected.
301
+
302
+ :type codeblock:
303
+ `PythonBlock`
304
+ """
305
+ if not isinstance(codeblock, PythonBlock):
306
+ codeblock = PythonBlock(codeblock)
307
+ if not codeblock.filename:
308
+ return NullCtx()
309
+ if codeblock.flags & CompilerFlags("absolute_import"):
310
+ return NullCtx()
311
+ return ImportPathCtx(str(codeblock.filename.dir))
312
+
313
+
314
+ def fix_unused_and_missing_imports(
315
+ codeblock: Union[PythonBlock, str],
316
+ add_missing=True,
317
+ remove_unused="AUTOMATIC",
318
+ add_mandatory=True,
319
+ db=None,
320
+ params=None,
321
+ ) -> PythonBlock:
322
+ r"""
323
+ Check for unused and missing imports, and fix them automatically.
324
+
325
+ Also formats imports.
326
+
327
+ In the example below, ``m1`` and ``m3`` are unused, so are automatically
328
+ removed. ``np`` was undefined, so an ``import numpy as np`` was
329
+ automatically added.
330
+
331
+ >>> codeblock = PythonBlock(
332
+ ... 'from foo import m1, m2, m3, m4\n'
333
+ ... 'm2, m4, np.foo', filename="/tmp/foo.py")
334
+
335
+ >>> print(fix_unused_and_missing_imports(codeblock, add_mandatory=False))
336
+ [PYFLYBY] /tmp/foo.py: removed unused 'from foo import m1'
337
+ [PYFLYBY] /tmp/foo.py: removed unused 'from foo import m3'
338
+ [PYFLYBY] /tmp/foo.py: added 'import numpy as np'
339
+ import numpy as np
340
+ from foo import m2, m4
341
+ m2, m4, np.foo
342
+
343
+ :type codeblock:
344
+ `PythonBlock` or convertible (``str``)
345
+ :rtype:
346
+ `PythonBlock`
347
+ """
348
+ if isinstance(codeblock, Filename):
349
+ _codeblock = PythonBlock(codeblock)
350
+ if not isinstance(codeblock, PythonBlock):
351
+ _codeblock = PythonBlock(codeblock)
352
+ else:
353
+ _codeblock = codeblock
354
+ if remove_unused == "AUTOMATIC":
355
+ fn = _codeblock.filename
356
+ remove_unused = not (fn and
357
+ (fn.base == "__init__.py"
358
+ or ".pyflyby" in str(fn).split("/")))
359
+ elif remove_unused is True or remove_unused is False:
360
+ pass
361
+ else:
362
+ raise ValueError("Invalid remove_unused=%r" % (remove_unused,))
363
+ params = ImportFormatParams(params)
364
+ db = ImportDB.interpret_arg(db, target_filename=_codeblock.filename)
365
+ # Do a first pass reformatting the imports to get rid of repeated or
366
+ # shadowed imports, e.g. L1 here:
367
+ # import foo # L1
368
+ # import foo # L2
369
+ # foo # L3
370
+ _codeblock = reformat_import_statements(_codeblock, params=params)
371
+
372
+ filename = _codeblock.filename
373
+ transformer = SourceToSourceFileImportsTransformation(_codeblock)
374
+ missing_imports, unused_imports = scan_for_import_issues(
375
+ _codeblock, find_unused_imports=remove_unused, parse_docstrings=True
376
+ )
377
+ logger.debug("missing_imports = %r", missing_imports)
378
+ logger.debug("unused_imports = %r", unused_imports)
379
+ if remove_unused and unused_imports:
380
+ # Go through imports to remove. [This used to be organized by going
381
+ # through import blocks and removing all relevant blocks from there,
382
+ # but if one removal caused problems the whole thing would fail. The
383
+ # CPU cost of calling without_imports() multiple times isn't worth
384
+ # that.]
385
+ # TODO: don't remove unused mandatory imports. [This isn't
386
+ # implemented yet because this isn't necessary for __future__ imports
387
+ # since they aren't reported as unused, and those are the only ones we
388
+ # have by default right now.]
389
+ for lineno, imp in unused_imports:
390
+ try:
391
+ imp = transformer.remove_import(imp, lineno)
392
+ except NoSuchImportError:
393
+ logger.error(
394
+ "%s: couldn't remove import %r", filename, imp,)
395
+ except LineNumberNotFoundError as e:
396
+ logger.error(
397
+ "%s: unused import %r on line %d not global",
398
+ filename, str(imp), e.args[0])
399
+ else:
400
+ logger.info("%s: removed unused '%s'", filename, imp)
401
+
402
+ if add_missing and missing_imports:
403
+ missing_imports.sort(key=lambda k: (k[1], k[0]))
404
+ known = db.known_imports.by_import_as
405
+ # Decide on where to put each import to be added. Find the import
406
+ # block with the longest common prefix. Tie-break by preferring later
407
+ # blocks.
408
+ added_imports = set()
409
+ for lineno, ident in missing_imports:
410
+ import_as = ident.parts[0]
411
+ try:
412
+ imports = known[import_as]
413
+ except KeyError:
414
+ logger.warning(
415
+ "%s:%s: undefined name %r and no known import for it",
416
+ filename, lineno, import_as)
417
+ continue
418
+ if len(imports) != 1:
419
+ logger.error("%s: don't know which of %r to use",
420
+ filename, imports)
421
+ continue
422
+ imp_to_add = imports[0]
423
+ if imp_to_add in added_imports:
424
+ continue
425
+ transformer.add_import(imp_to_add, lineno)
426
+ added_imports.add(imp_to_add)
427
+ logger.info("%s: added %r", filename,
428
+ imp_to_add.pretty_print().strip())
429
+
430
+ if add_mandatory:
431
+ # Todo: allow not adding to empty __init__ files?
432
+ mandatory = db.mandatory_imports.imports
433
+ for imp in mandatory:
434
+ try:
435
+ transformer.add_import(imp)
436
+ except ImportAlreadyExistsError:
437
+ pass
438
+ else:
439
+ logger.info("%s: added mandatory %r",
440
+ filename, imp.pretty_print().strip())
441
+
442
+ return transformer.output(params=params)
443
+
444
+
445
+ def remove_broken_imports(codeblock, params=None):
446
+ """
447
+ Try to execute each import, and remove the ones that don't work.
448
+
449
+ Also formats imports.
450
+
451
+ :type codeblock:
452
+ `PythonBlock` or convertible (``str``)
453
+ :rtype:
454
+ `PythonBlock`
455
+ """
456
+ if not isinstance(codeblock, PythonBlock):
457
+ codeblock = PythonBlock(codeblock)
458
+ params = ImportFormatParams(params)
459
+ filename = codeblock.filename
460
+ transformer = SourceToSourceFileImportsTransformation(codeblock)
461
+ for block in transformer.import_blocks:
462
+ broken = []
463
+ for imp in list(block.importset.imports):
464
+ ns = {}
465
+ try:
466
+ exec(imp.pretty_print(), ns)
467
+ except Exception as e:
468
+ logger.info("%s: Could not import %r; removing it: %s: %s",
469
+ filename, imp.fullname, type(e).__name__, e)
470
+ broken.append(imp)
471
+ block.importset = block.importset.without_imports(broken)
472
+ return transformer.output(params=params)
473
+
474
+
475
+ def replace_star_imports(codeblock, params=None):
476
+ r"""
477
+ Replace lines such as::
478
+
479
+ from foo.bar import *
480
+ with
481
+ from foo.bar import f1, f2, f3
482
+
483
+ Note that this requires involves actually importing ``foo.bar``, which may
484
+ have side effects. (TODO: rewrite to avoid this?)
485
+
486
+ The result includes all imports from the ``email`` module. The result
487
+ excludes shadowed imports. In this example:
488
+
489
+ 1. The original ``MIMEAudio`` import is shadowed, so it is removed.
490
+ 2. The ``MIMEImage`` import in the ``email`` module is shadowed by a
491
+ subsequent import, so it is omitted.
492
+
493
+ >>> codeblock = PythonBlock('from keyword import *', filename="/tmp/x.py")
494
+
495
+ >>> print(replace_star_imports(codeblock)) # doctest: +SKIP
496
+ [PYFLYBY] /tmp/x.py: replaced 'from keyword import *' with 2 imports
497
+ from keyword import iskeyword, kwlist
498
+ <BLANKLINE>
499
+
500
+ Usually you'll want to remove unused imports after replacing star imports.
501
+
502
+ :type codeblock:
503
+ `PythonBlock` or convertible (``str``)
504
+ :rtype:
505
+ `PythonBlock`
506
+ """
507
+ from pyflyby._modules import ModuleHandle
508
+ params = ImportFormatParams(params)
509
+ if not isinstance(codeblock, PythonBlock):
510
+ codeblock = PythonBlock(codeblock)
511
+ filename = codeblock.filename
512
+ transformer = SourceToSourceFileImportsTransformation(codeblock)
513
+ for block in transformer.import_blocks:
514
+ # Iterate over the import statements in ``block.input``. We do this
515
+ # instead of using ``block.importset`` because the latter doesn't
516
+ # preserve the order of inputs. The order is important for
517
+ # determining what's shadowed.
518
+ imports = [
519
+ imp
520
+ for s in block.input.statements
521
+ for imp in ImportStatement(s).imports
522
+ ]
523
+ # Process "from ... import *" statements.
524
+ new_imports = []
525
+ for imp in imports:
526
+ if imp.split.member_name != "*":
527
+ new_imports.append(imp)
528
+ elif imp.split.module_name.startswith("."):
529
+ # The source contains e.g. "from .foo import *". Right now we
530
+ # don't have a good way to figure out the absolute module
531
+ # name, so we can't get at foo. That said, there's a decent
532
+ # chance that this is inside an __init__ anyway, which is one
533
+ # of the few justifiable use cases for star imports in library
534
+ # code.
535
+ logger.warning("%s: can't replace star imports in relative import: %s",
536
+ filename, imp.pretty_print().strip())
537
+ new_imports.append(imp)
538
+ else:
539
+ module = ModuleHandle(imp.split.module_name)
540
+ try:
541
+ with ImportPathForRelativeImportsCtx(codeblock):
542
+ exports = module.exports
543
+ except Exception as e:
544
+ logger.warning(
545
+ "%s: couldn't import '%s' to enumerate exports, "
546
+ "leaving unchanged: '%s'. %s: %s",
547
+ filename, module.name, imp, type(e).__name__, e)
548
+ new_imports.append(imp)
549
+ continue
550
+ if not exports:
551
+ # We found nothing in the target module. This probably
552
+ # means that module itself is just importing things from
553
+ # other modules. Currently we intentionally exclude those
554
+ # imports since usually we don't want them. TODO: do
555
+ # something better here.
556
+ logger.warning("%s: found nothing to import from %s, "
557
+ "leaving unchanged: '%s'",
558
+ filename, module, imp)
559
+ new_imports.append(imp)
560
+ else:
561
+ new_imports.extend(exports)
562
+ logger.info("%s: replaced %r with %d imports", filename,
563
+ imp.pretty_print().strip(), len(exports))
564
+ block.importset = ImportSet(new_imports, ignore_shadowed=True)
565
+ return transformer.output(params=params)
566
+
567
+
568
+ def transform_imports(codeblock, transformations, params=None):
569
+ """
570
+ Transform imports as specified by ``transformations``.
571
+
572
+ transform_imports() perfectly replaces all imports in top-level import
573
+ blocks.
574
+
575
+ For the rest of the code body, transform_imports() does a crude textual
576
+ string replacement. This is imperfect but handles most cases. There may
577
+ be some false positives, but this is difficult to avoid. Generally we do
578
+ want to do replacements even within in strings and comments.
579
+
580
+ >>> result = transform_imports("from m import x", {"m.x": "m.y.z"})
581
+ >>> print(result.text.joined.strip())
582
+ from m.y import z as x
583
+
584
+ :type codeblock:
585
+ `PythonBlock` or convertible (``str``)
586
+ :type transformations:
587
+ ``dict`` from ``str`` to ``str``
588
+ :param transformations:
589
+ A map of import prefixes to replace, e.g. {"aa.bb": "xx.yy"}
590
+ :rtype:
591
+ `PythonBlock`
592
+ """
593
+ if not isinstance(codeblock, PythonBlock):
594
+ codeblock = PythonBlock(codeblock)
595
+ params = ImportFormatParams(params)
596
+ transformer = SourceToSourceFileImportsTransformation(codeblock)
597
+ @memoize
598
+ def transform_import(imp):
599
+ # Transform a block of imports.
600
+ # TODO: optimize
601
+ # TODO: handle transformations containing both a.b=>x and a.b.c=>y
602
+ for k, v in transformations.items():
603
+ imp = imp.replace(k, v)
604
+ return imp
605
+ def transform_block(block):
606
+ # Do a crude string replacement in the PythonBlock.
607
+ if not isinstance(block, PythonBlock):
608
+ block = PythonBlock(block)
609
+ s = block.text.joined
610
+ for k, v in transformations.items():
611
+ s = re.sub("\\b%s\\b" % (re.escape(k)), v, s)
612
+ return PythonBlock(s, flags=block.flags)
613
+ # Loop over transformer blocks.
614
+ for block in transformer.blocks:
615
+ if isinstance(block, SourceToSourceImportBlockTransformation):
616
+ input_imports = block.importset.imports
617
+ output_imports = [ transform_import(imp) for imp in input_imports ]
618
+ block.importset = ImportSet(output_imports, ignore_shadowed=True)
619
+ else:
620
+ block._output = transform_block(block.input)
621
+ return transformer.output(params=params)
622
+
623
+
624
+ def canonicalize_imports(codeblock, params=None, db=None):
625
+ """
626
+ Transform ``codeblock`` as specified by ``__canonical_imports__`` in the
627
+ global import library.
628
+
629
+ :type codeblock:
630
+ `PythonBlock` or convertible (``str``)
631
+ :rtype:
632
+ `PythonBlock`
633
+ """
634
+ if not isinstance(codeblock, PythonBlock):
635
+ codeblock = PythonBlock(codeblock)
636
+ params = ImportFormatParams(params)
637
+ db = ImportDB.interpret_arg(db, target_filename=codeblock.filename)
638
+ transformations = db.canonical_imports
639
+ return transform_imports(codeblock, transformations, params=params)