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