summaryrefslogtreecommitdiff
path: root/src/tox/config/loader/convert.py
blob: b9259b05b4832557e06ea8971df287763dc3b64b (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from __future__ import annotations

import sys
from abc import ABC, abstractmethod
from collections import OrderedDict
from pathlib import Path
from typing import Any, Callable, Dict, Generic, Iterator, List, Optional, Set, TypeVar, Union, cast

from ..types import Command, EnvList

if sys.version_info >= (3, 8):  # pragma: no cover (py38+)
    from typing import Literal
else:  # pragma: no cover (py38+)
    from typing_extensions import Literal


_NO_MAPPING = object()
T = TypeVar("T")
V = TypeVar("V")

Factory = Optional[Callable[[object], T]]  # note the argument is anything, due e.g. memory loader can inject anything


class Convert(ABC, Generic[T]):
    """A class that converts a raw type to a given tox (python) type"""

    def to(self, raw: T, of_type: type[V], factory: Factory[V]) -> V:
        """
        Convert given raw type to python type

        :param raw: the raw type
        :param of_type: python type
        :param factory: factory method to build the object
        :return: the converted type
        """
        from_module = getattr(of_type, "__module__", None)
        if from_module in ("typing", "typing_extensions"):
            return self._to_typing(raw, of_type, factory)
        if issubclass(of_type, Path):
            return self.to_path(raw)  # type: ignore[return-value]
        if issubclass(of_type, bool):
            return self.to_bool(raw)  # type: ignore[return-value]
        if issubclass(of_type, Command):
            return self.to_command(raw)  # type: ignore[return-value]
        if issubclass(of_type, EnvList):
            return self.to_env_list(raw)  # type: ignore[return-value]
        if issubclass(of_type, str):
            return self.to_str(raw)  # type: ignore[return-value]
        if isinstance(raw, of_type):  # already target type no need to transform it
            # do it this late to allow normalization - e.g. string strip
            return raw
        if factory:
            return factory(raw)
        return of_type(raw)  # type: ignore[call-arg]

    def _to_typing(self, raw: T, of_type: type[V], factory: Factory[V]) -> V:
        origin = getattr(of_type, "__origin__", of_type.__class__)
        result: Any = _NO_MAPPING
        if origin in (list, List):
            entry_type = of_type.__args__[0]  # type: ignore[attr-defined]
            result = [self.to(i, entry_type, factory) for i in self.to_list(raw, entry_type)]
        elif origin in (set, Set):
            entry_type = of_type.__args__[0]  # type: ignore[attr-defined]
            result = {self.to(i, entry_type, factory) for i in self.to_set(raw, entry_type)}
        elif origin in (dict, Dict):
            key_type, value_type = of_type.__args__[0], of_type.__args__[1]  # type: ignore[attr-defined]
            result = OrderedDict(
                (self.to(k, key_type, factory), self.to(v, value_type, factory))
                for k, v in self.to_dict(raw, (key_type, value_type))
            )
        elif origin == Union:  # handle Optional values
            args: list[type[Any]] = of_type.__args__  # type: ignore[attr-defined]
            none = type(None)
            if len(args) == 2 and none in args:
                if isinstance(raw, str):
                    raw = raw.strip()  # type: ignore[assignment]
                if not raw:
                    result = None
                else:
                    new_type = next(i for i in args if i != none)  # pragma: no cover # this will always find a element
                    result = self.to(raw, new_type, factory)
        elif origin in (Literal, type(Literal)):
            choice = of_type.__args__  # type: ignore[attr-defined]
            if raw not in choice:
                raise ValueError(f"{raw} must be one of {choice}")
            result = raw
        if result is not _NO_MAPPING:
            return cast(V, result)
        raise TypeError(f"{raw} cannot cast to {of_type!r}")

    @staticmethod
    @abstractmethod
    def to_str(value: T) -> str:
        """
        Convert to string.

        :param value: the value to convert
        :returns: a string representation of the value
        """
        raise NotImplementedError

    @staticmethod
    @abstractmethod
    def to_bool(value: T) -> bool:
        """
        Convert to boolean.

        :param value: the value to convert
        :returns: a boolean representation of the value
        """
        raise NotImplementedError

    @staticmethod
    @abstractmethod
    def to_list(value: T, of_type: type[Any]) -> Iterator[T]:
        """
        Convert to list.

        :param value: the value to convert
        :param of_type: the type of elements in the list
        :returns: a list representation of the value
        """
        raise NotImplementedError

    @staticmethod
    @abstractmethod
    def to_set(value: T, of_type: type[Any]) -> Iterator[T]:
        """
        Convert to set.

        :param value: the value to convert
        :param of_type: the type of elements in the set
        :returns: a set representation of the value
        """
        raise NotImplementedError

    @staticmethod
    @abstractmethod
    def to_dict(value: T, of_type: tuple[type[Any], type[Any]]) -> Iterator[tuple[T, T]]:
        """
        Convert to dictionary.

        :param value: the value to convert
        :param of_type: a tuple indicating the type of the key and the value
        :returns: a iteration of key-value pairs that gets populated into a dict
        """
        raise NotImplementedError

    @staticmethod
    @abstractmethod
    def to_path(value: T) -> Path:
        """
        Convert to path.

        :param value: the value to convert
        :returns: path representation of the value
        """
        raise NotImplementedError

    @staticmethod
    @abstractmethod
    def to_command(value: T) -> Command:
        """
        Convert to a command to execute.

        :param value: the value to convert
        :returns: command representation of the value
        """
        raise NotImplementedError

    @staticmethod
    @abstractmethod
    def to_env_list(value: T) -> EnvList:
        """
        Convert to a tox EnvList.

        :param value: the value to convert
        :returns: a list of tox environments from the value
        """
        raise NotImplementedError


__all__ = [
    "Convert",
    "Factory",
]