diff options
Diffstat (limited to 'Lib/lib2to3/fixes/basefix.py')
-rw-r--r-- | Lib/lib2to3/fixes/basefix.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/Lib/lib2to3/fixes/basefix.py b/Lib/lib2to3/fixes/basefix.py index 38666c1..937f8c1 100644 --- a/Lib/lib2to3/fixes/basefix.py +++ b/Lib/lib2to3/fixes/basefix.py @@ -16,6 +16,7 @@ except NameError: # Local imports from ..patcomp import PatternCompiler from .. import pygram +from .util import does_tree_import class BaseFix(object): @@ -36,6 +37,8 @@ class BaseFix(object): used_names = set() # A set of all used NAMEs order = "post" # Does the fixer prefer pre- or post-order traversal explicit = False # Is this ignored by refactor.py -f all? + run_order = 5 # Fixers will be sorted by run order before execution + # Lower numbers will be run first. # Shortcut for access to Python grammar symbols syms = pygram.python_symbols @@ -163,3 +166,23 @@ class BaseFix(object): filename - the name of the file the tree came from. """ pass + + +class ConditionalFix(BaseFix): + """ Base class for fixers which not execute if an import is found. """ + + # This is the name of the import which, if found, will cause the test to be skipped + skip_on = None + + def start_tree(self, *args): + super(ConditionalFix, self).start_tree(*args) + self._should_skip = None + + def should_skip(self, node): + if self._should_skip is not None: + return self._should_skip + pkg = self.skip_on.split(".") + name = pkg[-1] + pkg = ".".join(pkg[:-1]) + self._should_skip = does_tree_import(pkg, name, node) + return self._should_skip |