]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | """Fix incompatible renames\r |
2 | \r | |
3 | Fixes:\r | |
4 | * sys.maxint -> sys.maxsize\r | |
5 | """\r | |
6 | # Author: Christian Heimes\r | |
7 | # based on Collin Winter's fix_import\r | |
8 | \r | |
9 | # Local imports\r | |
10 | from .. import fixer_base\r | |
11 | from ..fixer_util import Name, attr_chain\r | |
12 | \r | |
13 | MAPPING = {"sys": {"maxint" : "maxsize"},\r | |
14 | }\r | |
15 | LOOKUP = {}\r | |
16 | \r | |
17 | def alternates(members):\r | |
18 | return "(" + "|".join(map(repr, members)) + ")"\r | |
19 | \r | |
20 | \r | |
21 | def build_pattern():\r | |
22 | #bare = set()\r | |
23 | for module, replace in MAPPING.items():\r | |
24 | for old_attr, new_attr in replace.items():\r | |
25 | LOOKUP[(module, old_attr)] = new_attr\r | |
26 | #bare.add(module)\r | |
27 | #bare.add(old_attr)\r | |
28 | #yield """\r | |
29 | # import_name< 'import' (module=%r\r | |
30 | # | dotted_as_names< any* module=%r any* >) >\r | |
31 | # """ % (module, module)\r | |
32 | yield """\r | |
33 | import_from< 'from' module_name=%r 'import'\r | |
34 | ( attr_name=%r | import_as_name< attr_name=%r 'as' any >) >\r | |
35 | """ % (module, old_attr, old_attr)\r | |
36 | yield """\r | |
37 | power< module_name=%r trailer< '.' attr_name=%r > any* >\r | |
38 | """ % (module, old_attr)\r | |
39 | #yield """bare_name=%s""" % alternates(bare)\r | |
40 | \r | |
41 | \r | |
42 | class FixRenames(fixer_base.BaseFix):\r | |
43 | BM_compatible = True\r | |
44 | PATTERN = "|".join(build_pattern())\r | |
45 | \r | |
46 | order = "pre" # Pre-order tree traversal\r | |
47 | \r | |
48 | # Don't match the node if it's within another match\r | |
49 | def match(self, node):\r | |
50 | match = super(FixRenames, self).match\r | |
51 | results = match(node)\r | |
52 | if results:\r | |
53 | if any(match(obj) for obj in attr_chain(node, "parent")):\r | |
54 | return False\r | |
55 | return results\r | |
56 | return False\r | |
57 | \r | |
58 | #def start_tree(self, tree, filename):\r | |
59 | # super(FixRenames, self).start_tree(tree, filename)\r | |
60 | # self.replace = {}\r | |
61 | \r | |
62 | def transform(self, node, results):\r | |
63 | mod_name = results.get("module_name")\r | |
64 | attr_name = results.get("attr_name")\r | |
65 | #bare_name = results.get("bare_name")\r | |
66 | #import_mod = results.get("module")\r | |
67 | \r | |
68 | if mod_name and attr_name:\r | |
69 | new_attr = unicode(LOOKUP[(mod_name.value, attr_name.value)])\r | |
70 | attr_name.replace(Name(new_attr, prefix=attr_name.prefix))\r |