Tarek Ziade | 1231a4e | 2011-05-19 13:07:25 +0200 | [diff] [blame] | 1 | """Compatibility helpers. |
| 2 | |
| 3 | This module provides classes, variables and imports which are used to |
| 4 | support packaging across Python 2.x and 3.x. |
| 5 | """ |
| 6 | |
| 7 | from packaging import logger |
| 8 | |
| 9 | |
| 10 | # XXX Having two classes with the same name is not a good thing. |
| 11 | # XXX 2to3-related code should move from util to this module |
| 12 | |
| 13 | # TODO Move common code here: PY3 (bool indicating if we're on 3.x), any, etc. |
| 14 | |
| 15 | try: |
| 16 | from packaging.util import Mixin2to3 as _Mixin2to3 |
| 17 | _CONVERT = True |
| 18 | _KLASS = _Mixin2to3 |
| 19 | except ImportError: |
| 20 | _CONVERT = False |
| 21 | _KLASS = object |
| 22 | |
| 23 | __all__ = ['Mixin2to3'] |
| 24 | |
| 25 | |
| 26 | class Mixin2to3(_KLASS): |
| 27 | """ The base class which can be used for refactoring. When run under |
| 28 | Python 3.0, the run_2to3 method provided by Mixin2to3 is overridden. |
| 29 | When run on Python 2.x, it merely creates a class which overrides run_2to3, |
| 30 | yet does nothing in particular with it. |
| 31 | """ |
| 32 | if _CONVERT: |
| 33 | |
| 34 | def _run_2to3(self, files, doctests=[], fixers=[]): |
| 35 | """ Takes a list of files and doctests, and performs conversion |
| 36 | on those. |
| 37 | - First, the files which contain the code(`files`) are converted. |
| 38 | - Second, the doctests in `files` are converted. |
| 39 | - Thirdly, the doctests in `doctests` are converted. |
| 40 | """ |
| 41 | if fixers: |
| 42 | self.fixer_names = fixers |
| 43 | |
| 44 | logger.info('converting Python code') |
| 45 | _KLASS.run_2to3(self, files) |
| 46 | |
| 47 | logger.info('converting doctests in Python files') |
| 48 | _KLASS.run_2to3(self, files, doctests_only=True) |
| 49 | |
| 50 | if doctests != []: |
| 51 | logger.info('converting doctest in text files') |
| 52 | _KLASS.run_2to3(self, doctests, doctests_only=True) |
| 53 | else: |
| 54 | # If run on Python 2.x, there is nothing to do. |
| 55 | |
| 56 | def _run_2to3(self, files, doctests=[], fixers=[]): |
| 57 | pass |