Implemented read code on TLObjects Generator

The code generated by the generator now also writes the files
on_response(...) method. Also, all the generated files are
saved in a dictionary containing `constructorId: class`
This commit is contained in:
Lonami
2016-08-27 21:49:38 +02:00
parent 06832f8108
commit f00329265d
5 changed files with 161 additions and 29 deletions

View File

@@ -1,4 +1,5 @@
from io import BytesIO, BufferedReader
from tl.all_tlobjects import tlobjects
import os
@@ -25,6 +26,9 @@ class BinaryReader:
def read_long(self, signed=True):
return int.from_bytes(self.reader.read(8), signed=signed, byteorder='big')
def read_large_int(self, bits):
return int.from_bytes(self.reader.read(bits // 8), byteorder='big')
def read(self, length):
return self.reader.read(length)
@@ -54,6 +58,19 @@ class BinaryReader:
def tgread_string(self):
return str(self.tgread_bytes(), encoding='utf-8')
def tgread_object(self):
"""Reads a Telegram object"""
id = self.read_int()
clazz = tlobjects.get(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)))
# Instantiate the class and return the result
result = clazz()
result.on_response(self)
return result
# endregion
def close(self):