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:] images = images[10:]
result.extend( for x in documents:
await self.send_file( result.append(await self.send_file(
entity, x, allow_cache=allow_cache, entity, x, allow_cache=allow_cache,
caption=caption, force_document=force_document, caption=caption, force_document=force_document,
progress_callback=progress_callback, reply_to=reply_to, progress_callback=progress_callback, reply_to=reply_to,
attributes=attributes, thumb=thumb, voice_note=voice_note, attributes=attributes, thumb=thumb, voice_note=voice_note,
video_note=video_note, **kwargs video_note=video_note, **kwargs
) for x in documents ))
)
return result return result
entity = await self.get_input_entity(entity) entity = await self.get_input_entity(entity)
@ -186,10 +186,11 @@ class UploadMethods(MessageParseMethods, UserMethods):
entity = await self.get_input_entity(entity) entity = await self.get_input_entity(entity)
if not utils.is_list_like(caption): if not utils.is_list_like(caption):
caption = (caption,) caption = (caption,)
captions = [
await self._parse_message_text(caption or '', parse_mode) captions = []
for caption in reversed(caption) # Pop from the end (so reverse) 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) reply_to = utils.get_message_id(reply_to)
# Need to upload the media first, but only if they're not cached yet # 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 users (get users), input chat (get chats) and
# input channels (get channels) to get the most entities # input channels (get channels) to get the most entities
# in the less amount of calls possible. # in the less amount of calls possible.
inputs = [ inputs = []
x if isinstance(x, str) else await self.get_input_entity(x) for x in entity:
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 users = [x for x in inputs
if isinstance(x, (types.InputPeerUser, types.InputPeerSelf))] if isinstance(x, (types.InputPeerUser, types.InputPeerSelf))]
chats = [x.chat_id for x in inputs 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 # chats and channels list from before. While this would reduce
# the amount of ResolveUsername calls, it would fail to catch # the amount of ResolveUsername calls, it would fail to catch
# username changes. # username changes.
result = [ result = []
await self._get_entity_from_string(x) if isinstance(x, str) for x in inputs:
else ( if isinstance(x, str):
id_entity[utils.get_peer_id(x)] result.append(await self._get_entity_from_string(x))
if not isinstance(x, types.InputPeerSelf) elif not isinstance(x, types.InputPeerSelf):
else next(u for u in id_entity.values() result.append(id_entity[utils.get_peer_id(x)])
if isinstance(u, types.User) and u.is_self) else:
) result.append(next(
for x in inputs u for u in id_entity.values()
] if isinstance(u, types.User) and u.is_self
))
return result[0] if single else result return result[0] if single else result
async def get_input_entity(self, peer): 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) ac = AUTO_CASTS.get(arg.type, None)
if not ac: if not ac:
continue continue
if arg.is_flag:
builder.writeln('if self.{}:', arg.name)
if arg.is_vector: if arg.is_vector:
builder.write('self.{0} = [{1} for _x in self.{0}]', builder.writeln('_tmp = []')
arg.name, ac.format('_x')) 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: else:
builder.write('self.{} = {}', arg.name, builder.writeln('self.{} = {}', arg.name,
ac.format('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() builder.end_block()