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