mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-12-01 22:03:46 +03:00
Remove custom enum parsing
Python enums can already be parsed out-of-the-box.
This commit is contained in:
parent
8c9ee3f731
commit
a95393648f
|
@ -69,21 +69,21 @@ class _ChatAction:
|
|||
return action
|
||||
|
||||
return {
|
||||
enums.TYPING: _tl.SendMessageTypingAction(),
|
||||
enums.CONTACT: _tl.SendMessageChooseContactAction(),
|
||||
enums.GAME: _tl.SendMessageGamePlayAction(),
|
||||
enums.LOCATION: _tl.SendMessageGeoLocationAction(),
|
||||
enums.STICKER: _tl.SendMessageChooseStickerAction(),
|
||||
enums.RECORD_AUDIO: _tl.SendMessageRecordAudioAction(),
|
||||
enums.RECORD_ROUND: _tl.SendMessageRecordRoundAction(),
|
||||
enums.RECORD_VIDEO: _tl.SendMessageRecordVideoAction(),
|
||||
enums.AUDIO: _tl.SendMessageUploadAudioAction(1),
|
||||
enums.ROUND: _tl.SendMessageUploadRoundAction(1),
|
||||
enums.VIDEO: _tl.SendMessageUploadVideoAction(1),
|
||||
enums.PHOTO: _tl.SendMessageUploadPhotoAction(1),
|
||||
enums.DOCUMENT: _tl.SendMessageUploadDocumentAction(1),
|
||||
enums.CANCEL: _tl.SendMessageCancelAction(),
|
||||
}[enums.parse_typing_action(action)]
|
||||
enums.Action.TYPING: _tl.SendMessageTypingAction(),
|
||||
enums.Action.CONTACT: _tl.SendMessageChooseContactAction(),
|
||||
enums.Action.GAME: _tl.SendMessageGamePlayAction(),
|
||||
enums.Action.LOCATION: _tl.SendMessageGeoLocationAction(),
|
||||
enums.Action.STICKER: _tl.SendMessageChooseStickerAction(),
|
||||
enums.Action.RECORD_AUDIO: _tl.SendMessageRecordAudioAction(),
|
||||
enums.Action.RECORD_ROUND: _tl.SendMessageRecordRoundAction(),
|
||||
enums.Action.RECORD_VIDEO: _tl.SendMessageRecordVideoAction(),
|
||||
enums.Action.AUDIO: _tl.SendMessageUploadAudioAction(1),
|
||||
enums.Action.ROUND: _tl.SendMessageUploadRoundAction(1),
|
||||
enums.Action.VIDEO: _tl.SendMessageUploadVideoAction(1),
|
||||
enums.Action.PHOTO: _tl.SendMessageUploadPhotoAction(1),
|
||||
enums.Action.DOCUMENT: _tl.SendMessageUploadDocumentAction(1),
|
||||
enums.Action.CANCEL: _tl.SendMessageCancelAction(),
|
||||
}[enums.Action(action)]
|
||||
|
||||
def progress(self, current, total):
|
||||
if hasattr(self._action, 'progress'):
|
||||
|
@ -98,7 +98,7 @@ class _ParticipantsIter(requestiter.RequestIter):
|
|||
else:
|
||||
filter = _tl.ChannelParticipantsRecent()
|
||||
else:
|
||||
filter = enums.parse_participant(filter)
|
||||
filter = enums.Participant(filter)
|
||||
if filter == enums.Participant.ADMIN:
|
||||
filter = _tl.ChannelParticipantsAdmins()
|
||||
elif filter == enums.Participant.BOT:
|
||||
|
|
|
@ -210,7 +210,7 @@ async def download_profile_photo(
|
|||
photo = entity.photo
|
||||
|
||||
if isinstance(photo, (_tl.UserProfilePhoto, _tl.ChatPhoto)):
|
||||
thumb = enums.Size.ORIGINAL if thumb == () else enums.parse_photo_size(thumb)
|
||||
thumb = enums.Size.ORIGINAL if thumb == () else enums.Size(thumb)
|
||||
|
||||
dc_id = photo.dc_id
|
||||
loc = _tl.InputPeerPhotoFileLocation(
|
||||
|
@ -494,11 +494,11 @@ def _get_thumb(thumbs, thumb):
|
|||
if isinstance(thumb, tlobject.TLObject):
|
||||
return thumb
|
||||
|
||||
thumb = enums.parse_photo_size(thumb)
|
||||
thumb = enums.Size(thumb)
|
||||
return min(
|
||||
thumbs,
|
||||
default=None,
|
||||
key=lambda t: abs(thumb - enums.parse_photo_size(t.type))
|
||||
key=lambda t: abs(thumb - enums.Size(t.type))
|
||||
)
|
||||
|
||||
def _download_cached_photo_size(self: 'TelegramClient', size, file):
|
||||
|
|
|
@ -199,7 +199,7 @@ def init(
|
|||
enums.ConnectionMode.FULL: transports.Full(),
|
||||
enums.ConnectionMode.INTERMEDIATE: transports.Intermediate(),
|
||||
enums.ConnectionMode.ABRIDGED: transports.Abridged(),
|
||||
}[enums.parse_conn_mode(connection)]
|
||||
}[enums.ConnectionMode(connection)]
|
||||
init_proxy = None
|
||||
|
||||
# Used on connection. Capture the variables in a lambda since
|
||||
|
|
|
@ -129,25 +129,3 @@ class Size(Enum):
|
|||
Size.ANIMATED: 7,
|
||||
Size.VIDEO: 6,
|
||||
}[self]
|
||||
|
||||
|
||||
def _mk_parser(cls):
|
||||
def parser(value):
|
||||
if isinstance(value, cls):
|
||||
return value
|
||||
elif isinstance(value, str):
|
||||
for variant in cls:
|
||||
if value == variant.value:
|
||||
return variant
|
||||
|
||||
raise ValueError(f'unknown {cls.__name__}: {value!r}')
|
||||
else:
|
||||
raise TypeError(f'not a valid {cls.__name__}: {type(value).__name__!r}')
|
||||
|
||||
return parser
|
||||
|
||||
|
||||
parse_conn_mode = _mk_parser(ConnectionMode)
|
||||
parse_participant = _mk_parser(Participant)
|
||||
parse_typing_action = _mk_parser(Action)
|
||||
parse_photo_size = _mk_parser(Size)
|
||||
|
|
Loading…
Reference in New Issue
Block a user