1 | n/a | """Fix changes imports of urllib which are now incompatible. |
---|
2 | n/a | This is rather similar to fix_imports, but because of the more |
---|
3 | n/a | complex nature of the fixing for urllib, it has its own fixer. |
---|
4 | n/a | """ |
---|
5 | n/a | # Author: Nick Edds |
---|
6 | n/a | |
---|
7 | n/a | # Local imports |
---|
8 | n/a | from lib2to3.fixes.fix_imports import alternates, FixImports |
---|
9 | n/a | from lib2to3.fixer_util import (Name, Comma, FromImport, Newline, |
---|
10 | n/a | find_indentation, Node, syms) |
---|
11 | n/a | |
---|
12 | n/a | MAPPING = {"urllib": [ |
---|
13 | n/a | ("urllib.request", |
---|
14 | n/a | ["URLopener", "FancyURLopener", "urlretrieve", |
---|
15 | n/a | "_urlopener", "urlopen", "urlcleanup", |
---|
16 | n/a | "pathname2url", "url2pathname"]), |
---|
17 | n/a | ("urllib.parse", |
---|
18 | n/a | ["quote", "quote_plus", "unquote", "unquote_plus", |
---|
19 | n/a | "urlencode", "splitattr", "splithost", "splitnport", |
---|
20 | n/a | "splitpasswd", "splitport", "splitquery", "splittag", |
---|
21 | n/a | "splittype", "splituser", "splitvalue", ]), |
---|
22 | n/a | ("urllib.error", |
---|
23 | n/a | ["ContentTooShortError"])], |
---|
24 | n/a | "urllib2" : [ |
---|
25 | n/a | ("urllib.request", |
---|
26 | n/a | ["urlopen", "install_opener", "build_opener", |
---|
27 | n/a | "Request", "OpenerDirector", "BaseHandler", |
---|
28 | n/a | "HTTPDefaultErrorHandler", "HTTPRedirectHandler", |
---|
29 | n/a | "HTTPCookieProcessor", "ProxyHandler", |
---|
30 | n/a | "HTTPPasswordMgr", |
---|
31 | n/a | "HTTPPasswordMgrWithDefaultRealm", |
---|
32 | n/a | "AbstractBasicAuthHandler", |
---|
33 | n/a | "HTTPBasicAuthHandler", "ProxyBasicAuthHandler", |
---|
34 | n/a | "AbstractDigestAuthHandler", |
---|
35 | n/a | "HTTPDigestAuthHandler", "ProxyDigestAuthHandler", |
---|
36 | n/a | "HTTPHandler", "HTTPSHandler", "FileHandler", |
---|
37 | n/a | "FTPHandler", "CacheFTPHandler", |
---|
38 | n/a | "UnknownHandler"]), |
---|
39 | n/a | ("urllib.error", |
---|
40 | n/a | ["URLError", "HTTPError"]), |
---|
41 | n/a | ] |
---|
42 | n/a | } |
---|
43 | n/a | |
---|
44 | n/a | # Duplicate the url parsing functions for urllib2. |
---|
45 | n/a | MAPPING["urllib2"].append(MAPPING["urllib"][1]) |
---|
46 | n/a | |
---|
47 | n/a | |
---|
48 | n/a | def build_pattern(): |
---|
49 | n/a | bare = set() |
---|
50 | n/a | for old_module, changes in MAPPING.items(): |
---|
51 | n/a | for change in changes: |
---|
52 | n/a | new_module, members = change |
---|
53 | n/a | members = alternates(members) |
---|
54 | n/a | yield """import_name< 'import' (module=%r |
---|
55 | n/a | | dotted_as_names< any* module=%r any* >) > |
---|
56 | n/a | """ % (old_module, old_module) |
---|
57 | n/a | yield """import_from< 'from' mod_member=%r 'import' |
---|
58 | n/a | ( member=%s | import_as_name< member=%s 'as' any > | |
---|
59 | n/a | import_as_names< members=any* >) > |
---|
60 | n/a | """ % (old_module, members, members) |
---|
61 | n/a | yield """import_from< 'from' module_star=%r 'import' star='*' > |
---|
62 | n/a | """ % old_module |
---|
63 | n/a | yield """import_name< 'import' |
---|
64 | n/a | dotted_as_name< module_as=%r 'as' any > > |
---|
65 | n/a | """ % old_module |
---|
66 | n/a | # bare_with_attr has a special significance for FixImports.match(). |
---|
67 | n/a | yield """power< bare_with_attr=%r trailer< '.' member=%s > any* > |
---|
68 | n/a | """ % (old_module, members) |
---|
69 | n/a | |
---|
70 | n/a | |
---|
71 | n/a | class FixUrllib(FixImports): |
---|
72 | n/a | |
---|
73 | n/a | def build_pattern(self): |
---|
74 | n/a | return "|".join(build_pattern()) |
---|
75 | n/a | |
---|
76 | n/a | def transform_import(self, node, results): |
---|
77 | n/a | """Transform for the basic import case. Replaces the old |
---|
78 | n/a | import name with a comma separated list of its |
---|
79 | n/a | replacements. |
---|
80 | n/a | """ |
---|
81 | n/a | import_mod = results.get("module") |
---|
82 | n/a | pref = import_mod.prefix |
---|
83 | n/a | |
---|
84 | n/a | names = [] |
---|
85 | n/a | |
---|
86 | n/a | # create a Node list of the replacement modules |
---|
87 | n/a | for name in MAPPING[import_mod.value][:-1]: |
---|
88 | n/a | names.extend([Name(name[0], prefix=pref), Comma()]) |
---|
89 | n/a | names.append(Name(MAPPING[import_mod.value][-1][0], prefix=pref)) |
---|
90 | n/a | import_mod.replace(names) |
---|
91 | n/a | |
---|
92 | n/a | def transform_member(self, node, results): |
---|
93 | n/a | """Transform for imports of specific module elements. Replaces |
---|
94 | n/a | the module to be imported from with the appropriate new |
---|
95 | n/a | module. |
---|
96 | n/a | """ |
---|
97 | n/a | mod_member = results.get("mod_member") |
---|
98 | n/a | pref = mod_member.prefix |
---|
99 | n/a | member = results.get("member") |
---|
100 | n/a | |
---|
101 | n/a | # Simple case with only a single member being imported |
---|
102 | n/a | if member: |
---|
103 | n/a | # this may be a list of length one, or just a node |
---|
104 | n/a | if isinstance(member, list): |
---|
105 | n/a | member = member[0] |
---|
106 | n/a | new_name = None |
---|
107 | n/a | for change in MAPPING[mod_member.value]: |
---|
108 | n/a | if member.value in change[1]: |
---|
109 | n/a | new_name = change[0] |
---|
110 | n/a | break |
---|
111 | n/a | if new_name: |
---|
112 | n/a | mod_member.replace(Name(new_name, prefix=pref)) |
---|
113 | n/a | else: |
---|
114 | n/a | self.cannot_convert(node, "This is an invalid module element") |
---|
115 | n/a | |
---|
116 | n/a | # Multiple members being imported |
---|
117 | n/a | else: |
---|
118 | n/a | # a dictionary for replacements, order matters |
---|
119 | n/a | modules = [] |
---|
120 | n/a | mod_dict = {} |
---|
121 | n/a | members = results["members"] |
---|
122 | n/a | for member in members: |
---|
123 | n/a | # we only care about the actual members |
---|
124 | n/a | if member.type == syms.import_as_name: |
---|
125 | n/a | as_name = member.children[2].value |
---|
126 | n/a | member_name = member.children[0].value |
---|
127 | n/a | else: |
---|
128 | n/a | member_name = member.value |
---|
129 | n/a | as_name = None |
---|
130 | n/a | if member_name != ",": |
---|
131 | n/a | for change in MAPPING[mod_member.value]: |
---|
132 | n/a | if member_name in change[1]: |
---|
133 | n/a | if change[0] not in mod_dict: |
---|
134 | n/a | modules.append(change[0]) |
---|
135 | n/a | mod_dict.setdefault(change[0], []).append(member) |
---|
136 | n/a | |
---|
137 | n/a | new_nodes = [] |
---|
138 | n/a | indentation = find_indentation(node) |
---|
139 | n/a | first = True |
---|
140 | n/a | def handle_name(name, prefix): |
---|
141 | n/a | if name.type == syms.import_as_name: |
---|
142 | n/a | kids = [Name(name.children[0].value, prefix=prefix), |
---|
143 | n/a | name.children[1].clone(), |
---|
144 | n/a | name.children[2].clone()] |
---|
145 | n/a | return [Node(syms.import_as_name, kids)] |
---|
146 | n/a | return [Name(name.value, prefix=prefix)] |
---|
147 | n/a | for module in modules: |
---|
148 | n/a | elts = mod_dict[module] |
---|
149 | n/a | names = [] |
---|
150 | n/a | for elt in elts[:-1]: |
---|
151 | n/a | names.extend(handle_name(elt, pref)) |
---|
152 | n/a | names.append(Comma()) |
---|
153 | n/a | names.extend(handle_name(elts[-1], pref)) |
---|
154 | n/a | new = FromImport(module, names) |
---|
155 | n/a | if not first or node.parent.prefix.endswith(indentation): |
---|
156 | n/a | new.prefix = indentation |
---|
157 | n/a | new_nodes.append(new) |
---|
158 | n/a | first = False |
---|
159 | n/a | if new_nodes: |
---|
160 | n/a | nodes = [] |
---|
161 | n/a | for new_node in new_nodes[:-1]: |
---|
162 | n/a | nodes.extend([new_node, Newline()]) |
---|
163 | n/a | nodes.append(new_nodes[-1]) |
---|
164 | n/a | node.replace(nodes) |
---|
165 | n/a | else: |
---|
166 | n/a | self.cannot_convert(node, "All module elements are invalid") |
---|
167 | n/a | |
---|
168 | n/a | def transform_dot(self, node, results): |
---|
169 | n/a | """Transform for calls to module members in code.""" |
---|
170 | n/a | module_dot = results.get("bare_with_attr") |
---|
171 | n/a | member = results.get("member") |
---|
172 | n/a | new_name = None |
---|
173 | n/a | if isinstance(member, list): |
---|
174 | n/a | member = member[0] |
---|
175 | n/a | for change in MAPPING[module_dot.value]: |
---|
176 | n/a | if member.value in change[1]: |
---|
177 | n/a | new_name = change[0] |
---|
178 | n/a | break |
---|
179 | n/a | if new_name: |
---|
180 | n/a | module_dot.replace(Name(new_name, |
---|
181 | n/a | prefix=module_dot.prefix)) |
---|
182 | n/a | else: |
---|
183 | n/a | self.cannot_convert(node, "This is an invalid module element") |
---|
184 | n/a | |
---|
185 | n/a | def transform(self, node, results): |
---|
186 | n/a | if results.get("module"): |
---|
187 | n/a | self.transform_import(node, results) |
---|
188 | n/a | elif results.get("mod_member"): |
---|
189 | n/a | self.transform_member(node, results) |
---|
190 | n/a | elif results.get("bare_with_attr"): |
---|
191 | n/a | self.transform_dot(node, results) |
---|
192 | n/a | # Renaming and star imports are not supported for these modules. |
---|
193 | n/a | elif results.get("module_star"): |
---|
194 | n/a | self.cannot_convert(node, "Cannot handle star imports.") |
---|
195 | n/a | elif results.get("module_as"): |
---|
196 | n/a | self.cannot_convert(node, "This module is now multiple modules") |
---|