Skip to content

Commit

Permalink
Reuse .on_response/.__str__/.stringify, override iff necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Lonami committed Jan 19, 2018
1 parent f6d98a6 commit 33e50aa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
2 changes: 2 additions & 0 deletions telethon/extensions/binary_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ def tgread_object(self):
return True
elif value == 0xbc799737: # boolFalse
return False
elif value == 0x1cb5c415: # Vector
return [self.tgread_object() for _ in range(self.read_int())]

# If there was still no luck, give up
self.seek(-4) # Go back
Expand Down
11 changes: 11 additions & 0 deletions telethon/tl/tlobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class TLObject:
def __init__(self):
self.confirm_received = Event()
self.rpc_error = None
self.result = None

# These should be overrode
self.content_related = False # Only requests/functions/queries are
Expand Down Expand Up @@ -143,6 +144,16 @@ def serialize_datetime(dt):

raise TypeError('Cannot interpret "{}" as a date.'.format(dt))

# These are nearly always the same for all subclasses
def on_response(self, reader):
self.result = reader.tgread_object()

def __str__(self):
return TLObject.pretty_format(self)

def stringify(self):
return TLObject.pretty_format(self, indent=0)

# These should be overrode
def resolve(self, client, utils):
pass
Expand Down
33 changes: 20 additions & 13 deletions telethon_generator/tl_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,23 +395,30 @@ def _write_source_code(tlobject, builder, depth, type_constructors):
if not a.flag_indicator and not a.generic_definition
)
))
builder.end_block()

# Only requests can have a different response that's not their
# serialized body, that is, we'll be setting their .result.
if tlobject.is_function:
#
# The default behaviour is reading a TLObject too, so no need
# to override it unless necessary.
if tlobject.is_function and not TLGenerator._is_boxed(tlobject.result):
builder.end_block()
builder.writeln('def on_response(self, reader):')
TLGenerator.write_request_result_code(builder, tlobject)
builder.end_block()

# Write the __str__(self) and stringify(self) functions
builder.writeln('def __str__(self):')
builder.writeln('return TLObject.pretty_format(self)')
builder.end_block()

builder.writeln('def stringify(self):')
builder.writeln('return TLObject.pretty_format(self, indent=0)')
# builder.end_block() # No need to end the last block
@staticmethod
def _is_boxed(type_):
# https://core.telegram.org/mtproto/serialize#boxed-and-bare-types
# TL;DR; boxed types start with uppercase always, so we can use
# this to check whether everything in it is boxed or not.
#
# The API always returns a boxed type, but it may inside a Vector<>
# or a namespace, and the Vector may have a not-boxed type. For this
# reason we find whatever index, '<' or '.'. If neither are present
# we will get -1, and the 0th char is always upper case thus works.
# For Vector types and namespaces, it will check in the right place.
check_after = max(type_.find('<'), type_.find('.'))
return type_[check_after + 1].isupper()

@staticmethod
def _write_self_assign(builder, arg, get_input_code):
Expand Down Expand Up @@ -697,13 +704,13 @@ def write_request_result_code(builder, tlobject):
# not parsed as arguments are and it's a bit harder to tell which
# is which.
if tlobject.result == 'Vector<int>':
builder.writeln('reader.read_int() # Vector id')
builder.writeln('reader.read_int() # Vector ID')
builder.writeln('count = reader.read_int()')
builder.writeln(
'self.result = [reader.read_int() for _ in range(count)]'
)
elif tlobject.result == 'Vector<long>':
builder.writeln('reader.read_int() # Vector id')
builder.writeln('reader.read_int() # Vector ID')
builder.writeln('count = reader.read_long()')
builder.writeln(
'self.result = [reader.read_long() for _ in range(count)]'
Expand Down

0 comments on commit 33e50aa

Please sign in to comment.