mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-22 09:26:37 +03:00
Added ability to encode/decode TLObjects, updated README
This commit is contained in:
parent
87969745f5
commit
fddb3e9aac
|
@ -166,7 +166,6 @@ please stick with the suggested name or give one which is still descriptive enou
|
||||||
### Updating the `scheme.tl`
|
### Updating the `scheme.tl`
|
||||||
Have you found a more updated version of the `scheme.tl` file? Those are great news! Updating is as simple as grabbing the
|
Have you found a more updated version of the `scheme.tl` file? Those are great news! Updating is as simple as grabbing the
|
||||||
[latest version](https://github.com/telegramdesktop/tdesktop/blob/master/Telegram/SourceFiles/mtproto/scheme.tl) and
|
[latest version](https://github.com/telegramdesktop/tdesktop/blob/master/Telegram/SourceFiles/mtproto/scheme.tl) and
|
||||||
replacing the one you can find in this same directory by the updated one. Don't forget to run `python3 tl_generator.py`
|
replacing the one you can find in this same directory by the updated one. Don't forget to run `python3 tl_generator.py`.
|
||||||
afterwards and specifying the new layer number to be used when creating the `TelegramClient`.
|
|
||||||
|
|
||||||
If the changes weren't too big, everything should still work the same way as it did before; but with extra features.
|
If the changes weren't too big, everything should still work the same way as it did before; but with extra features.
|
||||||
|
|
|
@ -62,6 +62,7 @@ class TLGenerator:
|
||||||
with SourceBuilder(file) as builder:
|
with SourceBuilder(file) as builder:
|
||||||
# Both types and functions inherit from MTProtoRequest so they all can be sent
|
# Both types and functions inherit from MTProtoRequest so they all can be sent
|
||||||
builder.writeln('from telethon.tl.mtproto_request import MTProtoRequest')
|
builder.writeln('from telethon.tl.mtproto_request import MTProtoRequest')
|
||||||
|
builder.writeln('import json')
|
||||||
builder.writeln()
|
builder.writeln()
|
||||||
builder.writeln()
|
builder.writeln()
|
||||||
builder.writeln('class {}(MTProtoRequest):'.format(TLGenerator.get_class_name(tlobject)))
|
builder.writeln('class {}(MTProtoRequest):'.format(TLGenerator.get_class_name(tlobject)))
|
||||||
|
@ -153,6 +154,42 @@ class TLGenerator:
|
||||||
|
|
||||||
builder.writeln('def __str__(self):')
|
builder.writeln('def __str__(self):')
|
||||||
builder.writeln("return {}".format(str(tlobject)))
|
builder.writeln("return {}".format(str(tlobject)))
|
||||||
|
builder.end_block()
|
||||||
|
|
||||||
|
# Write JSON encoding
|
||||||
|
builder.writeln('def json_encode(self):')
|
||||||
|
builder.writeln('return json.dumps({')
|
||||||
|
builder.current_indent += 1
|
||||||
|
|
||||||
|
for arg in args:
|
||||||
|
if TLGenerator.is_tlobject(arg.type):
|
||||||
|
builder.writeln("'{0}': self.{0}.json_encode(),".format(arg.name))
|
||||||
|
else:
|
||||||
|
builder.writeln("'{0}': self.{0},".format(arg.name))
|
||||||
|
|
||||||
|
builder.current_indent -= 1
|
||||||
|
builder.writeln('})')
|
||||||
|
builder.end_block()
|
||||||
|
|
||||||
|
# Write JSON decoding
|
||||||
|
builder.writeln('@staticmethod')
|
||||||
|
builder.writeln('def json_decode(json_string):')
|
||||||
|
builder.writeln('# Create an empty instance which will be filled with the JSON values')
|
||||||
|
builder.writeln('instance = {}({})'.format(
|
||||||
|
TLGenerator.get_class_name(tlobject),
|
||||||
|
', '.join('None' for _ in range(len(args)))
|
||||||
|
))
|
||||||
|
builder.writeln('dictionary = json.loads(json_string)')
|
||||||
|
builder.writeln()
|
||||||
|
for arg in args:
|
||||||
|
if TLGenerator.is_tlobject(arg.type):
|
||||||
|
builder.writeln("instance.{0} = dictionary['{0}'].json_decode() if '{0}' in dictionary "
|
||||||
|
"and dictionary['{0}'] is not None else None".format(arg.name))
|
||||||
|
else:
|
||||||
|
builder.writeln("instance.{0} = dictionary.get('{0}', None)".format(arg.name))
|
||||||
|
|
||||||
|
builder.writeln()
|
||||||
|
builder.writeln('return instance')
|
||||||
# builder.end_block() # There is no need to end the last block
|
# builder.end_block() # There is no need to end the last block
|
||||||
|
|
||||||
# Once all the objects have been generated, we can now group them in a single file
|
# Once all the objects have been generated, we can now group them in a single file
|
||||||
|
@ -223,6 +260,12 @@ class TLGenerator:
|
||||||
else:
|
else:
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def is_tlobject(type):
|
||||||
|
"""Determines whether the type is a "basic" type or another TLObject"""
|
||||||
|
return type not in ['int', 'long', 'int128', 'int256', 'double',
|
||||||
|
'string', 'Bool', 'true', 'bytes', 'date']
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def write_onsend_code(builder, arg, args, name=None):
|
def write_onsend_code(builder, arg, args, name=None):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user