Completed
Pull Request — master (#2185)
by Zatreanu
01:56
created

PythonRequirement   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 77
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 17 1
B multiple() 0 53 4
1
from coalib.bears.requirements.PackageRequirement import PackageRequirement
2
3
4
class PythonRequirement(PackageRequirement):
5
    """
6
    This class is a subclass of ``PackageRequirement``, and helps specifying
7
    requirements from ``pip``, without using the manager name.
8
    """
9
10
    def __init__(self, package, version=""):
11
        """
12
        Constructs a new ``PythonRequirement``, using the ``PackageRequirement``
13
        constructor.
14
15
        >>> pr = PythonRequirement('setuptools', '19.2')
16
        >>> pr.manager
17
        'pip'
18
        >>> pr.package
19
        'setuptools'
20
        >>> pr.version
21
        '19.2'
22
23
        :param package: A string with the name of the package to be installed.
24
        :param version: A version string. Leave empty to specify latest version.
25
        """
26
        PackageRequirement.__init__(self, 'pip', package, version)
27
28
    @classmethod
29
    def multiple(cls, *args):
30
        """
31
        Creates a tuple of multiple ``PythonRequirements``.
32
33
        You should use the ``multiple`` method if you have more
34
        requirements from the same manager. This can receive both tuples of
35
        strings, in case you want a specific version, or a simple string, in
36
        case you want the latest version to be specified.
37
38
        This is the case where you would provide strings only, to specify the
39
        latest version automatically:
40
41
        >>> REQUIREMENTS = PythonRequirement.multiple(
42
        ...     'coala_decorators', 'setuptools')
43
44
        And if you choose to mix them, specifying version for some and for some
45
        not:
46
47
        >>> REQUIREMENTS = PythonRequirement.multiple(
48
        ...     'coala_decorators', ('setuptools', '19.2'))
49
50
        Lists are also valid arguments:
51
52
        >>> REQUIREMENTS = PythonRequirement.multiple(
53
        ...     ['coala_decorators', '19.2'],)
54
55
        In case you provide too many arguments into the tuple, an error will be
56
        raised:
57
58
        >>> REQUIREMENTS = PythonRequirement.multiple(
59
        ...     'coala_decorators', ('setuptools', '19.2', 'colorama'))
60
        Traceback (most recent call last):
61
        ...
62
        TypeError: Too many elements provided.
63
64
        :param args:       Should be iterables with two elements:
65
                           ``('packageName', 'version')`` or strings:
66
                           ``'packageName'`` if latest version is wanted.
67
        :return:           A tuple containing ``PythonRequirements``.
68
        :raises TypeError: In case the iterables contain more than two
69
                           elements.
70
        """
71
        reqs = []
72
        for requirement in args:
73
            if isinstance(requirement, str):
74
                reqs.append(cls(requirement))
75
            elif len(requirement) == 2:
76
                name, version = requirement
77
                reqs.append(cls(name, version))
78
            else:
79
                raise TypeError('Too many elements provided.')
80
        return tuple(reqs)
81