2017-12-28 14:32:16 +03:00
|
|
|
import struct
|
|
|
|
from datetime import datetime, date
|
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-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
|
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-10-08 18:51:29 +03:00
|
|
|
return '{}({})'.format(type(obj).__name__, ', '.join(
|
|
|
|
'{}={}'.format(k, TLObject.pretty_format(v))
|
|
|
|
for k, v in obj.to_dict(recursive=False).items()
|
|
|
|
))
|
2017-07-04 22:15:47 +03:00
|
|
|
if isinstance(obj, dict):
|
|
|
|
return '{{{}}}'.format(', '.join(
|
2017-10-08 18:51:29 +03:00
|
|
|
'{}: {}'.format(k, TLObject.pretty_format(v))
|
|
|
|
for k, v in obj.items()
|
2017-07-04 22:15:47 +03:00
|
|
|
))
|
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
|
|
|
)
|
2017-10-08 18:51:29 +03:00
|
|
|
elif isinstance(obj, datetime):
|
2017-11-23 15:39:35 +03:00
|
|
|
return 'datetime.utcfromtimestamp({})'.format(
|
|
|
|
int(obj.timestamp())
|
|
|
|
)
|
2017-07-04 22:15:47 +03:00
|
|
|
else:
|
2017-10-08 18:51:29 +03:00
|
|
|
return repr(obj)
|
2017-07-04 22:15:47 +03:00
|
|
|
else:
|
|
|
|
result = []
|
2017-10-08 18:51:29 +03:00
|
|
|
if isinstance(obj, TLObject) or isinstance(obj, dict):
|
|
|
|
if isinstance(obj, dict):
|
|
|
|
d = obj
|
|
|
|
start, end, sep = '{', '}', ': '
|
|
|
|
else:
|
|
|
|
d = obj.to_dict(recursive=False)
|
|
|
|
start, end, sep = '(', ')', '='
|
|
|
|
result.append(type(obj).__name__)
|
2017-07-04 22:15:47 +03:00
|
|
|
|
2017-10-08 18:51:29 +03:00
|
|
|
result.append(start)
|
|
|
|
if d:
|
|
|
|
result.append('\n')
|
|
|
|
indent += 1
|
|
|
|
for k, v in d.items():
|
|
|
|
result.append('\t' * indent)
|
|
|
|
result.append(k)
|
|
|
|
result.append(sep)
|
|
|
|
result.append(TLObject.pretty_format(v, indent))
|
|
|
|
result.append(',\n')
|
|
|
|
result.pop() # last ',\n'
|
|
|
|
indent -= 1
|
|
|
|
result.append('\n')
|
2017-07-04 22:15:47 +03:00
|
|
|
result.append('\t' * indent)
|
2017-10-08 18:51:29 +03:00
|
|
|
result.append(end)
|
2017-07-04 22:15:47 +03:00
|
|
|
|
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(']')
|
|
|
|
|
2017-10-08 18:51:29 +03:00
|
|
|
elif isinstance(obj, datetime):
|
2017-11-22 19:06:43 +03:00
|
|
|
result.append('datetime.utcfromtimestamp(')
|
2017-11-23 15:39:35 +03:00
|
|
|
result.append(repr(int(obj.timestamp())))
|
2017-10-08 18:51:29 +03:00
|
|
|
result.append(')')
|
|
|
|
|
2017-07-04 22:15:47 +03:00
|
|
|
else:
|
2017-10-08 18:51:29 +03:00
|
|
|
result.append(repr(obj))
|
2017-07-04 22:15:47 +03:00
|
|
|
|
|
|
|
return ''.join(result)
|
|
|
|
|
2017-09-26 15:36:02 +03:00
|
|
|
@staticmethod
|
|
|
|
def serialize_bytes(data):
|
|
|
|
"""Write bytes by using Telegram guidelines"""
|
2017-10-25 13:43:57 +03:00
|
|
|
if not isinstance(data, bytes):
|
|
|
|
if isinstance(data, str):
|
|
|
|
data = data.encode('utf-8')
|
|
|
|
else:
|
2017-12-28 02:22:28 +03:00
|
|
|
raise TypeError(
|
|
|
|
'bytes or str expected, not {}'.format(type(data)))
|
2017-09-28 10:55:29 +03:00
|
|
|
|
2017-09-26 15:36:02 +03:00
|
|
|
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
|
|
|
|
|
2017-09-26 15:41:11 +03:00
|
|
|
r.append(bytes([
|
|
|
|
254,
|
|
|
|
len(data) % 256,
|
|
|
|
(len(data) >> 8) % 256,
|
|
|
|
(len(data) >> 16) % 256
|
|
|
|
]))
|
2017-09-26 15:36:02 +03:00
|
|
|
r.append(data)
|
|
|
|
|
|
|
|
r.append(bytes(padding))
|
|
|
|
return b''.join(r)
|
|
|
|
|
2017-12-28 14:32:16 +03:00
|
|
|
@staticmethod
|
|
|
|
def serialize_datetime(dt):
|
|
|
|
if not dt:
|
|
|
|
return b'\0\0\0\0'
|
|
|
|
|
|
|
|
if isinstance(dt, datetime):
|
|
|
|
dt = int(dt.timestamp())
|
|
|
|
elif isinstance(dt, date):
|
2018-01-05 20:31:48 +03:00
|
|
|
dt = int(datetime(dt.year, dt.month, dt.day).timestamp())
|
2017-12-28 14:32:16 +03:00
|
|
|
elif isinstance(dt, float):
|
|
|
|
dt = int(dt)
|
|
|
|
|
|
|
|
if isinstance(dt, int):
|
|
|
|
return struct.pack('<I', dt)
|
|
|
|
|
|
|
|
raise TypeError('Cannot interpret "{}" as a date.'.format(dt))
|
|
|
|
|
2016-08-26 13:58:53 +03:00
|
|
|
# These should be overrode
|
2018-01-19 13:47:45 +03:00
|
|
|
def resolve(self, client, utils):
|
|
|
|
pass
|
|
|
|
|
2017-09-23 12:01:25 +03:00
|
|
|
def to_dict(self, recursive=True):
|
2017-07-04 22:15:47 +03:00
|
|
|
return {}
|
|
|
|
|
2017-10-17 20:54:24 +03:00
|
|
|
def __bytes__(self):
|
2017-09-26 15:36:02 +03:00
|
|
|
return b''
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-10-07 14:26:09 +03:00
|
|
|
@staticmethod
|
|
|
|
def from_reader(reader):
|
|
|
|
return TLObject()
|