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