2016-08-26 13:58:53 +03:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
|
2017-07-24 17:54:48 +03:00
|
|
|
class TLObject:
|
2016-08-26 13:58:53 +03:00
|
|
|
def __init__(self):
|
|
|
|
self.sent = False
|
|
|
|
|
2017-06-18 11:01:59 +03:00
|
|
|
self.request_msg_id = 0 # Long
|
2016-08-26 13:58:53 +03:00
|
|
|
self.sequence = 0
|
|
|
|
|
|
|
|
self.dirty = False
|
|
|
|
self.send_time = None
|
|
|
|
self.confirm_received = False
|
|
|
|
|
|
|
|
# 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
|
|
|
self.responded = False
|
|
|
|
|
|
|
|
# These should not be overrode
|
|
|
|
def on_send_success(self):
|
|
|
|
self.send_time = datetime.now()
|
|
|
|
self.sent = True
|
|
|
|
|
|
|
|
def on_confirm(self):
|
|
|
|
self.confirm_received = True
|
|
|
|
|
|
|
|
def need_resend(self):
|
2016-11-30 00:29:42 +03:00
|
|
|
return self.dirty or (
|
2017-07-24 17:54:48 +03:00
|
|
|
self.content_related and not self.confirm_received and
|
2016-11-30 00:29:42 +03:00
|
|
|
datetime.now() - self.send_time > timedelta(seconds=3))
|
2016-08-26 13:58:53 +03:00
|
|
|
|
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-07-04 22:15:47 +03:00
|
|
|
return '{{{}: {}}}'.format(
|
|
|
|
type(obj).__name__,
|
2017-07-24 17:54:48 +03:00
|
|
|
TLObject.pretty_format(obj.to_dict())
|
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()
|
|
|
|
))
|
|
|
|
elif isinstance(obj, str):
|
|
|
|
return '"{}"'.format(obj)
|
|
|
|
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('{')
|
|
|
|
result.append(type(obj).__name__)
|
|
|
|
result.append(': ')
|
2017-07-24 17:54:48 +03:00
|
|
|
result.append(TLObject.pretty_format(
|
2017-07-04 22:15:47 +03:00
|
|
|
obj.to_dict(), indent
|
|
|
|
))
|
|
|
|
|
|
|
|
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('}')
|
|
|
|
|
|
|
|
elif isinstance(obj, str):
|
|
|
|
result.append('"')
|
|
|
|
result.append(obj)
|
|
|
|
result.append('"')
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2016-08-26 13:58:53 +03:00
|
|
|
# These should be overrode
|
2017-07-04 22:15:47 +03:00
|
|
|
def to_dict(self):
|
|
|
|
return {}
|
|
|
|
|
2016-08-26 13:58:53 +03:00
|
|
|
def on_send(self, writer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def on_response(self, reader):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def on_exception(self, exception):
|
|
|
|
pass
|