"""Fix incompatible imports and module references. Fixes: * StringIO -> io * cStringIO -> io * md5 -> hashlib """ # Author: Collin Winter # Local imports from .. import fixer_base from ..fixer_util import Name, attr_chain, any, set # XXX(alexandre): It would be possible to get the modules exports by fetching # XXX: their __all__ attribute. However, I fear that this would add an additional # XXX: overhead to the fixer. MAPPING = {"StringIO": "io", "cStringIO": "io", "__builtin__" : "builtins", 'copy_reg': 'copyreg', 'Queue': 'queue', 'SocketServer': 'socketserver', 'ConfigParser': 'configparser', 'repr': 'reprlib', 'FileDialog': 'tkinter.filedialog', 'tkFileDialog': 'tkinter.filedialog', 'SimpleDialog': 'tkinter.simpledialog', 'tkSimpleDialog': 'tkinter.simpledialog', 'tkColorChooser': 'tkinter.colorchooser', 'tkCommonDialog': 'tkinter.commondialog', 'Dialog': 'tkinter.dialog', 'Tkdnd': 'tkinter.dnd', 'tkFont': 'tkinter.font', 'tkMessageBox': 'tkinter.messagebox', 'ScrolledText': 'tkinter.scrolledtext', 'turtle': 'tkinter.turtle', 'Tkconstants': 'tkinter.constants', 'Tix': 'tkinter.tix', 'Tkinter': 'tkinter', 'markupbase': '_markupbase', '_winreg': 'winreg', 'thread': '_thread', 'dummy_thread': '_dummy_thread', 'whichdb': 'dbm', 'anydbm': 'dbm', 'dbhash': 'dbm.bsd', 'dumbdbm': 'dbm.dumb', 'dbm': 'dbm.ndbm', 'gdbm': 'dbm.gnu', 'xmlrpclib': 'xmlrpc.client', 'DocXMLRPCServer': 'xmlrpc.server', 'SimpleXMLRPCServer': 'xmlrpc.server', 'httplib': 'http.client', 'Cookie': 'http.cookies', 'cookielib': 'http.cookiejar', 'BaseHTTPServer': 'http.server', 'SimpleHTTPServer': 'http.server', 'CGIHTTPServer': 'http.server', 'test.test_support': 'test.support', 'commands': 'subprocess', 'UserString' : 'collections', 'UserList' : 'collections', } def alternates(members): return "(" + "|".join(map(repr, members)) + ")" def build_pattern(): bare = set() for old_module, new_module in MAPPING.items(): bare.add(old_module) yield """import_name< 'import' (module=%r | dotted_as_names< any* module=%r any* >) > """ % (old_module, old_module) yield """import_from< 'from' module_name=%r 'import' star='*' > """ % old_module yield """import_name< 'import' dotted_as_name< module_name=%r 'as' any > > """ % old_module yield """bare_name=%s""" % alternates(bare) class FixImports(fixer_base.BaseFix): PATTERN = "|".join(build_pattern()) order = "pre" # Pre-order tree traversal # Don't match the node if it's within another match def match(self, node): match = super(FixImports, self).match results = match(node) if results: if any([match(obj) for obj in attr_chain(node, "parent")]): return False return results return False def start_tree(self, tree, filename): super(FixImports, self).start_tree(tree, filename) self.replace = {} def transform(self, node, results): import_mod = results.get("module") mod_name = results.get("module_name") bare_name = results.get("bare_name") star = results.get("star") if import_mod or mod_name: new_name = MAPPING[(import_mod or mod_name).value] if import_mod: self.replace[import_mod.value] = new_name import_mod.replace(Name(new_name, prefix=import_mod.get_prefix())) elif mod_name: if star: self.cannot_convert(node, "Cannot handle star imports.") else: mod_name.replace(Name(new_name, prefix=mod_name.get_prefix())) elif bare_name: bare_name = bare_name[0] new_name = self.replace.get(bare_name.value) if new_name: bare_name.replace(Name(new_name, prefix=bare_name.get_prefix()))