mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-09 13:29:47 +00:00
Make sessions async
SQLiteSession is not updated, don't try to use it
This commit is contained in:
@@ -79,7 +79,7 @@ class Session(ABC):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def get_update_state(self, entity_id):
|
||||
async def get_update_state(self, entity_id):
|
||||
"""
|
||||
Returns the ``UpdateState`` associated with the given `entity_id`.
|
||||
If the `entity_id` is 0, it should return the ``UpdateState`` for
|
||||
@@ -89,7 +89,7 @@ class Session(ABC):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def set_update_state(self, entity_id, state):
|
||||
async def set_update_state(self, entity_id, state):
|
||||
"""
|
||||
Sets the given ``UpdateState`` for the specified `entity_id`, which
|
||||
should be 0 if the ``UpdateState`` is the "general" state (and not
|
||||
@@ -98,14 +98,14 @@ class Session(ABC):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def close(self):
|
||||
async def close(self):
|
||||
"""
|
||||
Called on client disconnection. Should be used to
|
||||
free any used resources. Can be left empty if none.
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def save(self):
|
||||
async def save(self):
|
||||
"""
|
||||
Called whenever important properties change. It should
|
||||
make persist the relevant session information to disk.
|
||||
@@ -113,22 +113,15 @@ class Session(ABC):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def delete(self):
|
||||
async def delete(self):
|
||||
"""
|
||||
Called upon client.log_out(). Should delete the stored
|
||||
information from disk since it's not valid anymore.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def list_sessions(cls):
|
||||
"""
|
||||
Lists available sessions. Not used by the library itself.
|
||||
"""
|
||||
return []
|
||||
|
||||
@abstractmethod
|
||||
def process_entities(self, tlo):
|
||||
async def process_entities(self, tlo):
|
||||
"""
|
||||
Processes the input ``TLObject`` or ``list`` and saves
|
||||
whatever information is relevant (e.g., ID or access hash).
|
||||
@@ -136,7 +129,7 @@ class Session(ABC):
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def get_input_entity(self, key):
|
||||
async def get_input_entity(self, key):
|
||||
"""
|
||||
Turns the given key into an ``InputPeer`` (e.g. ``InputPeerUser``).
|
||||
The library uses this method whenever an ``InputPeer`` is needed
|
||||
@@ -144,24 +137,3 @@ class Session(ABC):
|
||||
to use a cached username to avoid extra RPC).
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def cache_file(self, md5_digest, file_size, instance):
|
||||
"""
|
||||
Caches the given file information persistently, so that it
|
||||
doesn't need to be re-uploaded in case the file is used again.
|
||||
|
||||
The ``instance`` will be either an ``InputPhoto`` or ``InputDocument``,
|
||||
both with an ``.id`` and ``.access_hash`` attributes.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@abstractmethod
|
||||
def get_file(self, md5_digest, file_size, cls):
|
||||
"""
|
||||
Returns an instance of ``cls`` if the ``md5_digest`` and ``file_size``
|
||||
match an existing saved record. The class will either be an
|
||||
``InputPhoto`` or ``InputDocument``, both with two parameters
|
||||
``id`` and ``access_hash`` in that order.
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
@@ -71,19 +71,19 @@ class MemorySession(Session):
|
||||
def takeout_id(self, value):
|
||||
self._takeout_id = value
|
||||
|
||||
def get_update_state(self, entity_id):
|
||||
async def get_update_state(self, entity_id):
|
||||
return self._update_states.get(entity_id, None)
|
||||
|
||||
def set_update_state(self, entity_id, state):
|
||||
async def set_update_state(self, entity_id, state):
|
||||
self._update_states[entity_id] = state
|
||||
|
||||
def close(self):
|
||||
async def close(self):
|
||||
pass
|
||||
|
||||
def save(self):
|
||||
async def save(self):
|
||||
pass
|
||||
|
||||
def delete(self):
|
||||
async def delete(self):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
@@ -144,31 +144,31 @@ class MemorySession(Session):
|
||||
rows.append(row)
|
||||
return rows
|
||||
|
||||
def process_entities(self, tlo):
|
||||
async def process_entities(self, tlo):
|
||||
self._entities |= set(self._entities_to_rows(tlo))
|
||||
|
||||
def get_entity_rows_by_phone(self, phone):
|
||||
async def get_entity_rows_by_phone(self, phone):
|
||||
try:
|
||||
return next((id, hash) for id, hash, _, found_phone, _
|
||||
in self._entities if found_phone == phone)
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
def get_entity_rows_by_username(self, username):
|
||||
async def get_entity_rows_by_username(self, username):
|
||||
try:
|
||||
return next((id, hash) for id, hash, found_username, _, _
|
||||
in self._entities if found_username == username)
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
def get_entity_rows_by_name(self, name):
|
||||
async def get_entity_rows_by_name(self, name):
|
||||
try:
|
||||
return next((id, hash) for id, hash, _, _, found_name
|
||||
in self._entities if found_name == name)
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
def get_entity_rows_by_id(self, id, exact=True):
|
||||
async def get_entity_rows_by_id(self, id, exact=True):
|
||||
try:
|
||||
if exact:
|
||||
return next((id, hash) for found_id, hash, _, _, _
|
||||
@@ -184,7 +184,7 @@ class MemorySession(Session):
|
||||
except StopIteration:
|
||||
pass
|
||||
|
||||
def get_input_entity(self, key):
|
||||
async def get_input_entity(self, key):
|
||||
try:
|
||||
if key.SUBCLASS_OF_ID in (0xc91c90b6, 0xe669bf46, 0x40f202fd):
|
||||
# hex(crc32(b'InputPeer', b'InputUser' and b'InputChannel'))
|
||||
@@ -204,21 +204,21 @@ class MemorySession(Session):
|
||||
if isinstance(key, str):
|
||||
phone = utils.parse_phone(key)
|
||||
if phone:
|
||||
result = self.get_entity_rows_by_phone(phone)
|
||||
result = await self.get_entity_rows_by_phone(phone)
|
||||
else:
|
||||
username, invite = utils.parse_username(key)
|
||||
if username and not invite:
|
||||
result = self.get_entity_rows_by_username(username)
|
||||
result = await self.get_entity_rows_by_username(username)
|
||||
else:
|
||||
tup = utils.resolve_invite_link(key)[1]
|
||||
if tup:
|
||||
result = self.get_entity_rows_by_id(tup, exact=False)
|
||||
result = await self.get_entity_rows_by_id(tup, exact=False)
|
||||
|
||||
elif isinstance(key, int):
|
||||
result = self.get_entity_rows_by_id(key, exact)
|
||||
result = await self.get_entity_rows_by_id(key, exact)
|
||||
|
||||
if not result and isinstance(key, str):
|
||||
result = self.get_entity_rows_by_name(key)
|
||||
result = await self.get_entity_rows_by_name(key)
|
||||
|
||||
if result:
|
||||
entity_id, entity_hash = result # unpack resulting tuple
|
||||
@@ -233,14 +233,14 @@ class MemorySession(Session):
|
||||
else:
|
||||
raise ValueError('Could not find input entity with key ', key)
|
||||
|
||||
def cache_file(self, md5_digest, file_size, instance):
|
||||
async def cache_file(self, md5_digest, file_size, instance):
|
||||
if not isinstance(instance, (InputDocument, InputPhoto)):
|
||||
raise TypeError('Cannot cache %s instance' % type(instance))
|
||||
key = (md5_digest, file_size, _SentFileType.from_type(type(instance)))
|
||||
value = (instance.id, instance.access_hash)
|
||||
self._files[key] = value
|
||||
|
||||
def get_file(self, md5_digest, file_size, cls):
|
||||
async def get_file(self, md5_digest, file_size, cls):
|
||||
key = (md5_digest, file_size, _SentFileType.from_type(cls))
|
||||
try:
|
||||
return cls(*self._files[key])
|
||||
|
Reference in New Issue
Block a user