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.rpc_error = None
|
2018-01-19 14:12:52 +03:00
|
|
|
self.result = 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
|
2018-04-12 19:02:46 +03:00
|
|
|
|
|
|
|
# Internal parameter to tell pickler in which state Event object was
|
|
|
|
self._event_is_set = False
|
|
|
|
self._set_event()
|
|
|
|
|
|
|
|
def _set_event(self):
|
|
|
|
self.confirm_received = Event()
|
|
|
|
|
|
|
|
# Set Event state to 'set' if needed
|
|
|
|
if self._event_is_set:
|
|
|
|
self.confirm_received.set()
|
|
|
|
|
|
|
|
def __getstate__(self):
|
|
|
|
# Save state of the Event object
|
|
|
|
self._event_is_set = self.confirm_received.is_set()
|
|
|
|
|
|
|
|
# Exclude Event object from dict and return new state
|
|
|
|
new_dct = dict(self.__dict__)
|
|
|
|
del new_dct["confirm_received"]
|
|
|
|
return new_dct
|
|
|
|
|
|
|
|
def __setstate__(self, state):
|
|
|
|
self.__dict__ = state
|
|
|
|
self._set_event()
|
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):
|
2018-01-25 11:44:07 +03:00
|
|
|
obj = obj.to_dict()
|
|
|
|
|
2017-07-04 22:15:47 +03:00
|
|
|
if isinstance(obj, dict):
|
2018-01-25 11:51:12 +03:00
|
|
|
return '{}({})'.format(obj.get('_', 'dict'), ', '.join(
|
|
|
|
'{}={}'.format(k, TLObject.pretty_format(v))
|
|
|
|
for k, v in obj.items() if k != '_'
|
|
|
|
))
|
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 = []
|
2018-01-25 11:44:07 +03:00
|
|
|
if isinstance(obj, TLObject):
|
|
|
|
obj = obj.to_dict()
|
|
|
|
|
|
|
|
if isinstance(obj, dict):
|
2018-01-25 11:51:12 +03:00
|
|
|
result.append(obj.get('_', 'dict'))
|
|
|
|
result.append('(')
|
2018-01-25 11:44:07 +03:00
|
|
|
if obj:
|
2017-10-08 18:51:29 +03:00
|
|
|
result.append('\n')
|
|
|
|
indent += 1
|
2018-01-25 11:44:07 +03:00
|
|
|
for k, v in obj.items():
|
2018-01-25 11:51:12 +03:00
|
|
|
if k == '_':
|
2018-01-25 11:44:07 +03:00
|
|
|
continue
|
2017-10-08 18:51:29 +03:00
|
|
|
result.append('\t' * indent)
|
2018-01-25 11:51:12 +03:00
|
|
|
result.append(k)
|
|
|
|
result.append('=')
|
|
|
|
result.append(TLObject.pretty_format(v, indent))
|
2017-10-08 18:51:29 +03:00
|
|
|
result.append(',\n')
|
|
|
|
result.pop() # last ',\n'
|
|
|
|
indent -= 1
|
|
|
|
result.append('\n')
|
2017-07-04 22:15:47 +03:00
|
|
|
result.append('\t' * indent)
|
2018-01-25 11:51:12 +03:00
|
|
|
result.append(')')
|
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))
|
|
|
|
|
2018-01-19 14:12:52 +03:00
|
|
|
# These are nearly always the same for all subclasses
|
|
|
|
def on_response(self, reader):
|
|
|
|
self.result = reader.tgread_object()
|
|
|
|
|
2018-02-01 14:10:03 +03:00
|
|
|
def __eq__(self, o):
|
|
|
|
return isinstance(o, type(self)) and self.to_dict() == o.to_dict()
|
|
|
|
|
|
|
|
def __ne__(self, o):
|
|
|
|
return not isinstance(o, type(self)) or self.to_dict() != o.to_dict()
|
|
|
|
|
2018-01-19 14:12:52 +03:00
|
|
|
def __str__(self):
|
|
|
|
return TLObject.pretty_format(self)
|
|
|
|
|
|
|
|
def stringify(self):
|
|
|
|
return TLObject.pretty_format(self, indent=0)
|
|
|
|
|
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
|
|
|
|
|
2018-01-25 11:44:07 +03:00
|
|
|
def to_dict(self):
|
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()
|