summaryrefslogtreecommitdiff
path: root/src/virtualenv/config/convert.py
blob: e5f1ae8f5278953ca2230fc1a3c16c9664e71332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from __future__ import annotations

import logging
import os


class TypeData:
    def __init__(self, default_type, as_type):
        self.default_type = default_type
        self.as_type = as_type

    def __repr__(self):
        return f"{self.__class__.__name__}(base={self.default_type}, as={self.as_type})"

    def convert(self, value):
        return self.default_type(value)


class BoolType(TypeData):
    BOOLEAN_STATES = {
        "1": True,
        "yes": True,
        "true": True,
        "on": True,
        "0": False,
        "no": False,
        "false": False,
        "off": False,
    }

    def convert(self, value):
        if value.lower() not in self.BOOLEAN_STATES:
            raise ValueError(f"Not a boolean: {value}")
        return self.BOOLEAN_STATES[value.lower()]


class NoneType(TypeData):
    def convert(self, value):
        if not value:
            return None
        return str(value)


class ListType(TypeData):
    def _validate(self):
        """ """

    def convert(self, value, flatten=True):  # noqa: U100
        values = self.split_values(value)
        result = []
        for value in values:
            sub_values = value.split(os.pathsep)
            result.extend(sub_values)
        converted = [self.as_type(i) for i in result]
        return converted

    def split_values(self, value):
        """Split the provided value into a list.

        First this is done by newlines. If there were no newlines in the text,
        then we next try to split by comma.
        """
        if isinstance(value, (str, bytes)):
            # Use `splitlines` rather than a custom check for whether there is
            # more than one line. This ensures that the full `splitlines()`
            # logic is supported here.
            values = value.splitlines()
            if len(values) <= 1:
                values = value.split(",")
            values = filter(None, [x.strip() for x in values])
        else:
            values = list(value)

        return values


def convert(value, as_type, source):
    """Convert the value as a given type where the value comes from the given source"""
    try:
        return as_type.convert(value)
    except Exception as exception:
        logging.warning("%s failed to convert %r as %r because %r", source, value, as_type, exception)
        raise


_CONVERT = {bool: BoolType, type(None): NoneType, list: ListType}


def get_type(action):
    default_type = type(action.default)
    as_type = default_type if action.type is None else action.type
    return _CONVERT.get(default_type, TypeData)(default_type, as_type)


__all__ = [
    "convert",
    "get_type",
]