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

PackageRequirement.__init__()   A

Complexity

Conditions 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
1
from coala_decorators.decorators import generate_eq, generate_repr
2
3
4
@generate_eq("manager", "package", "version")
5
@generate_repr()
6
class PackageRequirement:
7
    """
8
    This class helps keeping track of bear requirements. It should simply
9
    be appended to the REQUIREMENTS tuple inside the Bear class.
10
11
    Two ``PackageRequirements`` should always be equal if they have the same
12
    manager, package and version:
13
14
    >>> pr1 = PackageRequirement('pip', 'coala_decorators', '0.1.0')
15
    >>> pr2 = PackageRequirement('pip', 'coala_decorators', '0.1.0')
16
    >>> pr1 == pr2
17
    True
18
    """
19
20
    def __init__(self, manager: str, package: str, version=""):
21
        """
22
        Constructs a new ``PackageRequirement``.
23
24
        >>> pr = PackageRequirement('pip', 'colorama', '0.1.0')
25
        >>> pr.manager
26
        'pip'
27
        >>> pr.package
28
        'colorama'
29
        >>> pr.version
30
        '0.1.0'
31
32
        :param manager: A string with the name of the manager (pip, npm, etc).
33
        :param package: A string with the name of the package to be installed.
34
        :param version: A version string. Leave empty to specify latest version.
35
        """
36
        self.manager = manager
37
        self.package = package
38
        self.version = version
39
40
    def check(self):
41
        """
42
        Check if the requirement is satisfied.
43
44
        >>> PackageRequirement('pip', 'coala_decorators', '0.2.1').check()
45
        Traceback (most recent call last):
46
        ...
47
        NotImplementedError
48
49
        :return: Returns True if satisfied, False if not.
50
        """
51
        raise NotImplementedError
52
53
    @classmethod
54
    def multiple(cls, *args):
55
        """
56
        Creates a tuple of multiple ``PackageRequirements``.
57
58
        >>> REQUIREMENTS = PackageRequirement.multiple(
59
        ...     ('pip', 'coala_decorators', '0.1.0'),
60
        ...     ('npm', 'perlcritic', '6.2.3'))
61
62
        Can omit version to specify the latest is wanted:
63
64
        >>> REQUIREMENTS = PackageRequirement.multiple(
65
        ...     ('pip', 'coala_decorators'), ('npm', 'perlcritic'))
66
67
        Will raise an error in case too many elements are provided:
68
69
        >>> REQUIREMENTS = PackageRequirement.multiple(
70
        ...     ('setuptools', '19.2', 'colorama', 'bower'))
71
        Traceback (most recent call last):
72
        ...
73
        TypeError: Too many elements provided.
74
75
        """
76
        reqs = []
77
        for requirement in args:
78
            if len(requirement) == 2:
79
                manager, package = requirement
80
                reqs.append(cls(manager, package))
81
            elif len(requirement) == 3:
82
                manager, package, version = requirement
83
                reqs.append(cls(manager, package, requirement))
84
            else:
85
                raise TypeError('Too many elements provided.')
86
        return tuple(reqs)
87