| 1 | n/a | """Fixer for import statements. |
|---|
| 2 | n/a | If spam is being imported from the local directory, this import: |
|---|
| 3 | n/a | from spam import eggs |
|---|
| 4 | n/a | Becomes: |
|---|
| 5 | n/a | from .spam import eggs |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | And this import: |
|---|
| 8 | n/a | import spam |
|---|
| 9 | n/a | Becomes: |
|---|
| 10 | n/a | from . import spam |
|---|
| 11 | n/a | """ |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | # Local imports |
|---|
| 14 | n/a | from .. import fixer_base |
|---|
| 15 | n/a | from os.path import dirname, join, exists, sep |
|---|
| 16 | n/a | from ..fixer_util import FromImport, syms, token |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | def traverse_imports(names): |
|---|
| 20 | n/a | """ |
|---|
| 21 | n/a | Walks over all the names imported in a dotted_as_names node. |
|---|
| 22 | n/a | """ |
|---|
| 23 | n/a | pending = [names] |
|---|
| 24 | n/a | while pending: |
|---|
| 25 | n/a | node = pending.pop() |
|---|
| 26 | n/a | if node.type == token.NAME: |
|---|
| 27 | n/a | yield node.value |
|---|
| 28 | n/a | elif node.type == syms.dotted_name: |
|---|
| 29 | n/a | yield "".join([ch.value for ch in node.children]) |
|---|
| 30 | n/a | elif node.type == syms.dotted_as_name: |
|---|
| 31 | n/a | pending.append(node.children[0]) |
|---|
| 32 | n/a | elif node.type == syms.dotted_as_names: |
|---|
| 33 | n/a | pending.extend(node.children[::-2]) |
|---|
| 34 | n/a | else: |
|---|
| 35 | n/a | raise AssertionError("unknown node type") |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | class FixImport(fixer_base.BaseFix): |
|---|
| 39 | n/a | BM_compatible = True |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | PATTERN = """ |
|---|
| 42 | n/a | import_from< 'from' imp=any 'import' ['('] any [')'] > |
|---|
| 43 | n/a | | |
|---|
| 44 | n/a | import_name< 'import' imp=any > |
|---|
| 45 | n/a | """ |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | def start_tree(self, tree, name): |
|---|
| 48 | n/a | super(FixImport, self).start_tree(tree, name) |
|---|
| 49 | n/a | self.skip = "absolute_import" in tree.future_features |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | def transform(self, node, results): |
|---|
| 52 | n/a | if self.skip: |
|---|
| 53 | n/a | return |
|---|
| 54 | n/a | imp = results['imp'] |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | if node.type == syms.import_from: |
|---|
| 57 | n/a | # Some imps are top-level (eg: 'import ham') |
|---|
| 58 | n/a | # some are first level (eg: 'import ham.eggs') |
|---|
| 59 | n/a | # some are third level (eg: 'import ham.eggs as spam') |
|---|
| 60 | n/a | # Hence, the loop |
|---|
| 61 | n/a | while not hasattr(imp, 'value'): |
|---|
| 62 | n/a | imp = imp.children[0] |
|---|
| 63 | n/a | if self.probably_a_local_import(imp.value): |
|---|
| 64 | n/a | imp.value = "." + imp.value |
|---|
| 65 | n/a | imp.changed() |
|---|
| 66 | n/a | else: |
|---|
| 67 | n/a | have_local = False |
|---|
| 68 | n/a | have_absolute = False |
|---|
| 69 | n/a | for mod_name in traverse_imports(imp): |
|---|
| 70 | n/a | if self.probably_a_local_import(mod_name): |
|---|
| 71 | n/a | have_local = True |
|---|
| 72 | n/a | else: |
|---|
| 73 | n/a | have_absolute = True |
|---|
| 74 | n/a | if have_absolute: |
|---|
| 75 | n/a | if have_local: |
|---|
| 76 | n/a | # We won't handle both sibling and absolute imports in the |
|---|
| 77 | n/a | # same statement at the moment. |
|---|
| 78 | n/a | self.warning(node, "absolute and local imports together") |
|---|
| 79 | n/a | return |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | new = FromImport(".", [imp]) |
|---|
| 82 | n/a | new.prefix = node.prefix |
|---|
| 83 | n/a | return new |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | def probably_a_local_import(self, imp_name): |
|---|
| 86 | n/a | if imp_name.startswith("."): |
|---|
| 87 | n/a | # Relative imports are certainly not local imports. |
|---|
| 88 | n/a | return False |
|---|
| 89 | n/a | imp_name = imp_name.split(".", 1)[0] |
|---|
| 90 | n/a | base_path = dirname(self.filename) |
|---|
| 91 | n/a | base_path = join(base_path, imp_name) |
|---|
| 92 | n/a | # If there is no __init__.py next to the file its not in a package |
|---|
| 93 | n/a | # so can't be a relative import. |
|---|
| 94 | n/a | if not exists(join(dirname(base_path), "__init__.py")): |
|---|
| 95 | n/a | return False |
|---|
| 96 | n/a | for ext in [".py", sep, ".pyc", ".so", ".sl", ".pyd"]: |
|---|
| 97 | n/a | if exists(base_path + ext): |
|---|
| 98 | n/a | return True |
|---|
| 99 | n/a | return False |
|---|