1
1
from asyncio import sleep
2
2
from enum import Enum
3
- from inspect import isawaitable , isfunction
3
+ from inspect import isawaitable
4
4
from logging import getLogger
5
- from typing import Coroutine , Iterable , List , Optional , Type , TypeVar , Union , get_args
5
+ from typing import Coroutine , List , Optional , Type , TypeVar , Union , get_args
6
6
7
7
try :
8
8
from typing import _GenericAlias
@@ -43,7 +43,7 @@ class Force(str, Enum):
43
43
HTTP = "http"
44
44
45
45
46
- def get (* args , ** kwargs ):
46
+ def get (client : Client , obj : Type [ _T ], ** kwargs ) -> Optional [ _T ] :
47
47
"""
48
48
Helper method to get an object.
49
49
@@ -56,17 +56,11 @@ def get(*args, **kwargs):
56
56
* purely from cache
57
57
* purely from HTTP
58
58
* from HTTP if not found in cache else from cache
59
- * Get an object from an iterable
60
- * based of its name
61
- * based of its ID
62
- * based of a custom check
63
- * based of any other attribute the object inside the iterable has
64
59
65
60
The method has to be awaited when:
66
61
* You don't force anything
67
62
* You force HTTP
68
63
The method doesn't have to be awaited when:
69
- * You get from an iterable
70
64
* You force cache
71
65
72
66
.. note ::
@@ -93,22 +87,6 @@ def get(*args, **kwargs):
93
87
enforce HTTP. To prevent this bug from happening it is suggested using ``force="http"`` instead of
94
88
the enum.
95
89
96
- Getting from an iterable:
97
-
98
- .. code-block:: python
99
-
100
- # Getting an object from an iterable
101
- check = lambda role: role.name == "ADMIN" and role.color == 0xff0000
102
- roles = [
103
- interactions.Role(name="NOT ADMIN", color=0xff0000),
104
- interactions.Role(name="ADMIN", color=0xff0000),
105
- ]
106
- role = get(roles, check=check)
107
- # role will be `interactions.Role(name="ADMIN", color=0xff0000)`
108
-
109
- You can specify *any* attribute to check that the object could have (although only ``check``, ``id`` and
110
- ``name`` are type-hinted) and the method will check for a match.
111
-
112
90
Getting an object:
113
91
114
92
Here you will see two examples on how to get a single objects and the variations of how the object can be
@@ -198,81 +176,68 @@ def _check():
198
176
def _check ():
199
177
return False
200
178
201
- if len (args ) == 2 :
179
+ if not isinstance (obj , type ) and not isinstance (obj , _GenericAlias ):
180
+ client : Client
181
+ obj : Union [Type [_T ], Type [List [_T ]]]
182
+ raise LibraryException (message = "The object must not be an instance of a class!" , code = 12 )
202
183
203
- if any (isinstance (_ , Iterable ) for _ in args ):
204
- raise LibraryException (
205
- message = "You can only use Iterables as single-argument!" , code = 12
206
- )
184
+ kwargs = _resolve_kwargs (obj , ** kwargs )
185
+ http_name = f"get_{ obj .__name__ .lower ()} "
186
+ kwarg_name = f"{ obj .__name__ .lower ()} _id"
187
+ if isinstance (obj , _GenericAlias ) or _check ():
188
+ _obj : Type [_T ] = get_args (obj )[0 ]
189
+ _objects : List [Union [_obj , Coroutine ]] = []
190
+ kwarg_name += "s"
207
191
208
- client , obj = args
209
- if not isinstance (obj , type ) and not isinstance (obj , _GenericAlias ):
210
- client : Client
211
- obj : Union [Type [_T ], Type [List [_T ]]]
212
- raise LibraryException (
213
- message = "The object must not be an instance of a class!" , code = 12
214
- )
215
-
216
- kwargs = _resolve_kwargs (obj , ** kwargs )
217
- http_name = f"get_{ obj .__name__ .lower ()} "
218
- kwarg_name = f"{ obj .__name__ .lower ()} _id"
219
- if isinstance (obj , _GenericAlias ) or _check ():
220
- _obj : Type [_T ] = get_args (obj )[0 ]
221
- _objects : List [Union [_obj , Coroutine ]] = []
222
- kwarg_name += "s"
192
+ force_cache = kwargs .pop ("force" , None ) == "cache"
193
+ force_http = kwargs .pop ("force" , None ) == "http"
223
194
224
- force_cache = kwargs . pop ( "force" , None ) == "cache"
225
- force_http = kwargs . pop ( "force" , None ) == "http"
195
+ if not force_http :
196
+ _objects = _get_cache ( _obj , client , kwarg_name , _list = True , ** kwargs )
226
197
227
- if not force_http :
228
- _objects = _get_cache ( _obj , client , kwarg_name , _list = True , ** kwargs )
198
+ if force_cache :
199
+ return _objects
229
200
230
- if force_cache :
231
- return _objects
201
+ elif not force_http and None not in _objects :
202
+ return _return_cache ( _objects )
232
203
233
- elif not force_http and None not in _objects :
234
- return _return_cache (_objects )
204
+ elif force_http :
205
+ _objects .clear ()
206
+ _func = getattr (client ._http , http_name )
207
+ for _id in kwargs .get (kwarg_name ):
208
+ _kwargs = kwargs
209
+ _kwargs .pop (kwarg_name )
210
+ _kwargs [kwarg_name [:- 1 ]] = _id
211
+ _objects .append (_func (** _kwargs ))
212
+ return _http_request (_obj , http = client ._http , request = _objects )
235
213
236
- elif force_http :
237
- _objects .clear ()
238
- _func = getattr (client ._http , http_name )
239
- for _id in kwargs .get (kwarg_name ):
214
+ else :
215
+ _func = getattr (client ._http , http_name )
216
+ for _index , __obj in enumerate (_objects ):
217
+ if __obj is None :
218
+ _id = kwargs .get (kwarg_name )[_index ]
240
219
_kwargs = kwargs
241
220
_kwargs .pop (kwarg_name )
242
221
_kwargs [kwarg_name [:- 1 ]] = _id
243
- _objects .append (_func (** _kwargs ))
244
- return _http_request (_obj , http = client ._http , request = _objects )
245
-
246
- else :
247
- _func = getattr (client ._http , http_name )
248
- for _index , __obj in enumerate (_objects ):
249
- if __obj is None :
250
- _id = kwargs .get (kwarg_name )[_index ]
251
- _kwargs = kwargs
252
- _kwargs .pop (kwarg_name )
253
- _kwargs [kwarg_name [:- 1 ]] = _id
254
- _request = _func (** _kwargs )
255
- _objects [_index ] = _request
256
- return _http_request (_obj , http = client ._http , request = _objects )
257
-
258
- _obj : Optional [_T ] = None
222
+ _request = _func (** _kwargs )
223
+ _objects [_index ] = _request
224
+ return _http_request (_obj , http = client ._http , request = _objects )
259
225
260
- force_cache = kwargs .pop ("force" , None ) == "cache"
261
- force_http = kwargs .pop ("force" , None ) == "http"
262
- if not force_http :
263
- _obj = _get_cache (obj , client , kwarg_name , ** kwargs )
226
+ _obj : Optional [_T ] = None
264
227
265
- if force_cache :
266
- return _obj
228
+ force_cache = kwargs .pop ("force" , None ) == "cache"
229
+ force_http = kwargs .pop ("force" , None ) == "http"
230
+ if not force_http :
231
+ _obj = _get_cache (obj , client , kwarg_name , ** kwargs )
267
232
268
- elif not force_http and _obj :
269
- return _return_cache ( _obj )
233
+ if force_cache :
234
+ return _obj
270
235
271
- else :
272
- return _http_request ( obj = obj , http = client . _http , _name = http_name , ** kwargs )
236
+ elif not force_http and _obj :
237
+ return _return_cache ( _obj )
273
238
274
- elif len ( args ) == 1 :
275
- return _search_iterable ( * args , ** kwargs )
239
+ else :
240
+ return _http_request ( obj = obj , http = client . _http , _name = http_name , ** kwargs )
276
241
277
242
278
243
async def _http_request (
@@ -339,40 +304,6 @@ def _get_cache(
339
304
return _obj
340
305
341
306
342
- def _search_iterable (items : Iterable [_T ], ** kwargs ) -> Optional [_T ]:
343
- if not isinstance (items , Iterable ):
344
- raise LibraryException (message = "The specified items must be an iterable!" , code = 12 )
345
-
346
- if not kwargs :
347
- raise LibraryException (
348
- message = "You have to specify either a custom check or a keyword argument to check against!" ,
349
- code = 12 ,
350
- )
351
-
352
- if len (list (kwargs )) > 1 :
353
- raise LibraryException (
354
- message = "Only one keyword argument to check against is allowed!" , code = 12
355
- )
356
-
357
- _arg = str (list (kwargs )[0 ])
358
- kwarg = kwargs .get (_arg )
359
- kwarg_is_function : bool = isfunction (kwarg )
360
-
361
- __obj = next (
362
- (
363
- item
364
- for item in items
365
- if (
366
- str (getattr (item , _arg , None )) == str (kwarg )
367
- if not kwarg_is_function
368
- else kwarg (item )
369
- )
370
- ),
371
- None ,
372
- )
373
- return __obj
374
-
375
-
376
307
def _resolve_kwargs (obj , ** kwargs ):
377
308
# This function is needed to get correct kwarg names
378
309
if __id := kwargs .pop ("parent_id" , None ):
0 commit comments