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