1
|
|
|
# Copyright Pincer 2021-Present |
|
|
|
|
2
|
|
|
# Full MIT License can be found in `LICENSE` at the project root. |
3
|
|
|
|
4
|
|
|
from inspect import getfullargspec, Parameter, Signature |
5
|
|
|
from typing import Any, Union, Callable, Mapping, List |
6
|
|
|
|
7
|
|
|
from .types import Coro, TypeCache |
8
|
|
|
from ..objects.message.context import MessageContext |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
def should_pass_cls(call: Union[Coro, Callable[..., Any]]) -> bool: |
12
|
|
|
# TODO: fix docs |
|
|
|
|
13
|
|
|
""" |
14
|
|
|
Checks whether a callable requires a self/cls as first parameter. |
15
|
|
|
|
16
|
|
|
Parameters |
17
|
|
|
---------- |
18
|
|
|
call: Union[Coro, Callable[..., Any]] |
19
|
|
|
The callable to check. |
20
|
|
|
|
21
|
|
|
Returns |
22
|
|
|
------- |
23
|
|
|
bool |
24
|
|
|
Whether the callable requires a self/cls as first parameter. |
25
|
|
|
""" |
26
|
|
|
args = getfullargspec(call).args |
27
|
|
|
return len(args) >= 1 and args[0] in ["self", "cls"] |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def should_pass_gateway(call: Union[Coro, Callable[..., Any]]) -> bool: |
31
|
|
|
# TODO: fix docs |
|
|
|
|
32
|
|
|
""" |
33
|
|
|
Checks whether a callable requires a dispatcher as last parameter. |
34
|
|
|
|
35
|
|
|
Parameters |
36
|
|
|
---------- |
37
|
|
|
call: Union[:class:`~pincer.utils.types.Coro`, Callable[[...], Any]] |
38
|
|
|
The callable to check. |
39
|
|
|
|
40
|
|
|
Returns |
41
|
|
|
------- |
42
|
|
|
bool |
43
|
|
|
Whether the callable requires a dispatcher as first parameter. |
44
|
|
|
""" |
45
|
|
|
args = getfullargspec(call).args |
46
|
|
|
return len(args) >= 1 and args[1] in ("gateway", "shard") |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
context_types = [Signature.empty, MessageContext] |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def should_pass_ctx(sig: Mapping[str, Parameter], params: List[str]) -> bool: |
53
|
|
|
# TODO: fix docs |
|
|
|
|
54
|
|
|
""" |
55
|
|
|
|
56
|
|
|
Parameters |
57
|
|
|
---------- |
58
|
|
|
sig |
59
|
|
|
params |
60
|
|
|
|
61
|
|
|
Returns |
62
|
|
|
------- |
63
|
|
|
|
64
|
|
|
""" |
65
|
|
|
if not params: |
66
|
|
|
return False |
67
|
|
|
|
68
|
|
|
annotation = sig[params[0]].annotation |
69
|
|
|
if isinstance(annotation, str): |
70
|
|
|
TypeCache() |
71
|
|
|
annotation = eval(annotation, TypeCache.cache, globals()) |
|
|
|
|
72
|
|
|
|
73
|
|
|
return ( |
74
|
|
|
annotation == MessageContext |
75
|
|
|
or params[0] == "ctx" |
76
|
|
|
) |
77
|
|
|
|