From cfea0f80dae73e9db80e9617fafb85727d3248f5 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Tue, 11 Jul 2017 11:14:58 +0200 Subject: [PATCH] Consider vector attributes for get_input_* utils (closes #166) --- telethon_generator/tl_generator.py | 32 ++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/telethon_generator/tl_generator.py b/telethon_generator/tl_generator.py index e6e3dc4d..f7ab886f 100644 --- a/telethon_generator/tl_generator.py +++ b/telethon_generator/tl_generator.py @@ -322,19 +322,15 @@ class TLGenerator: ) else: raise ValueError('Cannot infer a value for ', arg) + + # Well-known cases, auto-cast it to the right type elif arg.type == 'InputPeer' and tlobject.is_function: - # Well-known case, auto-cast it to the right type - builder.writeln( - 'self.{0} = get_input_peer({0})'.format(arg.name) - ) + TLGenerator.write_get_input(builder, arg, 'get_input_peer') elif arg.type == 'InputChannel' and tlobject.is_function: - builder.writeln( - 'self.{0} = get_input_channel({0})'.format(arg.name) - ) + TLGenerator.write_get_input(builder, arg, 'get_input_channel') elif arg.type == 'InputUser' and tlobject.is_function: - builder.writeln( - 'self.{0} = get_input_user({0})'.format(arg.name) - ) + TLGenerator.write_get_input(builder, arg, 'get_input_user') + else: builder.writeln('self.{0} = {0}'.format(arg.name)) @@ -434,6 +430,22 @@ class TLGenerator: builder.writeln('return MTProtoRequest.pretty_format(self, indent=0)') # builder.end_block() # No need to end the last block + @staticmethod + def write_get_input(builder, arg, get_input_code): + """Returns "True" if the get_input_* code was written when assigning + a parameter upon creating the request. Returns False otherwise + """ + if arg.is_vector: + builder.writeln( + 'self.{0} = [{1}({0}_item) for {0}_item in {0}]' + .format(arg.name, get_input_code) + ) + pass + else: + builder.writeln( + 'self.{0} = {1}({0})'.format(arg.name, get_input_code) + ) + @staticmethod def get_class_name(tlobject): """Gets the class name following the Python style guidelines, in ThisClassFormat"""