2017-09-02 21:41:00 +03:00
|
|
|
from threading import Event
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
|
2017-07-24 17:54:48 +03:00
|
|
|
class TLObject:
|
2016-08-26 13:58:53 +03:00
|
|
|
def __init__(self):
|
2017-06-18 11:01:59 +03:00
|
|
|
self.request_msg_id = 0 # Long
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-09-02 21:41:00 +03:00
|
|
|
self.confirm_received = Event()
|
|
|
|
self.rpc_error = None
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
# These should be overrode
|
2016-09-09 12:47:37 +03:00
|
|
|
self.constructor_id = 0
|
2017-07-24 17:54:48 +03:00
|
|
|
self.content_related = False # Only requests/functions/queries are
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
# These should not be overrode
|
2017-07-04 22:15:47 +03:00
|
|
|
@staticmethod
|
|
|
|
def pretty_format(obj, indent=None):
|
|
|
|
"""Pretty formats the given object as a string which is returned.
|
|
|
|
If indent is None, a single line will be returned.
|
|
|
|
"""
|
|
|
|
if indent is None:
|
2017-07-24 17:54:48 +03:00
|
|
|
if isinstance(obj, TLObject):
|
2017-09-23 12:01:25 +03:00
|
|
|
children = obj.to_dict(recursive=False)
|
|
|
|
if children:
|
|
|
|
return '{}: {}'.format(
|
|
|
|
type(obj).__name__, TLObject.pretty_format(children)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return type(obj).__name__
|
2017-07-04 22:15:47 +03:00
|
|
|
if isinstance(obj, dict):
|
|
|
|
return '{{{}}}'.format(', '.join(
|
|
|
|
'{}: {}'.format(
|
2017-07-24 17:54:48 +03:00
|
|
|
k, TLObject.pretty_format(v)
|
2017-07-04 22:15:47 +03:00
|
|
|
) for k, v in obj.items()
|
|
|
|
))
|
2017-08-24 21:56:08 +03:00
|
|
|
elif isinstance(obj, str) or isinstance(obj, bytes):
|
|
|
|
return repr(obj)
|
2017-07-04 22:15:47 +03:00
|
|
|
elif hasattr(obj, '__iter__'):
|
|
|
|
return '[{}]'.format(
|
2017-07-24 17:54:48 +03:00
|
|
|
', '.join(TLObject.pretty_format(x) for x in obj)
|
2017-07-04 22:15:47 +03:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
return str(obj)
|
|
|
|
else:
|
|
|
|
result = []
|
2017-07-24 17:54:48 +03:00
|
|
|
if isinstance(obj, TLObject):
|
2017-07-04 22:15:47 +03:00
|
|
|
result.append(type(obj).__name__)
|
2017-09-23 12:01:25 +03:00
|
|
|
children = obj.to_dict(recursive=False)
|
|
|
|
if children:
|
|
|
|
result.append(': ')
|
|
|
|
result.append(TLObject.pretty_format(
|
|
|
|
obj.to_dict(recursive=False), indent
|
|
|
|
))
|
2017-07-04 22:15:47 +03:00
|
|
|
|
|
|
|
elif isinstance(obj, dict):
|
|
|
|
result.append('{\n')
|
|
|
|
indent += 1
|
|
|
|
for k, v in obj.items():
|
|
|
|
result.append('\t' * indent)
|
|
|
|
result.append(k)
|
|
|
|
result.append(': ')
|
2017-07-24 17:54:48 +03:00
|
|
|
result.append(TLObject.pretty_format(v, indent))
|
2017-07-04 22:15:47 +03:00
|
|
|
result.append(',\n')
|
|
|
|
indent -= 1
|
|
|
|
result.append('\t' * indent)
|
|
|
|
result.append('}')
|
|
|
|
|
2017-09-17 17:35:35 +03:00
|
|
|
elif isinstance(obj, str) or isinstance(obj, bytes):
|
|
|
|
result.append(repr(obj))
|
2017-07-04 22:15:47 +03:00
|
|
|
|
|
|
|
elif hasattr(obj, '__iter__'):
|
|
|
|
result.append('[\n')
|
|
|
|
indent += 1
|
|
|
|
for x in obj:
|
|
|
|
result.append('\t' * indent)
|
2017-07-24 17:54:48 +03:00
|
|
|
result.append(TLObject.pretty_format(x, indent))
|
2017-07-04 22:15:47 +03:00
|
|
|
result.append(',\n')
|
|
|
|
indent -= 1
|
|
|
|
result.append('\t' * indent)
|
|
|
|
result.append(']')
|
|
|
|
|
|
|
|
else:
|
|
|
|
result.append(str(obj))
|
|
|
|
|
|
|
|
return ''.join(result)
|
|
|
|
|
2017-09-26 15:36:02 +03:00
|
|
|
@staticmethod
|
|
|
|
def serialize_bytes(data):
|
|
|
|
"""Write bytes by using Telegram guidelines"""
|
|
|
|
r = []
|
|
|
|
if len(data) < 254:
|
|
|
|
padding = (len(data) + 1) % 4
|
|
|
|
if padding != 0:
|
|
|
|
padding = 4 - padding
|
|
|
|
|
|
|
|
r.append(bytes([len(data)]))
|
|
|
|
r.append(data)
|
|
|
|
|
|
|
|
else:
|
|
|
|
padding = len(data) % 4
|
|
|
|
if padding != 0:
|
|
|
|
padding = 4 - padding
|
|
|
|
|
|
|
|
r.append(bytes([254]))
|
|
|
|
r.append(bytes([len(data) % 256]))
|
|
|
|
r.append(bytes([(len(data) >> 8) % 256]))
|
|
|
|
r.append(bytes([(len(data) >> 16) % 256]))
|
|
|
|
r.append(data)
|
|
|
|
|
|
|
|
r.append(bytes(padding))
|
|
|
|
return b''.join(r)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def serialize_string(string):
|
|
|
|
return TLObject.serialize_bytes(string.encode('utf-8'))
|
|
|
|
|
2016-08-26 13:58:53 +03:00
|
|
|
# These should be overrode
|
2017-09-23 12:01:25 +03:00
|
|
|
def to_dict(self, recursive=True):
|
2017-07-04 22:15:47 +03:00
|
|
|
return {}
|
|
|
|
|
2017-09-26 15:36:02 +03:00
|
|
|
def to_bytes(self):
|
|
|
|
return b''
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
def on_response(self, reader):
|
|
|
|
pass
|