First attempt at TelegramClient. Added fixes and doc

This commit is contained in:
Lonami
2016-09-04 11:07:18 +02:00
parent c863537b7b
commit 39a23559f0
19 changed files with 256 additions and 65 deletions

View File

@@ -1,6 +1,7 @@
from io import BytesIO, BufferedReader
from tl.all_tlobjects import tlobjects
from struct import unpack
import inspect
import os
@@ -85,14 +86,20 @@ class BinaryReader:
def tgread_object(self):
"""Reads a Telegram object"""
id = self.read_int()
clazz = tlobjects.get(id, None)
constructor_id = self.read_int()
clazz = tlobjects.get(constructor_id, None)
if clazz is None:
raise ImportError('Could not find a matching ID for the TLObject that was supposed to be read. '
'Found ID: {}'.format(hex(id)))
'Found ID: {}'.format(hex(constructor_id)))
# Instantiate the class and return the result
result = clazz()
# Now we need to determine the number of parameters of the class, so we can
# instantiate it with all of them set to None, and still, no need to write
# the default =None in all the classes, thus forcing the user to provide a real value
sig = inspect.signature(clazz.__init__)
params = [None] * (len(sig.parameters) - 1) # Subtract 1 (self)
result = clazz(*params) # https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists
# Finally, read the object and return the result
result.on_response(self)
return result
@@ -100,7 +107,6 @@ class BinaryReader:
def close(self):
self.reader.close()
# TODO Do I need to close the underlying stream?
# region Position related