Fix Python 3.5 compatibility

This commit is contained in:
Lonami Exo 2018-06-20 20:18:16 +02:00
parent 9e3f6483e8
commit 9c0a22ddd7
3 changed files with 41 additions and 27 deletions

View File

@ -129,15 +129,15 @@ class UploadMethods(MessageParseMethods, UserMethods):
)
images = images[10:]
result.extend(
await self.send_file(
for x in documents:
result.append(await self.send_file(
entity, x, allow_cache=allow_cache,
caption=caption, force_document=force_document,
progress_callback=progress_callback, reply_to=reply_to,
attributes=attributes, thumb=thumb, voice_note=voice_note,
video_note=video_note, **kwargs
) for x in documents
)
))
return result
entity = await self.get_input_entity(entity)
@ -186,10 +186,11 @@ class UploadMethods(MessageParseMethods, UserMethods):
entity = await self.get_input_entity(entity)
if not utils.is_list_like(caption):
caption = (caption,)
captions = [
await self._parse_message_text(caption or '', parse_mode)
for caption in reversed(caption) # Pop from the end (so reverse)
]
captions = []
for c in reversed(caption): # Pop from the end (so reverse)
captions.append(await self._parse_message_text(c or '', parse_mode))
reply_to = utils.get_message_id(reply_to)
# Need to upload the media first, but only if they're not cached yet

View File

@ -130,10 +130,13 @@ class UserMethods(TelegramBaseClient):
# input users (get users), input chat (get chats) and
# input channels (get channels) to get the most entities
# in the less amount of calls possible.
inputs = [
x if isinstance(x, str) else await self.get_input_entity(x)
for x in entity
]
inputs = []
for x in entity:
if isinstance(x, str):
inputs.append(x)
else:
inputs.append(await self.get_input_entity(x))
users = [x for x in inputs
if isinstance(x, (types.InputPeerUser, types.InputPeerSelf))]
chats = [x.chat_id for x in inputs
@ -164,16 +167,18 @@ class UserMethods(TelegramBaseClient):
# chats and channels list from before. While this would reduce
# the amount of ResolveUsername calls, it would fail to catch
# username changes.
result = [
await self._get_entity_from_string(x) if isinstance(x, str)
else (
id_entity[utils.get_peer_id(x)]
if not isinstance(x, types.InputPeerSelf)
else next(u for u in id_entity.values()
if isinstance(u, types.User) and u.is_self)
)
for x in inputs
]
result = []
for x in inputs:
if isinstance(x, str):
result.append(await self._get_entity_from_string(x))
elif not isinstance(x, types.InputPeerSelf):
result.append(id_entity[utils.get_peer_id(x)])
else:
result.append(next(
u for u in id_entity.values()
if isinstance(u, types.User) and u.is_self
))
return result[0] if single else result
async def get_input_entity(self, peer):

View File

@ -238,14 +238,22 @@ def _write_resolve(tlobject, builder):
ac = AUTO_CASTS.get(arg.type, None)
if not ac:
continue
if arg.is_flag:
builder.writeln('if self.{}:', arg.name)
if arg.is_vector:
builder.write('self.{0} = [{1} for _x in self.{0}]',
arg.name, ac.format('_x'))
builder.writeln('_tmp = []')
builder.writeln('for _x in self.{0}:', arg.name)
builder.writeln('_tmp.append({})', ac.format('_x'))
builder.end_block()
builder.writeln('self.{} = _tmp', arg.name)
else:
builder.write('self.{} = {}', arg.name,
builder.writeln('self.{} = {}', arg.name,
ac.format('self.' + arg.name))
builder.writeln(' if self.{} else None'.format(arg.name)
if arg.is_flag else '')
if arg.is_flag:
builder.end_block()
builder.end_block()