Completed
Pull Request — master (#2166)
by Zatreanu
01:53
created

PythonRequirement.multiple()   B

Complexity

Conditions 5

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
c 2
b 0
f 0
dl 0
loc 25
rs 8.0894
1
from coalib.bears.requirements.PackageRequirement import PackageRequirement
2
3
4
class PythonRequirement(PackageRequirement):
5
6
    """
7
    This class is a subclass of ``PackageRequirement``, and helps specifying
8
    more requirements from ``pip``, without writing the name of the class
9
    multiple times.
10
11
    Alternatively, you should use the ``multiple`` method if you have more
12
    requirements from the same manager. This can receive both tuples of strings,
13
    in case you want a specific version, or a simple string, in case you want
14
    the latest version to be installed.
15
16
    This is the case where you would provide strings only, to install the
17
    latest version automatically:
18
19
    >>> REQUIREMENTS = PythonRequirement.multiple(
20
    ...                                         'coala_decorators','setuptools')
21
22
    And if you choose to mix them, specifying version for some and for some not:
23
24
    >>> REQUIREMENTS = PythonRequirement.multiple(
25
    ...                             'coala_decorators',('setuptools', '19.2'))
26
27
    In case you provide too many arguments into the tuple, an error will be
28
    raised:
29
30
    >>> REQUIREMENTS = PythonRequirement.multiple(
31
    ...                 'coala_decorators',('setuptools', '19.2', 'colorama'))
32
    Traceback (most recent call last):
33
    ...
34
    TypeError: The tuple must have 2 elements.
35
36
    The same would happen in case you provide something different than a string
37
    or a tuple:
38
39
    >>> x = [1, 2, 3, 4]
40
    >>> REQUIREMENTS = PythonRequirement.multiple(x)
41
    Traceback (most recent call last):
42
    ...
43
    TypeError: The arguments need to be tuples or strings.
44
    """
45
46
    def __init__(self, package, version=""):
47
        """
48
        Constructs a new PythonRequirement, using the PackageRequirement
49
        constructor.
50
51
        :param package: A string with the name of the package to be installed.
52
        :param version: A number that contains the version. Leave empty to
53
                        install latest.
54
        """
55
        PackageRequirement.__init__(self, 'pip', package, version)
56
57
    @classmethod
58
    def multiple(cls, *args):
59
        """
60
        Creates a tuple of multiple ``pip`` requirements, consisting of
61
        either tuples or strings.
62
63
        :param args: Either strings if the latest version is wanted, or tuples
64
                     of strings, where the strings consist of the package and
65
                     the version number (which is also a string).
66
        :return:     A tuple containing tuples (package names and versions) and
67
                     strings (package names only).
68
        """
69
        reqs = ()
70
        for requirement in args:
71
            if isinstance(requirement, str):
72
                reqs += (requirement,)
73
            elif isinstance(requirement, tuple):
74
                try:
75
                    name, version = requirement
76
                    reqs += (name, version)
77
                except ValueError:
78
                    raise TypeError('The tuple must have 2 elements.')
79
            else:
80
                raise TypeError('The arguments need to be tuples or strings.')
81
        return reqs
82