|
1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
2
|
|
|
# MIT License |
|
3
|
|
|
# |
|
4
|
|
|
# Copyright (c) 2021 Pincer |
|
5
|
|
|
# |
|
6
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining |
|
7
|
|
|
# a copy of this software and associated documentation files |
|
8
|
|
|
# (the "Software"), to deal in the Software without restriction, |
|
9
|
|
|
# including without limitation the rights to use, copy, modify, merge, |
|
10
|
|
|
# publish, distribute, sublicense, and/or sell copies of the Software, |
|
11
|
|
|
# and to permit persons to whom the Software is furnished to do so, |
|
12
|
|
|
# subject to the following conditions: |
|
13
|
|
|
# |
|
14
|
|
|
# The above copyright notice and this permission notice shall be |
|
15
|
|
|
# included in all copies or substantial portions of the Software. |
|
16
|
|
|
# |
|
17
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
18
|
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
19
|
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
20
|
|
|
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
|
21
|
|
|
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
|
22
|
|
|
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
23
|
|
|
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
24
|
|
|
from inspect import signature |
|
25
|
|
|
from typing import Any, Optional, Protocol, TypeVar |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
from pincer.utils.insertion import should_pass_cls |
|
28
|
|
|
from pincer.utils.types import Coro |
|
29
|
|
|
|
|
30
|
|
|
T = TypeVar("T") |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
class GetItem(Protocol): |
|
34
|
|
|
"""Represents a class which implements the __getitem__ property.""" |
|
35
|
|
|
|
|
36
|
|
|
def __getitem__(self, key: int) -> Any: |
|
37
|
|
|
return ... |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
def get_index( |
|
41
|
|
|
collection: GetItem, |
|
42
|
|
|
index: int, |
|
43
|
|
|
fallback: Optional[T] = None |
|
44
|
|
|
) -> Optional[T]: |
|
45
|
|
|
""" |
|
46
|
|
|
Gets an item from a collection through index. |
|
47
|
|
|
Allows you to provide a fallback for if that index is out of bounds. |
|
48
|
|
|
|
|
49
|
|
|
:param collection: |
|
50
|
|
|
The collection from which the item is retrieved. |
|
51
|
|
|
|
|
52
|
|
|
:param index: |
|
53
|
|
|
The index of the item in the collection. |
|
54
|
|
|
|
|
55
|
|
|
:param fallback: |
|
56
|
|
|
The fallback value which will be used if the index doesn't |
|
57
|
|
|
exist. Default value is None. |
|
58
|
|
|
|
|
59
|
|
|
:return: |
|
60
|
|
|
The item at the provided index from the collection, or if that |
|
61
|
|
|
item doesn't exist it will return the fallback value. |
|
62
|
|
|
""" |
|
63
|
|
|
try: |
|
64
|
|
|
return collection[index] |
|
65
|
|
|
|
|
66
|
|
|
except IndexError: |
|
67
|
|
|
return fallback |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
def get_signature_and_params(func: Coro): |
|
71
|
|
|
""" |
|
72
|
|
|
Get the signature and its parameters from a coroutine. |
|
73
|
|
|
|
|
74
|
|
|
:param func: |
|
75
|
|
|
The coroutine from whom the information should be extracted. |
|
76
|
|
|
""" |
|
77
|
|
|
sig = signature(func).parameters |
|
78
|
|
|
params = list(sig) |
|
79
|
|
|
|
|
80
|
|
|
if should_pass_cls(func): |
|
81
|
|
|
del params[0] |
|
82
|
|
|
|
|
83
|
|
|
return sig, params |
|
84
|
|
|
|