|
21 | 21 | # flake8: noqa: E501 |
22 | 22 | # pylint: disable=line-too-long |
23 | 23 | """This module contains the Builder classes for the telegram.ext module.""" |
| 24 | +from pathlib import Path |
24 | 25 | from queue import Queue |
25 | 26 | from threading import Event |
26 | 27 | from typing import ( |
|
38 | 39 |
|
39 | 40 | from telegram import Bot |
40 | 41 | from telegram.request import Request |
41 | | -from telegram._utils.types import ODVInput, DVInput |
| 42 | +from telegram._utils.types import ODVInput, DVInput, FilePathInput |
42 | 43 | from telegram._utils.warnings import warn |
43 | 44 | from telegram._utils.defaultvalue import DEFAULT_NONE, DefaultValue, DEFAULT_FALSE |
44 | 45 | from telegram.ext import Dispatcher, JobQueue, Updater, ExtBot, ContextTypes, CallbackContext |
@@ -349,14 +350,23 @@ def _set_request(self: BuilderType, request: Request) -> BuilderType: |
349 | 350 | return self |
350 | 351 |
|
351 | 352 | def _set_private_key( |
352 | | - self: BuilderType, private_key: bytes, password: bytes = None |
| 353 | + self: BuilderType, |
| 354 | + private_key: Union[bytes, FilePathInput], |
| 355 | + password: Union[bytes, FilePathInput] = None, |
353 | 356 | ) -> BuilderType: |
354 | 357 | if self._bot is not DEFAULT_NONE: |
355 | 358 | raise RuntimeError(_TWO_ARGS_REQ.format('private_key', 'bot instance')) |
356 | 359 | if self._dispatcher_check: |
357 | 360 | raise RuntimeError(_TWO_ARGS_REQ.format('private_key', 'Dispatcher instance')) |
358 | | - self._private_key = private_key |
359 | | - self._private_key_password = password |
| 361 | + |
| 362 | + self._private_key = ( |
| 363 | + private_key if isinstance(private_key, bytes) else Path(private_key).read_bytes() |
| 364 | + ) |
| 365 | + if password is None or isinstance(password, bytes): |
| 366 | + self._private_key_password = password |
| 367 | + else: |
| 368 | + self._private_key_password = Path(password).read_bytes() |
| 369 | + |
360 | 370 | return self |
361 | 371 |
|
362 | 372 | def _set_defaults(self: BuilderType, defaults: 'Defaults') -> BuilderType: |
@@ -608,16 +618,24 @@ def request(self: BuilderType, request: Request) -> BuilderType: |
608 | 618 | """ |
609 | 619 | return self._set_request(request) |
610 | 620 |
|
611 | | - def private_key(self: BuilderType, private_key: bytes, password: bytes = None) -> BuilderType: |
| 621 | + def private_key( |
| 622 | + self: BuilderType, |
| 623 | + private_key: Union[bytes, FilePathInput], |
| 624 | + password: Union[bytes, FilePathInput] = None, |
| 625 | + ) -> BuilderType: |
612 | 626 | """Sets the private key and corresponding password for decryption of telegram passport data |
613 | 627 | to be used for :attr:`telegram.ext.Dispatcher.bot`. |
614 | 628 |
|
615 | 629 | .. seealso:: `passportbot.py <https://github.com/python-telegram-bot/python-telegram-bot\ |
616 | 630 | /tree/master/examples#passportbotpy>`_, `Telegram Passports <https://git.io/fAvYd>`_ |
617 | 631 |
|
618 | 632 | Args: |
619 | | - private_key (:obj:`bytes`): The private key. |
620 | | - password (:obj:`bytes`): Optional. The corresponding password. |
| 633 | + private_key (:obj:`bytes` | :obj:`str` | :obj:`pathlib.Path`): The private key or the |
| 634 | + file path of a file that contains the key. In the latter case, the file's content |
| 635 | + will be read automatically. |
| 636 | + password (:obj:`bytes` | :obj:`str` | :obj:`pathlib.Path`, optional): The corresponding |
| 637 | + password or the file path of a file that contains the password. In the latter case, |
| 638 | + the file's content will be read automatically. |
621 | 639 |
|
622 | 640 | Returns: |
623 | 641 | :class:`DispatcherBuilder`: The same builder with the updated argument. |
@@ -958,16 +976,24 @@ def request(self: BuilderType, request: Request) -> BuilderType: |
958 | 976 | """ |
959 | 977 | return self._set_request(request) |
960 | 978 |
|
961 | | - def private_key(self: BuilderType, private_key: bytes, password: bytes = None) -> BuilderType: |
| 979 | + def private_key( |
| 980 | + self: BuilderType, |
| 981 | + private_key: Union[bytes, FilePathInput], |
| 982 | + password: Union[bytes, FilePathInput] = None, |
| 983 | + ) -> BuilderType: |
962 | 984 | """Sets the private key and corresponding password for decryption of telegram passport data |
963 | 985 | to be used for :attr:`telegram.ext.Updater.bot`. |
964 | 986 |
|
965 | 987 | .. seealso:: `passportbot.py <https://github.com/python-telegram-bot/python-telegram-bot\ |
966 | 988 | /tree/master/examples#passportbotpy>`_, `Telegram Passports <https://git.io/fAvYd>`_ |
967 | 989 |
|
968 | 990 | Args: |
969 | | - private_key (:obj:`bytes`): The private key. |
970 | | - password (:obj:`bytes`): Optional. The corresponding password. |
| 991 | + private_key (:obj:`bytes` | :obj:`str` | :obj:`pathlib.Path`): The private key or the |
| 992 | + file path of a file that contains the key. In the latter case, the file's content |
| 993 | + will be read automatically. |
| 994 | + password (:obj:`bytes` | :obj:`str` | :obj:`pathlib.Path`, optional): The corresponding |
| 995 | + password or the file path of a file that contains the password. In the latter case, |
| 996 | + the file's content will be read automatically. |
971 | 997 |
|
972 | 998 | Returns: |
973 | 999 | :class:`UpdaterBuilder`: The same builder with the updated argument. |
|
0 commit comments