1
|
|
|
# Copyright Pincer 2021-Present |
|
|
|
|
2
|
|
|
# Full MIT License can be found in `LICENSE` at the project root. |
3
|
|
|
|
4
|
|
|
from __future__ import annotations |
5
|
|
|
|
6
|
|
|
from inspect import getfullargspec |
7
|
|
|
from typing import TYPE_CHECKING |
8
|
|
|
|
9
|
|
|
from .types import T, MISSING |
10
|
|
|
|
11
|
|
|
if TYPE_CHECKING: |
12
|
|
|
from ..client import Client |
|
|
|
|
13
|
|
|
from typing import Any, Callable, Dict, List, Optional, Set, Union |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
def construct_client_dict(client: Client, data: Dict) -> Dict: |
17
|
|
|
# TODO: fix docs |
|
|
|
|
18
|
|
|
""" |
19
|
|
|
|
20
|
|
|
Parameters |
21
|
|
|
---------- |
22
|
|
|
client |
23
|
|
|
data |
24
|
|
|
|
25
|
|
|
Returns |
26
|
|
|
------- |
27
|
|
|
|
28
|
|
|
""" |
29
|
|
|
return {**data, "_client": client, "_http": client.http} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
def convert( |
33
|
|
|
value: Any, |
|
|
|
|
34
|
|
|
factory: Callable[[Any], T], |
|
|
|
|
35
|
|
|
check: Optional[T] = None, |
|
|
|
|
36
|
|
|
client: Optional[Client] = None, |
|
|
|
|
37
|
|
|
) -> T: |
38
|
|
|
""" |
39
|
|
|
Parameters |
40
|
|
|
---------- |
41
|
|
|
value : Any |
42
|
|
|
The value that has to have its type converted. |
43
|
|
|
factory : Callable[[Any], T] |
44
|
|
|
The conversion factory/object to use. |
45
|
|
|
check : Optional[T] |
46
|
|
|
Skip conversion if ``value`` is already this type. |
47
|
|
|
client : Optional[:class:`~pincer.client.Client`] |
48
|
|
|
Reference to :class:`~pincer.client.Client` |
49
|
|
|
""" |
50
|
|
|
def handle_factory() -> T: |
51
|
|
|
if check is not None and isinstance(value, check): |
52
|
|
|
return value |
53
|
|
|
|
54
|
|
|
try: |
55
|
|
|
if client and "_client" in getfullargspec(factory).args: |
56
|
|
|
return factory(construct_client_dict(client, value)) |
57
|
|
|
except TypeError: # Building type/has no signature |
58
|
|
|
pass |
59
|
|
|
|
60
|
|
|
return factory(value) |
61
|
|
|
|
62
|
|
|
return MISSING if value is MISSING else handle_factory() |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
def remove_none(obj: Union[List, Dict, Set]) -> Union[List, Dict, Set]: |
|
|
|
|
66
|
|
|
""" |
67
|
|
|
Removes all ``None`` values from a list, dict or set. |
68
|
|
|
|
69
|
|
|
Parameters |
70
|
|
|
---------- |
71
|
|
|
obj : Union[List, Dict, Set] |
72
|
|
|
The list, dict or set to remove ``None`` values from. |
73
|
|
|
|
74
|
|
|
Returns |
75
|
|
|
------- |
76
|
|
|
Union[List, Dict, Set] |
77
|
|
|
The list, dict or set without ``None`` values. |
78
|
|
|
""" |
79
|
|
|
if isinstance(obj, list): |
|
|
|
|
80
|
|
|
return [i for i in obj if i is not None] |
81
|
|
|
elif isinstance(obj, set): |
82
|
|
|
return obj - {None} |
83
|
|
|
elif isinstance(obj, dict): |
84
|
|
|
return {k: v for k, v in obj.items() if None not in {k, v}} |
|
|
|
|