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