blob: df581a4deab9e93aade8591bf13f949050d7e50d [file] [log] [blame]
Martin v. Löwisef04c442008-03-19 05:04:44 +00001# Copyright 2006 Google, Inc. All Rights Reserved.
2# Licensed to PSF under a Contributor Agreement.
3
4"""Base class for fixers (optional, but recommended)."""
5
6# Python imports
Martin v. Löwisef04c442008-03-19 05:04:44 +00007import itertools
8
Martin v. Löwisef04c442008-03-19 05:04:44 +00009# Local imports
Benjamin Petersondf6dc8f2008-06-15 02:57:40 +000010from .patcomp import PatternCompiler
11from . import pygram
12from .fixer_util import does_tree_import
Martin v. Löwisef04c442008-03-19 05:04:44 +000013
14class BaseFix(object):
15
16 """Optional base class for fixers.
17
18 The subclass name must be FixFooBar where FooBar is the result of
19 removing underscores and capitalizing the words of the fix name.
20 For example, the class name for a fixer named 'has_key' should be
21 FixHasKey.
22 """
23
24 PATTERN = None # Most subclasses should override with a string literal
25 pattern = None # Compiled pattern, set by compile_pattern()
Benjamin Petersonf37eb3a2010-10-14 23:00:04 +000026 pattern_tree = None # Tree representation of the pattern
Martin v. Löwisef04c442008-03-19 05:04:44 +000027 options = None # Options object passed to initializer
28 filename = None # The filename (set by set_filename)
Martin v. Löwisef04c442008-03-19 05:04:44 +000029 numbers = itertools.count(1) # For new_name()
30 used_names = set() # A set of all used NAMEs
31 order = "post" # Does the fixer prefer pre- or post-order traversal
32 explicit = False # Is this ignored by refactor.py -f all?
Martin v. Löwis3faa84f2008-03-22 00:07:09 +000033 run_order = 5 # Fixers will be sorted by run order before execution
34 # Lower numbers will be run first.
Benjamin Peterson3059b002009-07-20 16:42:03 +000035 _accept_type = None # [Advanced and not public] This tells RefactoringTool
36 # which node type to accept when there's not a pattern.
Martin v. Löwisef04c442008-03-19 05:04:44 +000037
Benjamin Petersonf37eb3a2010-10-14 23:00:04 +000038 keep_line_order = False # For the bottom matcher: match with the
39 # original line order
40 BM_compatible = False # Compatibility with the bottom matching
41 # module; every fixer should set this
42 # manually
43
Martin v. Löwisef04c442008-03-19 05:04:44 +000044 # Shortcut for access to Python grammar symbols
45 syms = pygram.python_symbols
46
47 def __init__(self, options, log):
48 """Initializer. Subclass may override.
49
50 Args:
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +030051 options: a dict containing the options passed to RefactoringTool
Benjamin Peterson8951b612008-09-03 02:27:16 +000052 that could be used to customize the fixer through the command line.
Martin v. Löwisef04c442008-03-19 05:04:44 +000053 log: a list to append warnings and other messages to.
54 """
55 self.options = options
56 self.log = log
57 self.compile_pattern()
58
59 def compile_pattern(self):
60 """Compiles self.PATTERN into self.pattern.
61
62 Subclass may override if it doesn't want to use
63 self.{pattern,PATTERN} in .match().
64 """
65 if self.PATTERN is not None:
Benjamin Petersonf37eb3a2010-10-14 23:00:04 +000066 PC = PatternCompiler()
67 self.pattern, self.pattern_tree = PC.compile_pattern(self.PATTERN,
68 with_tree=True)
Martin v. Löwisef04c442008-03-19 05:04:44 +000069
70 def set_filename(self, filename):
Vinay Sajipbf9c0212011-07-13 23:15:07 +010071 """Set the filename.
Martin v. Löwisef04c442008-03-19 05:04:44 +000072
73 The main refactoring tool should call this.
74 """
75 self.filename = filename
Martin v. Löwisef04c442008-03-19 05:04:44 +000076
77 def match(self, node):
78 """Returns match for a given parse tree node.
79
80 Should return a true or false object (not necessarily a bool).
81 It may return a non-empty dict of matching sub-nodes as
82 returned by a matching pattern.
83
84 Subclass may override.
85 """
86 results = {"node": node}
87 return self.pattern.match(node, results) and results
88
89 def transform(self, node, results):
90 """Returns the transformation for a given parse tree node.
91
92 Args:
93 node: the root of the parse tree that matched the fixer.
94 results: a dict mapping symbolic names to part of the match.
Martin v. Löwisf733c602008-03-19 05:26:18 +000095
Martin v. Löwisef04c442008-03-19 05:04:44 +000096 Returns:
97 None, or a node that is a modified copy of the
98 argument node. The node argument may also be modified in-place to
99 effect the same change.
100
101 Subclass *must* override.
102 """
103 raise NotImplementedError()
104
Martin v. Löwisef04c442008-03-19 05:04:44 +0000105 def new_name(self, template="xxx_todo_changeme"):
106 """Return a string suitable for use as an identifier
107
108 The new name is guaranteed not to conflict with other identifiers.
109 """
110 name = template
111 while name in self.used_names:
Martin v. Löwis8a5f8ca2008-03-19 05:33:36 +0000112 name = template + str(next(self.numbers))
Martin v. Löwisef04c442008-03-19 05:04:44 +0000113 self.used_names.add(name)
114 return name
115
116 def log_message(self, message):
117 if self.first_log:
118 self.first_log = False
119 self.log.append("### In file %s ###" % self.filename)
120 self.log.append(message)
121
122 def cannot_convert(self, node, reason=None):
123 """Warn the user that a given chunk of code is not valid Python 3,
124 but that it cannot be converted automatically.
125
126 First argument is the top-level node for the code in question.
127 Optional second argument is why it can't be converted.
128 """
129 lineno = node.get_lineno()
130 for_output = node.clone()
Benjamin Peterson2c3ac6b2009-06-11 23:47:38 +0000131 for_output.prefix = ""
Martin v. Löwisef04c442008-03-19 05:04:44 +0000132 msg = "Line %d: could not convert: %s"
133 self.log_message(msg % (lineno, for_output))
134 if reason:
135 self.log_message(reason)
136
137 def warning(self, node, reason):
138 """Used for warning the user about possible uncertainty in the
139 translation.
140
141 First argument is the top-level node for the code in question.
142 Optional second argument is why it can't be converted.
143 """
144 lineno = node.get_lineno()
145 self.log_message("Line %d: %s" % (lineno, reason))
146
147 def start_tree(self, tree, filename):
148 """Some fixers need to maintain tree-wide state.
149 This method is called once, at the start of tree fix-up.
Martin v. Löwisf733c602008-03-19 05:26:18 +0000150
Martin v. Löwisef04c442008-03-19 05:04:44 +0000151 tree - the root node of the tree to be processed.
152 filename - the name of the file the tree came from.
153 """
154 self.used_names = tree.used_names
155 self.set_filename(filename)
156 self.numbers = itertools.count(1)
157 self.first_log = True
158
159 def finish_tree(self, tree, filename):
160 """Some fixers need to maintain tree-wide state.
161 This method is called once, at the conclusion of tree fix-up.
Martin v. Löwisf733c602008-03-19 05:26:18 +0000162
Martin v. Löwisef04c442008-03-19 05:04:44 +0000163 tree - the root node of the tree to be processed.
164 filename - the name of the file the tree came from.
165 """
166 pass
Martin v. Löwis3faa84f2008-03-22 00:07:09 +0000167
168
169class ConditionalFix(BaseFix):
170 """ Base class for fixers which not execute if an import is found. """
171
172 # This is the name of the import which, if found, will cause the test to be skipped
173 skip_on = None
174
175 def start_tree(self, *args):
176 super(ConditionalFix, self).start_tree(*args)
177 self._should_skip = None
178
179 def should_skip(self, node):
180 if self._should_skip is not None:
181 return self._should_skip
182 pkg = self.skip_on.split(".")
183 name = pkg[-1]
184 pkg = ".".join(pkg[:-1])
185 self._should_skip = does_tree_import(pkg, name, node)
186 return self._should_skip