+++ /dev/null
-"""Fix incompatible imports and module references."""\r
-# Authors: Collin Winter, Nick Edds\r
-\r
-# Local imports\r
-from .. import fixer_base\r
-from ..fixer_util import Name, attr_chain\r
-\r
-MAPPING = {'StringIO': 'io',\r
- 'cStringIO': 'io',\r
- 'cPickle': 'pickle',\r
- '__builtin__' : 'builtins',\r
- 'copy_reg': 'copyreg',\r
- 'Queue': 'queue',\r
- 'SocketServer': 'socketserver',\r
- 'ConfigParser': 'configparser',\r
- 'repr': 'reprlib',\r
- 'FileDialog': 'tkinter.filedialog',\r
- 'tkFileDialog': 'tkinter.filedialog',\r
- 'SimpleDialog': 'tkinter.simpledialog',\r
- 'tkSimpleDialog': 'tkinter.simpledialog',\r
- 'tkColorChooser': 'tkinter.colorchooser',\r
- 'tkCommonDialog': 'tkinter.commondialog',\r
- 'Dialog': 'tkinter.dialog',\r
- 'Tkdnd': 'tkinter.dnd',\r
- 'tkFont': 'tkinter.font',\r
- 'tkMessageBox': 'tkinter.messagebox',\r
- 'ScrolledText': 'tkinter.scrolledtext',\r
- 'Tkconstants': 'tkinter.constants',\r
- 'Tix': 'tkinter.tix',\r
- 'ttk': 'tkinter.ttk',\r
- 'Tkinter': 'tkinter',\r
- 'markupbase': '_markupbase',\r
- '_winreg': 'winreg',\r
- 'thread': '_thread',\r
- 'dummy_thread': '_dummy_thread',\r
- # anydbm and whichdb are handled by fix_imports2\r
- 'dbhash': 'dbm.bsd',\r
- 'dumbdbm': 'dbm.dumb',\r
- 'dbm': 'dbm.ndbm',\r
- 'gdbm': 'dbm.gnu',\r
- 'xmlrpclib': 'xmlrpc.client',\r
- 'DocXMLRPCServer': 'xmlrpc.server',\r
- 'SimpleXMLRPCServer': 'xmlrpc.server',\r
- 'httplib': 'http.client',\r
- 'htmlentitydefs' : 'html.entities',\r
- 'HTMLParser' : 'html.parser',\r
- 'Cookie': 'http.cookies',\r
- 'cookielib': 'http.cookiejar',\r
- 'BaseHTTPServer': 'http.server',\r
- 'SimpleHTTPServer': 'http.server',\r
- 'CGIHTTPServer': 'http.server',\r
- #'test.test_support': 'test.support',\r
- 'commands': 'subprocess',\r
- 'UserString' : 'collections',\r
- 'UserList' : 'collections',\r
- 'urlparse' : 'urllib.parse',\r
- 'robotparser' : 'urllib.robotparser',\r
-}\r
-\r
-\r
-def alternates(members):\r
- return "(" + "|".join(map(repr, members)) + ")"\r
-\r
-\r
-def build_pattern(mapping=MAPPING):\r
- mod_list = ' | '.join(["module_name='%s'" % key for key in mapping])\r
- bare_names = alternates(mapping.keys())\r
-\r
- yield """name_import=import_name< 'import' ((%s) |\r
- multiple_imports=dotted_as_names< any* (%s) any* >) >\r
- """ % (mod_list, mod_list)\r
- yield """import_from< 'from' (%s) 'import' ['(']\r
- ( any | import_as_name< any 'as' any > |\r
- import_as_names< any* >) [')'] >\r
- """ % mod_list\r
- yield """import_name< 'import' (dotted_as_name< (%s) 'as' any > |\r
- multiple_imports=dotted_as_names<\r
- any* dotted_as_name< (%s) 'as' any > any* >) >\r
- """ % (mod_list, mod_list)\r
-\r
- # Find usages of module members in code e.g. thread.foo(bar)\r
- yield "power< bare_with_attr=(%s) trailer<'.' any > any* >" % bare_names\r
-\r
-\r
-class FixImports(fixer_base.BaseFix):\r
-\r
- BM_compatible = True\r
- keep_line_order = True\r
- # This is overridden in fix_imports2.\r
- mapping = MAPPING\r
-\r
- # We want to run this fixer late, so fix_import doesn't try to make stdlib\r
- # renames into relative imports.\r
- run_order = 6\r
-\r
- def build_pattern(self):\r
- return "|".join(build_pattern(self.mapping))\r
-\r
- def compile_pattern(self):\r
- # We override this, so MAPPING can be pragmatically altered and the\r
- # changes will be reflected in PATTERN.\r
- self.PATTERN = self.build_pattern()\r
- super(FixImports, self).compile_pattern()\r
-\r
- # Don't match the node if it's within another match.\r
- def match(self, node):\r
- match = super(FixImports, self).match\r
- results = match(node)\r
- if results:\r
- # Module usage could be in the trailer of an attribute lookup, so we\r
- # might have nested matches when "bare_with_attr" is present.\r
- if "bare_with_attr" not in results and \\r
- any(match(obj) for obj in attr_chain(node, "parent")):\r
- return False\r
- return results\r
- return False\r
-\r
- def start_tree(self, tree, filename):\r
- super(FixImports, self).start_tree(tree, filename)\r
- self.replace = {}\r
-\r
- def transform(self, node, results):\r
- import_mod = results.get("module_name")\r
- if import_mod:\r
- mod_name = import_mod.value\r
- new_name = unicode(self.mapping[mod_name])\r
- import_mod.replace(Name(new_name, prefix=import_mod.prefix))\r
- if "name_import" in results:\r
- # If it's not a "from x import x, y" or "import x as y" import,\r
- # marked its usage to be replaced.\r
- self.replace[mod_name] = new_name\r
- if "multiple_imports" in results:\r
- # This is a nasty hack to fix multiple imports on a line (e.g.,\r
- # "import StringIO, urlparse"). The problem is that I can't\r
- # figure out an easy way to make a pattern recognize the keys of\r
- # MAPPING randomly sprinkled in an import statement.\r
- results = self.match(node)\r
- if results:\r
- self.transform(node, results)\r
- else:\r
- # Replace usage of the module.\r
- bare_name = results["bare_with_attr"][0]\r
- new_name = self.replace.get(bare_name.value)\r
- if new_name:\r
- bare_name.replace(Name(new_name, prefix=bare_name.prefix))\r