mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-22 17:36:34 +03:00
Improve .get_input_peer and use it only when creating requests*
This avoids cyclic dependencies, so types requiring an InputPeer as a parameter will NOT convert faulty types to the right ones.
This commit is contained in:
parent
b8e46446ba
commit
86d45cc276
|
@ -5,9 +5,10 @@ to convert between an entity like an User, Chat, etc. into its Input version)
|
||||||
from mimetypes import add_type, guess_extension
|
from mimetypes import add_type, guess_extension
|
||||||
|
|
||||||
from .tl.types import (
|
from .tl.types import (
|
||||||
Channel, Chat, ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser,
|
Channel, ChannelForbidden, Chat, ChatEmpty, ChatForbidden, ChatFull,
|
||||||
MessageMediaDocument, MessageMediaPhoto, PeerChannel, PeerChat, PeerUser,
|
ChatPhoto, InputPeerChannel, InputPeerChat, InputPeerUser, InputPeerEmpty,
|
||||||
User, UserProfilePhoto)
|
InputPeerSelf, MessageMediaDocument, MessageMediaPhoto, PeerChannel,
|
||||||
|
PeerChat, PeerUser, User, UserFull, UserProfilePhoto)
|
||||||
|
|
||||||
|
|
||||||
def get_display_name(entity):
|
def get_display_name(entity):
|
||||||
|
@ -46,18 +47,29 @@ def get_extension(media):
|
||||||
def get_input_peer(entity):
|
def get_input_peer(entity):
|
||||||
"""Gets the input peer for the given "entity" (user, chat or channel).
|
"""Gets the input peer for the given "entity" (user, chat or channel).
|
||||||
A ValueError is raised if the given entity isn't a supported type."""
|
A ValueError is raised if the given entity isn't a supported type."""
|
||||||
if (isinstance(entity, InputPeerUser) or
|
if any(isinstance(entity, c) for c in (
|
||||||
isinstance(entity, InputPeerChat) or
|
InputPeerUser, InputPeerChat, InputPeerChannel,
|
||||||
isinstance(entity, InputPeerChannel)):
|
InputPeerSelf, InputPeerEmpty)):
|
||||||
return entity
|
return entity
|
||||||
|
|
||||||
if isinstance(entity, User):
|
if isinstance(entity, User):
|
||||||
return InputPeerUser(entity.id, entity.access_hash)
|
return InputPeerUser(entity.id, entity.access_hash)
|
||||||
if isinstance(entity, Chat):
|
|
||||||
|
if any(isinstance(entity, c) for c in (
|
||||||
|
Chat, ChatEmpty, ChatForbidden)):
|
||||||
return InputPeerChat(entity.id)
|
return InputPeerChat(entity.id)
|
||||||
if isinstance(entity, Channel):
|
|
||||||
|
if any(isinstance(entity, c) for c in (
|
||||||
|
Channel, ChannelForbidden)):
|
||||||
return InputPeerChannel(entity.id, entity.access_hash)
|
return InputPeerChannel(entity.id, entity.access_hash)
|
||||||
|
|
||||||
|
# Less common cases
|
||||||
|
if isinstance(entity, UserFull):
|
||||||
|
return InputPeerUser(entity.user.id, entity.user.access_hash)
|
||||||
|
|
||||||
|
if isinstance(entity, ChatFull):
|
||||||
|
return InputPeerChat(entity.id)
|
||||||
|
|
||||||
raise ValueError('Cannot cast {} to any kind of InputPeer.'
|
raise ValueError('Cannot cast {} to any kind of InputPeer.'
|
||||||
.format(type(entity).__name__))
|
.format(type(entity).__name__))
|
||||||
|
|
||||||
|
|
|
@ -186,7 +186,8 @@ class TLGenerator:
|
||||||
builder.writeln('from {}.tl.mtproto_request import MTProtoRequest'
|
builder.writeln('from {}.tl.mtproto_request import MTProtoRequest'
|
||||||
.format('.' * depth))
|
.format('.' * depth))
|
||||||
|
|
||||||
if any(a for a in tlobject.args if a.type == 'InputPeer'):
|
if tlobject.is_function and \
|
||||||
|
any(a for a in tlobject.args if a.type == 'InputPeer'):
|
||||||
# We can automatically convert a normal peer to an InputPeer,
|
# We can automatically convert a normal peer to an InputPeer,
|
||||||
# it will make invoking a lot of requests a lot simpler.
|
# it will make invoking a lot of requests a lot simpler.
|
||||||
builder.writeln('from {}.utils import get_input_peer'
|
builder.writeln('from {}.utils import get_input_peer'
|
||||||
|
@ -312,7 +313,7 @@ class TLGenerator:
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise ValueError('Cannot infer a value for ', arg)
|
raise ValueError('Cannot infer a value for ', arg)
|
||||||
elif arg.type == 'InputPeer':
|
elif arg.type == 'InputPeer' and tlobject.is_function:
|
||||||
# Well-known case, auto-cast it to the right type
|
# Well-known case, auto-cast it to the right type
|
||||||
builder.writeln(
|
builder.writeln(
|
||||||
'self.{0} = get_input_peer({0})'.format(arg.name))
|
'self.{0} = get_input_peer({0})'.format(arg.name))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user