Skip to content

Commit cc61f6f

Browse files
committed
Change dump_tokens to save_tokens; add override to Client.close()
1 parent 0493b7f commit cc61f6f

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

twitchio/authentication/tokens.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ async def close(self) -> None:
297297
await super().close()
298298
await self.__isolated.close()
299299

300-
async def dump(self, name: str | None = None) -> None:
300+
async def save(self, name: str | None = None) -> None:
301301
if not self._has_loaded:
302302
return
303303

twitchio/client.py

+35-24
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def __init__(
147147

148148
self._login_called: bool = False
149149
self._has_closed: bool = False
150-
self._dump_tokens: bool = True
150+
self._save_tokens: bool = True
151151

152152
# Websockets for EventSub
153153
self._websockets: dict[str, dict[str, Websocket]] = defaultdict(dict)
@@ -169,7 +169,7 @@ def tokens(self) -> MappingProxyType[str, TokenMappingData]:
169169
170170
:meth:`~.load_tokens`
171171
172-
:meth:`~.dump_tokens`
172+
:meth:`~.save_tokens`
173173
174174
:meth:`~.add_token`
175175
@@ -257,7 +257,7 @@ async def setup_hook(self) -> None:
257257
"""
258258
...
259259

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:
261261
"""Method to login the client and generate or store an app token.
262262
263263
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
277277
load_tokens: bool
278278
Optional bool which indicates whether the :class:`Client` should call :meth:`.load_tokens` during
279279
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
282282
:meth:`.close` automatically. Defaults to ``True``.
283283
"""
284284
if self._login_called:
285285
return
286286

287287
self._login_called = True
288-
self._dump_tokens = dump_tokens
288+
self._save_tokens = save_tokens
289289

290290
if not token:
291291
payload: ClientCredentialsPayload = await self._http.client_credentials_token()
@@ -314,7 +314,7 @@ async def start(
314314
*,
315315
with_adapter: bool = True,
316316
load_tokens: bool = True,
317-
dump_tokens: bool = True,
317+
save_tokens: bool = True,
318318
) -> None:
319319
"""|coro|
320320
@@ -336,8 +336,8 @@ async def start(
336336
load_tokens: bool
337337
Optional bool which indicates whether the :class:`Client` should call :meth:`.load_tokens` during
338338
: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
341341
:meth:`.close` automatically. Defaults to ``True``.
342342
343343
Examples
@@ -356,7 +356,7 @@ async def main() -> None:
356356
await client.start()
357357
"""
358358
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)
360360

361361
if with_adapter:
362362
await self._adapter.run()
@@ -377,7 +377,7 @@ def run(
377377
*,
378378
with_adapter: bool = True,
379379
load_tokens: bool = True,
380-
dump_tokens: bool = True,
380+
save_tokens: bool = True,
381381
) -> None:
382382
"""Method to login the client and create a continuously running event loop.
383383
@@ -405,8 +405,8 @@ def run(
405405
load_tokens: bool
406406
Optional bool which indicates whether the :class:`Client` should call :meth:`.load_tokens` during
407407
: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
410410
:meth:`.close` automatically. Defaults to ``True``.
411411
412412
Examples
@@ -420,22 +420,30 @@ def run(
420420

421421
async def run() -> None:
422422
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)
424424

425425
try:
426426
asyncio.run(run())
427427
except KeyboardInterrupt:
428428
pass
429429

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.
432432
433433
This method is called for you automatically when using :meth:`~.run` or when using the client with the
434434
async context-manager, E.g: `async with client:`
435435
436436
You can override this method to implement your own clean-up logic, however you should call `await super().close()`
437437
when doing this.
438438
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+
439447
Examples
440448
--------
441449
@@ -462,9 +470,12 @@ async def close(self) -> None:
462470
for socket in sockets:
463471
await socket.close()
464472

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:
466477
async with self._http._token_lock:
467-
await self.dump_tokens()
478+
await self.save_tokens()
468479

469480
self._http.cleanup()
470481
self.__waiter.set()
@@ -627,7 +638,7 @@ async def load_tokens(self, path: str | None = None, /) -> None:
627638
You can override this method to implement your own token loading logic into the client, such as from a database.
628639
629640
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.
631642
632643
**However**, it is preferred you would override this function to load your tokens from a database,
633644
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:
655666
"""
656667
await self._http.load_tokens(name=path)
657668

658-
async def dump_tokens(self, path: str | None = None, /) -> None:
669+
async def save_tokens(self, path: str | None = None, /) -> None:
659670
"""|coro|
660671
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.
662673
663674
.. note::
664675
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
666677
is ``True`` in either, :meth:`.run`, :meth:`.start` or :meth:`.login` (Default).
667678
668679
.. note::
669680
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"`.
671682
672683
You can override this method to implement your own custom logic, such as saving tokens to a database, however
673684
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:
677688
path: str | None
678689
The path of the file to save to. Defaults to `.tio.tokens.json`.
679690
"""
680-
await self._http.dump(path)
691+
await self._http.save(path)
681692

682693
def add_listener(self, listener: Callable[..., Coroutine[Any, Any, None]], *, event: str | None = None) -> None:
683694
# TODO: Docs...

twitchio/ext/commands/bot.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def owner_id(self) -> str | None:
204204
"""
205205
return self._owner_id
206206

207-
async def close(self) -> None:
207+
async def close(self, **options: Any) -> None:
208208
for module in tuple(self.__modules):
209209
try:
210210
await self.unload_module(module)
@@ -217,7 +217,7 @@ async def close(self) -> None:
217217
except Exception as e:
218218
logger.debug('Failed to remove component "%s" gracefully during close: %s.', component, e)
219219

220-
await super().close()
220+
await super().close(**options)
221221

222222
def _cleanup_component(self, component: Component, /) -> None:
223223
for command in component.__all_commands__.values():

0 commit comments

Comments
 (0)