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