Passed
Pull Request — main (#50)
by Peter
01:13
created

pyclean.compat.ExtendAction.__call__()   A

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 5
dl 0
loc 5
rs 10
c 0
b 0
f 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