blob: bfce92de66c6cc7acaeadb14ffbbc95313cf4aea [file] [log] [blame]
Éric Araujo692a4932012-02-09 21:37:14 +01001"""Support for build-time 2to3 conversion."""
Tarek Ziade1231a4e2011-05-19 13:07:25 +02002
3from 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 Ziade1231a4e2011-05-19 13:07:25 +02009try:
10 from packaging.util import Mixin2to3 as _Mixin2to3
11 _CONVERT = True
12 _KLASS = _Mixin2to3
13except ImportError:
14 _CONVERT = False
15 _KLASS = object
16
17__all__ = ['Mixin2to3']
18
19
20class 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 Araujo692a4932012-02-09 21:37:14 +010028 def _run_2to3(self, files=[], doctests=[], fixers=[]):
Tarek Ziade1231a4e2011-05-19 13:07:25 +020029 """ 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 Araujo692a4932012-02-09 21:37:14 +010038 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 Ziade1231a4e2011-05-19 13:07:25 +020042
Éric Araujo692a4932012-02-09 21:37:14 +010043 if doctests:
44 logger.info('converting doctests in text files')
Tarek Ziade1231a4e2011-05-19 13:07:25 +020045 _KLASS.run_2to3(self, doctests, doctests_only=True)
46 else:
47 # If run on Python 2.x, there is nothing to do.
48
Éric Araujo692a4932012-02-09 21:37:14 +010049 def _run_2to3(self, files=[], doctests=[], fixers=[]):
Tarek Ziade1231a4e2011-05-19 13:07:25 +020050 pass