1 # Copyright 2006 Google, Inc. All Rights Reserved.
2 # Licensed to PSF under a Contributor Agreement.
4 """Base class for fixers (optional, but recommended)."""
11 from .patcomp
import PatternCompiler
13 from .fixer_util
import does_tree_import
15 class BaseFix(object):
17 """Optional base class for fixers.
19 The subclass name must be FixFooBar where FooBar is the result of
20 removing underscores and capitalizing the words of the fix name.
21 For example, the class name for a fixer named 'has_key' should be
25 PATTERN
= None # Most subclasses should override with a string literal
26 pattern
= None # Compiled pattern, set by compile_pattern()
27 options
= None # Options object passed to initializer
28 filename
= None # The filename (set by set_filename)
29 logger
= None # A logger (set by set_filename)
30 numbers
= itertools
.count(1) # For new_name()
31 used_names
= set() # A set of all used NAMEs
32 order
= "post" # Does the fixer prefer pre- or post-order traversal
33 explicit
= False # Is this ignored by refactor.py -f all?
34 run_order
= 5 # Fixers will be sorted by run order before execution
35 # Lower numbers will be run first.
36 _accept_type
= None # [Advanced and not public] This tells RefactoringTool
37 # which node type to accept when there's not a pattern.
39 # Shortcut for access to Python grammar symbols
40 syms
= pygram
.python_symbols
42 def __init__(self
, options
, log
):
43 """Initializer. Subclass may override.
46 options: an dict containing the options passed to RefactoringTool
47 that could be used to customize the fixer through the command line.
48 log: a list to append warnings and other messages to.
50 self
.options
= options
52 self
.compile_pattern()
54 def compile_pattern(self
):
55 """Compiles self.PATTERN into self.pattern.
57 Subclass may override if it doesn't want to use
58 self.{pattern,PATTERN} in .match().
60 if self
.PATTERN
is not None:
61 self
.pattern
= PatternCompiler().compile_pattern(self
.PATTERN
)
63 def set_filename(self
, filename
):
64 """Set the filename, and a logger derived from it.
66 The main refactoring tool should call this.
68 self
.filename
= filename
69 self
.logger
= logging
.getLogger(filename
)
71 def match(self
, node
):
72 """Returns match for a given parse tree node.
74 Should return a true or false object (not necessarily a bool).
75 It may return a non-empty dict of matching sub-nodes as
76 returned by a matching pattern.
78 Subclass may override.
80 results
= {"node": node
}
81 return self
.pattern
.match(node
, results
) and results
83 def transform(self
, node
, results
):
84 """Returns the transformation for a given parse tree node.
87 node: the root of the parse tree that matched the fixer.
88 results: a dict mapping symbolic names to part of the match.
91 None, or a node that is a modified copy of the
92 argument node. The node argument may also be modified in-place to
93 effect the same change.
95 Subclass *must* override.
97 raise NotImplementedError()
99 def new_name(self
, template
="xxx_todo_changeme"):
100 """Return a string suitable for use as an identifier
102 The new name is guaranteed not to conflict with other identifiers.
105 while name
in self
.used_names
:
106 name
= template
+ str(next(self
.numbers
))
107 self
.used_names
.add(name
)
110 def log_message(self
, message
):
112 self
.first_log
= False
113 self
.log
.append("### In file %s ###" % self
.filename
)
114 self
.log
.append(message
)
116 def cannot_convert(self
, node
, reason
=None):
117 """Warn the user that a given chunk of code is not valid Python 3,
118 but that it cannot be converted automatically.
120 First argument is the top-level node for the code in question.
121 Optional second argument is why it can't be converted.
123 lineno
= node
.get_lineno()
124 for_output
= node
.clone()
125 for_output
.prefix
= ""
126 msg
= "Line %d: could not convert: %s"
127 self
.log_message(msg
% (lineno
, for_output
))
129 self
.log_message(reason
)
131 def warning(self
, node
, reason
):
132 """Used for warning the user about possible uncertainty in the
135 First argument is the top-level node for the code in question.
136 Optional second argument is why it can't be converted.
138 lineno
= node
.get_lineno()
139 self
.log_message("Line %d: %s" % (lineno
, reason
))
141 def start_tree(self
, tree
, filename
):
142 """Some fixers need to maintain tree-wide state.
143 This method is called once, at the start of tree fix-up.
145 tree - the root node of the tree to be processed.
146 filename - the name of the file the tree came from.
148 self
.used_names
= tree
.used_names
149 self
.set_filename(filename
)
150 self
.numbers
= itertools
.count(1)
151 self
.first_log
= True
153 def finish_tree(self
, tree
, filename
):
154 """Some fixers need to maintain tree-wide state.
155 This method is called once, at the conclusion of tree fix-up.
157 tree - the root node of the tree to be processed.
158 filename - the name of the file the tree came from.
163 class ConditionalFix(BaseFix
):
164 """ Base class for fixers which not execute if an import is found. """
166 # This is the name of the import which, if found, will cause the test to be skipped
169 def start_tree(self
, *args
):
170 super(ConditionalFix
, self
).start_tree(*args
)
171 self
._should
_skip
= None
173 def should_skip(self
, node
):
174 if self
._should
_skip
is not None:
175 return self
._should
_skip
176 pkg
= self
.skip_on
.split(".")
178 pkg
= ".".join(pkg
[:-1])
179 self
._should
_skip
= does_tree_import(pkg
, name
, node
)
180 return self
._should
_skip