fix_imports.py 5.6 KB

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