Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 1 | # 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 |
| 7 | import logging |
| 8 | import itertools |
| 9 | |
| 10 | # Get a usable 'set' constructor |
| 11 | try: |
| 12 | set |
| 13 | except NameError: |
| 14 | from sets import Set as set |
| 15 | |
| 16 | # Local imports |
Benjamin Peterson | df6dc8f | 2008-06-15 02:57:40 +0000 | [diff] [blame] | 17 | from .patcomp import PatternCompiler |
| 18 | from . import pygram |
| 19 | from .fixer_util import does_tree_import |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 20 | |
| 21 | class BaseFix(object): |
| 22 | |
| 23 | """Optional base class for fixers. |
| 24 | |
| 25 | The subclass name must be FixFooBar where FooBar is the result of |
| 26 | removing underscores and capitalizing the words of the fix name. |
| 27 | For example, the class name for a fixer named 'has_key' should be |
| 28 | FixHasKey. |
| 29 | """ |
| 30 | |
| 31 | PATTERN = None # Most subclasses should override with a string literal |
| 32 | pattern = None # Compiled pattern, set by compile_pattern() |
| 33 | options = None # Options object passed to initializer |
| 34 | filename = None # The filename (set by set_filename) |
| 35 | logger = None # A logger (set by set_filename) |
| 36 | numbers = itertools.count(1) # For new_name() |
| 37 | used_names = set() # A set of all used NAMEs |
| 38 | order = "post" # Does the fixer prefer pre- or post-order traversal |
| 39 | explicit = False # Is this ignored by refactor.py -f all? |
Martin v. Löwis | 3faa84f | 2008-03-22 00:07:09 +0000 | [diff] [blame] | 40 | run_order = 5 # Fixers will be sorted by run order before execution |
| 41 | # Lower numbers will be run first. |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 42 | |
| 43 | # Shortcut for access to Python grammar symbols |
| 44 | syms = pygram.python_symbols |
| 45 | |
| 46 | def __init__(self, options, log): |
| 47 | """Initializer. Subclass may override. |
| 48 | |
| 49 | Args: |
| 50 | options: an optparse.Values instance which can be used |
| 51 | to inspect the command line options. |
| 52 | log: a list to append warnings and other messages to. |
| 53 | """ |
| 54 | self.options = options |
| 55 | self.log = log |
| 56 | self.compile_pattern() |
| 57 | |
| 58 | def compile_pattern(self): |
| 59 | """Compiles self.PATTERN into self.pattern. |
| 60 | |
| 61 | Subclass may override if it doesn't want to use |
| 62 | self.{pattern,PATTERN} in .match(). |
| 63 | """ |
| 64 | if self.PATTERN is not None: |
| 65 | self.pattern = PatternCompiler().compile_pattern(self.PATTERN) |
| 66 | |
| 67 | def set_filename(self, filename): |
| 68 | """Set the filename, and a logger derived from it. |
| 69 | |
| 70 | The main refactoring tool should call this. |
| 71 | """ |
| 72 | self.filename = filename |
| 73 | self.logger = logging.getLogger(filename) |
| 74 | |
| 75 | def match(self, node): |
| 76 | """Returns match for a given parse tree node. |
| 77 | |
| 78 | Should return a true or false object (not necessarily a bool). |
| 79 | It may return a non-empty dict of matching sub-nodes as |
| 80 | returned by a matching pattern. |
| 81 | |
| 82 | Subclass may override. |
| 83 | """ |
| 84 | results = {"node": node} |
| 85 | return self.pattern.match(node, results) and results |
| 86 | |
| 87 | def transform(self, node, results): |
| 88 | """Returns the transformation for a given parse tree node. |
| 89 | |
| 90 | Args: |
| 91 | node: the root of the parse tree that matched the fixer. |
| 92 | results: a dict mapping symbolic names to part of the match. |
Martin v. Löwis | f733c60 | 2008-03-19 05:26:18 +0000 | [diff] [blame] | 93 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 94 | Returns: |
| 95 | None, or a node that is a modified copy of the |
| 96 | argument node. The node argument may also be modified in-place to |
| 97 | effect the same change. |
| 98 | |
| 99 | Subclass *must* override. |
| 100 | """ |
| 101 | raise NotImplementedError() |
| 102 | |
| 103 | def parenthesize(self, node): |
| 104 | """Wrapper around pygram.parenthesize().""" |
| 105 | return pygram.parenthesize(node) |
| 106 | |
| 107 | def new_name(self, template="xxx_todo_changeme"): |
| 108 | """Return a string suitable for use as an identifier |
| 109 | |
| 110 | The new name is guaranteed not to conflict with other identifiers. |
| 111 | """ |
| 112 | name = template |
| 113 | while name in self.used_names: |
Martin v. Löwis | 8a5f8ca | 2008-03-19 05:33:36 +0000 | [diff] [blame] | 114 | name = template + str(next(self.numbers)) |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 115 | self.used_names.add(name) |
| 116 | return name |
| 117 | |
| 118 | def log_message(self, message): |
| 119 | if self.first_log: |
| 120 | self.first_log = False |
| 121 | self.log.append("### In file %s ###" % self.filename) |
| 122 | self.log.append(message) |
| 123 | |
| 124 | def cannot_convert(self, node, reason=None): |
| 125 | """Warn the user that a given chunk of code is not valid Python 3, |
| 126 | but that it cannot be converted automatically. |
| 127 | |
| 128 | First argument is the top-level node for the code in question. |
| 129 | Optional second argument is why it can't be converted. |
| 130 | """ |
| 131 | lineno = node.get_lineno() |
| 132 | for_output = node.clone() |
| 133 | for_output.set_prefix("") |
| 134 | msg = "Line %d: could not convert: %s" |
| 135 | self.log_message(msg % (lineno, for_output)) |
| 136 | if reason: |
| 137 | self.log_message(reason) |
| 138 | |
| 139 | def warning(self, node, reason): |
| 140 | """Used for warning the user about possible uncertainty in the |
| 141 | translation. |
| 142 | |
| 143 | First argument is the top-level node for the code in question. |
| 144 | Optional second argument is why it can't be converted. |
| 145 | """ |
| 146 | lineno = node.get_lineno() |
| 147 | self.log_message("Line %d: %s" % (lineno, reason)) |
| 148 | |
| 149 | def start_tree(self, tree, filename): |
| 150 | """Some fixers need to maintain tree-wide state. |
| 151 | This method is called once, at the start of tree fix-up. |
Martin v. Löwis | f733c60 | 2008-03-19 05:26:18 +0000 | [diff] [blame] | 152 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 153 | tree - the root node of the tree to be processed. |
| 154 | filename - the name of the file the tree came from. |
| 155 | """ |
| 156 | self.used_names = tree.used_names |
| 157 | self.set_filename(filename) |
| 158 | self.numbers = itertools.count(1) |
| 159 | self.first_log = True |
| 160 | |
| 161 | def finish_tree(self, tree, filename): |
| 162 | """Some fixers need to maintain tree-wide state. |
| 163 | This method is called once, at the conclusion of tree fix-up. |
Martin v. Löwis | f733c60 | 2008-03-19 05:26:18 +0000 | [diff] [blame] | 164 | |
Martin v. Löwis | ef04c44 | 2008-03-19 05:04:44 +0000 | [diff] [blame] | 165 | tree - the root node of the tree to be processed. |
| 166 | filename - the name of the file the tree came from. |
| 167 | """ |
| 168 | pass |
Martin v. Löwis | 3faa84f | 2008-03-22 00:07:09 +0000 | [diff] [blame] | 169 | |
| 170 | |
| 171 | class ConditionalFix(BaseFix): |
| 172 | """ Base class for fixers which not execute if an import is found. """ |
| 173 | |
| 174 | # This is the name of the import which, if found, will cause the test to be skipped |
| 175 | skip_on = None |
| 176 | |
| 177 | def start_tree(self, *args): |
| 178 | super(ConditionalFix, self).start_tree(*args) |
| 179 | self._should_skip = None |
| 180 | |
| 181 | def should_skip(self, node): |
| 182 | if self._should_skip is not None: |
| 183 | return self._should_skip |
| 184 | pkg = self.skip_on.split(".") |
| 185 | name = pkg[-1] |
| 186 | pkg = ".".join(pkg[:-1]) |
| 187 | self._should_skip = does_tree_import(pkg, name, node) |
| 188 | return self._should_skip |