| Total Complexity | 4 |
| Total Lines | 42 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 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 | detected_version = '%s%s' % ( |
||
| 23 | platform.python_implementation(), |
||
| 24 | sys.version[0], |
||
| 25 | ) |
||
| 26 | |||
| 27 | module_name = implementation[override if override else detected_version] |
||
| 28 | return import_module(module_name) |
||
| 29 | |||
| 30 | |||
| 31 | class ExtendAction(AppendAction): |
||
| 32 | """ |
||
| 33 | Argparse "extend" action for Python < 3.8. |
||
| 34 | A simplified backport from the Python standard library. |
||
| 35 | """ |
||
| 36 | |||
| 37 | def __call__(self, parser, namespace, values, option_string=None): |
||
| 38 | items = getattr(namespace, self.dest, None) |
||
| 39 | items = [] if items is None else items[:] |
||
| 40 | items.extend(values) |
||
| 41 | setattr(namespace, self.dest, items) |
||
| 42 |