|
9 | 9 |
|
10 | 10 | after any __future__ imports but before any other imports. |
11 | 11 | """ |
12 | | -from __future__ import absolute_import, unicode_literals |
13 | | - |
14 | | -from lib2to3.fixes.fix_imports import FixImports, MAPPING |
15 | | -from lib2to3.pgen2 import token |
16 | | -from lib2to3.pytree import Leaf, Node |
17 | | -from lib2to3.pygram import python_symbols as syms |
18 | | -from lib2to3.fixer_util import Name |
19 | | -from lib2to3 import patcomp |
20 | 12 |
|
| 13 | +from lib2to3.fixes.fix_imports import FixImports |
21 | 14 | from libfuturize.fixer_util import touch_import_top |
22 | | -from future.builtins import str |
23 | | - |
24 | | -BACKPORTS = set(['http', 'xmlrpc', 'email', 'urllib', 'html']) |
25 | | - |
26 | | -future_mapping = {} |
27 | | - |
28 | | - |
29 | | -# These modules exist on Py2 and Py2.7 so they needn't be replaced by |
30 | | -# future.standard_library.io etc.: |
31 | | -IN_PY2 = ['io', 'pickle', 'collections', 'subprocess'] |
32 | | - |
33 | | - |
34 | | -for (old, new) in MAPPING.items(): |
35 | | - # if new in IN_PY2: |
36 | | - # continue |
37 | | - if any([new.startswith(toplevel) for toplevel in BACKPORTS]): |
38 | | - # Change e.g. urllib.request to urllib_request |
39 | | - # if '.' in new: |
40 | | - # new.replace('.', '_') |
41 | | - future_mapping[old] = ('future.standard_library.' + new, |
42 | | - new.replace('.', '_')) |
43 | | - else: |
44 | | - future_mapping[old] = (new,) |
45 | 15 |
|
46 | 16 |
|
47 | 17 | class FixFutureStandardLibrary(FixImports): |
48 | 18 | run_order = 8 |
49 | | - mapping = future_mapping |
50 | 19 |
|
51 | 20 | def transform(self, node, results): |
52 | | - import_mod = results.get("module_name") |
53 | | - if import_mod: |
54 | | - mod_name = import_mod.value |
55 | | - if len(self.mapping[mod_name]) > 1: |
56 | | - new_name1, new_name2 = map(str, self.mapping[mod_name]) |
57 | | - # import_mod.replace(Name(new_name, prefix=import_mod.prefix)) |
58 | | - children = [Leaf(token.NAME, new_name1, prefix=u" "), |
59 | | - Leaf(token.NAME, u"as", prefix=u" "), |
60 | | - Leaf(token.NAME, new_name2, prefix=u" ")] |
61 | | - imp = Node(syms.dotted_as_name, children) |
62 | | - else: |
63 | | - new_name = self.mapping[mod_name][0] |
64 | | - imp = Name(new_name, prefix=import_mod.prefix) |
65 | | - new_name2 = new_name |
66 | | - |
67 | | - import_mod.replace(imp) |
68 | | - |
69 | | - if "name_import" in results: |
70 | | - # If it's not a "from x import x, y" or "import x as y" import, |
71 | | - # marked its usage to be replaced. |
72 | | - # TODO: fix this so that each module is imported only once. |
73 | | - self.replace[mod_name] = new_name2 |
74 | | - if "multiple_imports" in results: |
75 | | - # This is a nasty hack to fix multiple imports on a line (e.g., |
76 | | - # "import StringIO, urlparse"). The problem is that I can't |
77 | | - # figure out an easy way to make a pattern recognize the keys of |
78 | | - # MAPPING randomly sprinkled in an import statement. |
79 | | - results = self.match(node) |
80 | | - if results: |
81 | | - self.transform(node, results) |
82 | | - else: |
83 | | - # Replace usage of the module. |
84 | | - bare_name = results["bare_with_attr"][0] |
85 | | - new_name = self.replace.get(bare_name.value) |
86 | | - if new_name: |
87 | | - bare_name.replace(Name(new_name, prefix=bare_name.prefix)) |
| 21 | + result = super(FixFutureStandardLibrary, self).transform(node, results) |
| 22 | + # TODO: add a blank line between any __future__ imports and this? |
88 | 23 | touch_import_top(u'future', u'standard_library', node) |
89 | | - |
90 | | - # def transform(self, node, results): |
91 | | - # result = super(FixFutureStandardLibrary, self).transform(node, results) |
92 | | - # touch_import_top(u'future', u'standard_library', node) |
93 | | - # return result |
| 24 | + return result |
94 | 25 |
|
95 | 26 |
|
0 commit comments