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