blob: a82efd3691bb2b0e9b8b57f3ef3bf8c3d99ca8cc [file] [log] [blame]
Tarek Ziade1231a4e2011-05-19 13:07:25 +02001"""Compatibility helpers.
2
3This module provides classes, variables and imports which are used to
4support packaging across Python 2.x and 3.x.
5"""
6
7from 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
15try:
16 from packaging.util import Mixin2to3 as _Mixin2to3
17 _CONVERT = True
18 _KLASS = _Mixin2to3
19except ImportError:
20 _CONVERT = False
21 _KLASS = object
22
23__all__ = ['Mixin2to3']
24
25
26class 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