OLD | NEW |
(Empty) | |
| 1 """Fix UserDict. |
| 2 |
| 3 Incomplete! |
| 4 |
| 5 TODO: base this on fix_urllib perhaps? |
| 6 """ |
| 7 |
| 8 |
| 9 # Local imports |
| 10 from lib2to3 import fixer_base |
| 11 from lib2to3.fixer_util import Name, attr_chain |
| 12 from lib2to3.fixes.fix_imports import alternates, build_pattern, FixImports |
| 13 |
| 14 MAPPING = {'UserDict': 'collections', |
| 15 } |
| 16 |
| 17 # def alternates(members): |
| 18 # return "(" + "|".join(map(repr, members)) + ")" |
| 19 # |
| 20 # |
| 21 # def build_pattern(mapping=MAPPING): |
| 22 # mod_list = ' | '.join(["module_name='%s'" % key for key in mapping]) |
| 23 # bare_names = alternates(mapping.keys()) |
| 24 # |
| 25 # yield """name_import=import_name< 'import' ((%s) | |
| 26 # multiple_imports=dotted_as_names< any* (%s) any* >) > |
| 27 # """ % (mod_list, mod_list) |
| 28 # yield """import_from< 'from' (%s) 'import' ['('] |
| 29 # ( any | import_as_name< any 'as' any > | |
| 30 # import_as_names< any* >) [')'] > |
| 31 # """ % mod_list |
| 32 # yield """import_name< 'import' (dotted_as_name< (%s) 'as' any > | |
| 33 # multiple_imports=dotted_as_names< |
| 34 # any* dotted_as_name< (%s) 'as' any > any* >) > |
| 35 # """ % (mod_list, mod_list) |
| 36 # |
| 37 # # Find usages of module members in code e.g. thread.foo(bar) |
| 38 # yield "power< bare_with_attr=(%s) trailer<'.' any > any* >" % bare_names |
| 39 |
| 40 |
| 41 # class FixUserDict(fixer_base.BaseFix): |
| 42 class FixUserdict(FixImports): |
| 43 |
| 44 BM_compatible = True |
| 45 keep_line_order = True |
| 46 # This is overridden in fix_imports2. |
| 47 mapping = MAPPING |
| 48 |
| 49 # We want to run this fixer late, so fix_import doesn't try to make stdlib |
| 50 # renames into relative imports. |
| 51 run_order = 6 |
| 52 |
| 53 def build_pattern(self): |
| 54 return "|".join(build_pattern(self.mapping)) |
| 55 |
| 56 def compile_pattern(self): |
| 57 # We override this, so MAPPING can be pragmatically altered and the |
| 58 # changes will be reflected in PATTERN. |
| 59 self.PATTERN = self.build_pattern() |
| 60 super(FixImports, self).compile_pattern() |
| 61 |
| 62 # Don't match the node if it's within another match. |
| 63 def match(self, node): |
| 64 match = super(FixImports, self).match |
| 65 results = match(node) |
| 66 if results: |
| 67 # Module usage could be in the trailer of an attribute lookup, so we |
| 68 # might have nested matches when "bare_with_attr" is present. |
| 69 if "bare_with_attr" not in results and \ |
| 70 any(match(obj) for obj in attr_chain(node, "parent")): |
| 71 return False |
| 72 return results |
| 73 return False |
| 74 |
| 75 def start_tree(self, tree, filename): |
| 76 super(FixImports, self).start_tree(tree, filename) |
| 77 self.replace = {} |
| 78 |
| 79 def transform(self, node, results): |
| 80 import_mod = results.get("module_name") |
| 81 if import_mod: |
| 82 mod_name = import_mod.value |
| 83 new_name = unicode(self.mapping[mod_name]) |
| 84 import_mod.replace(Name(new_name, prefix=import_mod.prefix)) |
| 85 if "name_import" in results: |
| 86 # If it's not a "from x import x, y" or "import x as y" import, |
| 87 # marked its usage to be replaced. |
| 88 self.replace[mod_name] = new_name |
| 89 if "multiple_imports" in results: |
| 90 # This is a nasty hack to fix multiple imports on a line (e.g., |
| 91 # "import StringIO, urlparse"). The problem is that I can't |
| 92 # figure out an easy way to make a pattern recognize the keys of |
| 93 # MAPPING randomly sprinkled in an import statement. |
| 94 results = self.match(node) |
| 95 if results: |
| 96 self.transform(node, results) |
| 97 else: |
| 98 # Replace usage of the module. |
| 99 bare_name = results["bare_with_attr"][0] |
| 100 new_name = self.replace.get(bare_name.value) |
| 101 if new_name: |
| 102 bare_name.replace(Name(new_name, prefix=bare_name.prefix)) |
| 103 |
OLD | NEW |