1 """Fix incompatible imports and module references."""
2 # Authors: Collin Winter, Nick Edds
5 from .. import fixer_base
6 from ..fixer_util
import Name
, attr_chain
8 MAPPING
= {'StringIO': 'io',
11 '__builtin__' : 'builtins',
12 'copy_reg': 'copyreg',
14 'SocketServer': 'socketserver',
15 'ConfigParser': 'configparser',
17 'FileDialog': 'tkinter.filedialog',
18 'tkFileDialog': 'tkinter.filedialog',
19 'SimpleDialog': 'tkinter.simpledialog',
20 'tkSimpleDialog': 'tkinter.simpledialog',
21 'tkColorChooser': 'tkinter.colorchooser',
22 'tkCommonDialog': 'tkinter.commondialog',
23 'Dialog': 'tkinter.dialog',
24 'Tkdnd': 'tkinter.dnd',
25 'tkFont': 'tkinter.font',
26 'tkMessageBox': 'tkinter.messagebox',
27 'ScrolledText': 'tkinter.scrolledtext',
28 'Tkconstants': 'tkinter.constants',
32 'markupbase': '_markupbase',
35 'dummy_thread': '_dummy_thread',
36 # anydbm and whichdb are handled by fix_imports2
38 'dumbdbm': 'dbm.dumb',
41 'xmlrpclib': 'xmlrpc.client',
42 'DocXMLRPCServer': 'xmlrpc.server',
43 'SimpleXMLRPCServer': 'xmlrpc.server',
44 'httplib': 'http.client',
45 'htmlentitydefs' : 'html.entities',
46 'HTMLParser' : 'html.parser',
47 'Cookie': 'http.cookies',
48 'cookielib': 'http.cookiejar',
49 'BaseHTTPServer': 'http.server',
50 'SimpleHTTPServer': 'http.server',
51 'CGIHTTPServer': 'http.server',
52 #'test.test_support': 'test.support',
53 'commands': 'subprocess',
54 'UserString' : 'collections',
55 'UserList' : 'collections',
56 'urlparse' : 'urllib.parse',
57 'robotparser' : 'urllib.robotparser',
61 def alternates(members
):
62 return "(" + "|".join(map(repr, members
)) + ")"
65 def build_pattern(mapping
=MAPPING
):
66 mod_list
= ' | '.join(["module_name='%s'" % key
for key
in mapping
])
67 bare_names
= alternates(mapping
.keys())
69 yield """name_import=import_name< 'import' ((%s) |
70 multiple_imports=dotted_as_names< any* (%s) any* >) >
71 """ % (mod_list
, mod_list
)
72 yield """import_from< 'from' (%s) 'import' ['(']
73 ( any | import_as_name< any 'as' any > |
74 import_as_names< any* >) [')'] >
76 yield """import_name< 'import' (dotted_as_name< (%s) 'as' any > |
77 multiple_imports=dotted_as_names<
78 any* dotted_as_name< (%s) 'as' any > any* >) >
79 """ % (mod_list
, mod_list
)
81 # Find usages of module members in code e.g. thread.foo(bar)
82 yield "power< bare_with_attr=(%s) trailer<'.' any > any* >" % bare_names
85 class FixImports(fixer_base
.BaseFix
):
87 # This is overridden in fix_imports2.
90 # We want to run this fixer late, so fix_import doesn't try to make stdlib
91 # renames into relative imports.
94 def build_pattern(self
):
95 return "|".join(build_pattern(self
.mapping
))
97 def compile_pattern(self
):
98 # We override this, so MAPPING can be pragmatically altered and the
99 # changes will be reflected in PATTERN.
100 self
.PATTERN
= self
.build_pattern()
101 super(FixImports
, self
).compile_pattern()
103 # Don't match the node if it's within another match.
104 def match(self
, node
):
105 match
= super(FixImports
, self
).match
106 results
= match(node
)
108 # Module usage could be in the trailer of an attribute lookup, so we
109 # might have nested matches when "bare_with_attr" is present.
110 if "bare_with_attr" not in results
and \
111 any(match(obj
) for obj
in attr_chain(node
, "parent")):
116 def start_tree(self
, tree
, filename
):
117 super(FixImports
, self
).start_tree(tree
, filename
)
120 def transform(self
, node
, results
):
121 import_mod
= results
.get("module_name")
123 mod_name
= import_mod
.value
124 new_name
= self
.mapping
[mod_name
]
125 import_mod
.replace(Name(new_name
, prefix
=import_mod
.prefix
))
126 if "name_import" in results
:
127 # If it's not a "from x import x, y" or "import x as y" import,
128 # marked its usage to be replaced.
129 self
.replace
[mod_name
] = new_name
130 if "multiple_imports" in results
:
131 # This is a nasty hack to fix multiple imports on a line (e.g.,
132 # "import StringIO, urlparse"). The problem is that I can't
133 # figure out an easy way to make a pattern recognize the keys of
134 # MAPPING randomly sprinkled in an import statement.
135 results
= self
.match(node
)
137 self
.transform(node
, results
)
139 # Replace usage of the module.
140 bare_name
= results
["bare_with_attr"][0]
141 new_name
= self
.replace
.get(bare_name
.value
)
143 bare_name
.replace(Name(new_name
, prefix
=bare_name
.prefix
))