|
1
|
|
|
""" |
|
2
|
|
|
Cross-Python version compatibility. |
|
3
|
|
|
""" |
|
4
|
|
|
import platform |
|
5
|
|
|
import sys |
|
6
|
|
|
from argparse import _AppendAction as AppendAction |
|
7
|
|
|
from importlib import import_module |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
def get_implementation(override=None): |
|
11
|
|
|
""" |
|
12
|
|
|
Detect the active Python version and return a reference to the |
|
13
|
|
|
module serving the version-specific pyclean implementation. |
|
14
|
|
|
""" |
|
15
|
|
|
implementation = dict( |
|
16
|
|
|
CPython2='pyclean.py2clean', |
|
17
|
|
|
CPython3='pyclean.py3clean', |
|
18
|
|
|
PyPy2='pyclean.pypyclean', |
|
19
|
|
|
PyPy3='pyclean.pypyclean', |
|
20
|
|
|
) |
|
21
|
|
|
|
|
22
|
|
|
# pylint: disable=consider-using-f-string |
|
23
|
|
|
detected_version = '%s%s' % ( |
|
24
|
|
|
platform.python_implementation(), |
|
25
|
|
|
sys.version[0], |
|
26
|
|
|
) |
|
27
|
|
|
|
|
28
|
|
|
module_name = implementation[override if override else detected_version] |
|
29
|
|
|
return import_module(module_name) |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
class ExtendAction(AppendAction): |
|
33
|
|
|
""" |
|
34
|
|
|
Argparse "extend" action for Python < 3.8. |
|
35
|
|
|
A simplified backport from the Python standard library. |
|
36
|
|
|
""" |
|
37
|
|
|
|
|
38
|
|
|
def __call__(self, parser, namespace, values, option_string=None): |
|
39
|
|
|
items = getattr(namespace, self.dest, None) |
|
40
|
|
|
items = [] if items is None else items[:] |
|
41
|
|
|
items.extend(values) |
|
42
|
|
|
setattr(namespace, self.dest, items) |
|
43
|
|
|
|