@@ -147,7 +147,7 @@ def __init__(
147
147
148
148
self ._login_called : bool = False
149
149
self ._has_closed : bool = False
150
- self ._dump_tokens : bool = True
150
+ self ._save_tokens : bool = True
151
151
152
152
# Websockets for EventSub
153
153
self ._websockets : dict [str , dict [str , Websocket ]] = defaultdict (dict )
@@ -169,7 +169,7 @@ def tokens(self) -> MappingProxyType[str, TokenMappingData]:
169
169
170
170
:meth:`~.load_tokens`
171
171
172
- :meth:`~.dump_tokens `
172
+ :meth:`~.save_tokens `
173
173
174
174
:meth:`~.add_token`
175
175
@@ -257,7 +257,7 @@ async def setup_hook(self) -> None:
257
257
"""
258
258
...
259
259
260
- async def login (self , * , token : str | None = None , load_tokens : bool = True , dump_tokens : bool = True ) -> None :
260
+ async def login (self , * , token : str | None = None , load_tokens : bool = True , save_tokens : bool = True ) -> None :
261
261
"""Method to login the client and generate or store an app token.
262
262
263
263
This method is called automatically when using :meth:`~.start`.
@@ -277,15 +277,15 @@ async def login(self, *, token: str | None = None, load_tokens: bool = True, dum
277
277
load_tokens: bool
278
278
Optional bool which indicates whether the :class:`Client` should call :meth:`.load_tokens` during
279
279
login automatically. Defaults to ``True``.
280
- dump_tokens : bool
281
- Optional bool which inicates whether the :class:`Client` should call :meth:`.dump_tokens ` during the
280
+ save_tokens : bool
281
+ Optional bool which inicates whether the :class:`Client` should call :meth:`.save_tokens ` during the
282
282
:meth:`.close` automatically. Defaults to ``True``.
283
283
"""
284
284
if self ._login_called :
285
285
return
286
286
287
287
self ._login_called = True
288
- self ._dump_tokens = dump_tokens
288
+ self ._save_tokens = save_tokens
289
289
290
290
if not token :
291
291
payload : ClientCredentialsPayload = await self ._http .client_credentials_token ()
@@ -314,7 +314,7 @@ async def start(
314
314
* ,
315
315
with_adapter : bool = True ,
316
316
load_tokens : bool = True ,
317
- dump_tokens : bool = True ,
317
+ save_tokens : bool = True ,
318
318
) -> None :
319
319
"""|coro|
320
320
@@ -336,8 +336,8 @@ async def start(
336
336
load_tokens: bool
337
337
Optional bool which indicates whether the :class:`Client` should call :meth:`.load_tokens` during
338
338
:meth:`.login` automatically. Defaults to ``True``.
339
- dump_tokens : bool
340
- Optional bool which inicates whether the :class:`Client` should call :meth:`.dump_tokens ` during the
339
+ save_tokens : bool
340
+ Optional bool which inicates whether the :class:`Client` should call :meth:`.save_tokens ` during the
341
341
:meth:`.close` automatically. Defaults to ``True``.
342
342
343
343
Examples
@@ -356,7 +356,7 @@ async def main() -> None:
356
356
await client.start()
357
357
"""
358
358
self .__waiter .clear ()
359
- await self .login (token = token , load_tokens = load_tokens , dump_tokens = dump_tokens )
359
+ await self .login (token = token , load_tokens = load_tokens , save_tokens = save_tokens )
360
360
361
361
if with_adapter :
362
362
await self ._adapter .run ()
@@ -377,7 +377,7 @@ def run(
377
377
* ,
378
378
with_adapter : bool = True ,
379
379
load_tokens : bool = True ,
380
- dump_tokens : bool = True ,
380
+ save_tokens : bool = True ,
381
381
) -> None :
382
382
"""Method to login the client and create a continuously running event loop.
383
383
@@ -405,8 +405,8 @@ def run(
405
405
load_tokens: bool
406
406
Optional bool which indicates whether the :class:`Client` should call :meth:`.load_tokens` during
407
407
:meth:`.login` automatically. Defaults to ``True``.
408
- dump_tokens : bool
409
- Optional bool which inicates whether the :class:`Client` should call :meth:`.dump_tokens ` during the
408
+ save_tokens : bool
409
+ Optional bool which inicates whether the :class:`Client` should call :meth:`.save_tokens ` during the
410
410
:meth:`.close` automatically. Defaults to ``True``.
411
411
412
412
Examples
@@ -420,22 +420,30 @@ def run(
420
420
421
421
async def run () -> None :
422
422
async with self :
423
- await self .start (token = token , with_adapter = with_adapter , load_tokens = load_tokens , dump_tokens = dump_tokens )
423
+ await self .start (token = token , with_adapter = with_adapter , load_tokens = load_tokens , save_tokens = save_tokens )
424
424
425
425
try :
426
426
asyncio .run (run ())
427
427
except KeyboardInterrupt :
428
428
pass
429
429
430
- async def close (self ) -> None :
431
- """Method which closes the :class:`~Client` gracefully.
430
+ async def close (self , ** options : Any ) -> None :
431
+ r """Method which closes the :class:`~Client` gracefully.
432
432
433
433
This method is called for you automatically when using :meth:`~.run` or when using the client with the
434
434
async context-manager, E.g: `async with client:`
435
435
436
436
You can override this method to implement your own clean-up logic, however you should call `await super().close()`
437
437
when doing this.
438
438
439
+ Parameters
440
+ ----------
441
+ \*
442
+ save_tokens: bool | None
443
+ An optional bool override which allows overriding the identical keyword-argument set in either
444
+ :meth:`.run`, :meth:`.start` or :meth:`.login` to call the :meth:`.save_tokens` coroutine.
445
+ Defaults to ``None`` which won't override.
446
+
439
447
Examples
440
448
--------
441
449
@@ -462,9 +470,12 @@ async def close(self) -> None:
462
470
for socket in sockets :
463
471
await socket .close ()
464
472
465
- if self ._dump_tokens :
473
+ save_tokens = options .get ("save_tokens" )
474
+ save = save_tokens if save_tokens is not None else self ._save_tokens
475
+
476
+ if save :
466
477
async with self ._http ._token_lock :
467
- await self .dump_tokens ()
478
+ await self .save_tokens ()
468
479
469
480
self ._http .cleanup ()
470
481
self .__waiter .set ()
@@ -627,7 +638,7 @@ async def load_tokens(self, path: str | None = None, /) -> None:
627
638
You can override this method to implement your own token loading logic into the client, such as from a database.
628
639
629
640
By default this method loads tokens from a file named `".tio.tokens.json"` if it is present;
630
- always present if you use the default method of dumping tokens.
641
+ always present if you use the default method of saving tokens.
631
642
632
643
**However**, it is preferred you would override this function to load your tokens from a database,
633
644
as this has far less chance of being corrupted, damaged or lost.
@@ -655,19 +666,19 @@ async def load_tokens(self, path: str | None = None) -> None:
655
666
"""
656
667
await self ._http .load_tokens (name = path )
657
668
658
- async def dump_tokens (self , path : str | None = None , / ) -> None :
669
+ async def save_tokens (self , path : str | None = None , / ) -> None :
659
670
"""|coro|
660
671
661
- Method which dumps all the added OAuth tokens currently managed by this Client.
672
+ Method which saves all the added OAuth tokens currently managed by this Client.
662
673
663
674
.. note::
664
675
665
- This method is called by the client when it is gracefully closed and the ``dump_tokens `` keyword-argument
676
+ This method is called by the client when it is gracefully closed and the ``save_tokens `` keyword-argument
666
677
is ``True`` in either, :meth:`.run`, :meth:`.start` or :meth:`.login` (Default).
667
678
668
679
.. note::
669
680
670
- By default this method dumps to a JSON file named `".tio.tokens.json"`.
681
+ By default this method saves to a JSON file named `".tio.tokens.json"`.
671
682
672
683
You can override this method to implement your own custom logic, such as saving tokens to a database, however
673
684
it is preferred to use :meth:`~.add_token` to ensure the tokens are handled as they are added.
@@ -677,7 +688,7 @@ async def dump_tokens(self, path: str | None = None, /) -> None:
677
688
path: str | None
678
689
The path of the file to save to. Defaults to `.tio.tokens.json`.
679
690
"""
680
- await self ._http .dump (path )
691
+ await self ._http .save (path )
681
692
682
693
def add_listener (self , listener : Callable [..., Coroutine [Any , Any , None ]], * , event : str | None = None ) -> None :
683
694
# TODO: Docs...
0 commit comments