mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-06-02 12:43:05 +03:00
Small clean-up of the session classes
This commit is contained in:
parent
1e420f7f91
commit
a9c83250a1
|
@ -91,12 +91,11 @@ class Session(ABC):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@abstractmethod
|
|
||||||
def list_sessions(cls):
|
def list_sessions(cls):
|
||||||
"""
|
"""
|
||||||
Lists available sessions. Not used by the library itself.
|
Lists available sessions. Not used by the library itself.
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError
|
return []
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def process_entities(self, tlo):
|
def process_entities(self, tlo):
|
||||||
|
|
|
@ -67,13 +67,6 @@ class MemorySession(Session):
|
||||||
def delete(self):
|
def delete(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def list_sessions(cls):
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
def _entity_values_to_row(self, id, hash, username, phone, name):
|
|
||||||
return id, hash, username, phone, name
|
|
||||||
|
|
||||||
def _entity_to_row(self, e):
|
def _entity_to_row(self, e):
|
||||||
if not isinstance(e, TLObject):
|
if not isinstance(e, TLObject):
|
||||||
return
|
return
|
||||||
|
@ -103,7 +96,7 @@ class MemorySession(Session):
|
||||||
username = username.lower()
|
username = username.lower()
|
||||||
phone = getattr(e, 'phone', None)
|
phone = getattr(e, 'phone', None)
|
||||||
name = utils.get_display_name(e) or None
|
name = utils.get_display_name(e) or None
|
||||||
return self._entity_values_to_row(marked_id, p_hash, username, phone, name)
|
return marked_id, p_hash, username, phone, name
|
||||||
|
|
||||||
def _entities_to_rows(self, tlo):
|
def _entities_to_rows(self, tlo):
|
||||||
if not isinstance(tlo, TLObject) and utils.is_list_like(tlo):
|
if not isinstance(tlo, TLObject) and utils.is_list_like(tlo):
|
||||||
|
@ -129,24 +122,32 @@ class MemorySession(Session):
|
||||||
self._entities += set(self._entities_to_rows(tlo))
|
self._entities += set(self._entities_to_rows(tlo))
|
||||||
|
|
||||||
def get_entity_rows_by_phone(self, phone):
|
def get_entity_rows_by_phone(self, phone):
|
||||||
rows = [(id, hash) for id, hash, _, found_phone, _
|
try:
|
||||||
in self._entities if found_phone == phone]
|
return next((id, hash) for id, hash, _, found_phone, _
|
||||||
return rows[0] if rows else None
|
in self._entities if found_phone == phone)
|
||||||
|
except StopIteration:
|
||||||
|
pass
|
||||||
|
|
||||||
def get_entity_rows_by_username(self, username):
|
def get_entity_rows_by_username(self, username):
|
||||||
rows = [(id, hash) for id, hash, found_username, _, _
|
try:
|
||||||
in self._entities if found_username == username]
|
return next((id, hash) for id, hash, found_username, _, _
|
||||||
return rows[0] if rows else None
|
in self._entities if found_username == username)
|
||||||
|
except StopIteration:
|
||||||
|
pass
|
||||||
|
|
||||||
def get_entity_rows_by_name(self, name):
|
def get_entity_rows_by_name(self, name):
|
||||||
rows = [(id, hash) for id, hash, _, _, found_name
|
try:
|
||||||
in self._entities if found_name == name]
|
return next((id, hash) for id, hash, _, _, found_name
|
||||||
return rows[0] if rows else None
|
in self._entities if found_name == name)
|
||||||
|
except StopIteration:
|
||||||
|
pass
|
||||||
|
|
||||||
def get_entity_rows_by_id(self, id):
|
def get_entity_rows_by_id(self, id):
|
||||||
rows = [(id, hash) for found_id, hash, _, _, _
|
try:
|
||||||
in self._entities if found_id == id]
|
return next((id, hash) for found_id, hash, _, _, _
|
||||||
return rows[0] if rows else None
|
in self._entities if found_id == id)
|
||||||
|
except StopIteration:
|
||||||
|
pass
|
||||||
|
|
||||||
def get_input_entity(self, key):
|
def get_input_entity(self, key):
|
||||||
try:
|
try:
|
||||||
|
@ -199,6 +200,6 @@ class MemorySession(Session):
|
||||||
def get_file(self, md5_digest, file_size, cls):
|
def get_file(self, md5_digest, file_size, cls):
|
||||||
key = (md5_digest, file_size, _SentFileType.from_type(cls))
|
key = (md5_digest, file_size, _SentFileType.from_type(cls))
|
||||||
try:
|
try:
|
||||||
return self._files[key]
|
return cls(self._files[key])
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return None
|
return None
|
||||||
|
|
|
@ -276,18 +276,15 @@ class SQLiteSession(MemorySession):
|
||||||
|
|
||||||
def get_entity_rows_by_username(self, username):
|
def get_entity_rows_by_username(self, username):
|
||||||
return self._fetchone_entity(
|
return self._fetchone_entity(
|
||||||
'select id, hash from entities where username=?',
|
'select id, hash from entities where username=?', (username,))
|
||||||
(username,))
|
|
||||||
|
|
||||||
def get_entity_rows_by_name(self, name):
|
def get_entity_rows_by_name(self, name):
|
||||||
return self._fetchone_entity(
|
return self._fetchone_entity(
|
||||||
'select id, hash from entities where name=?',
|
'select id, hash from entities where name=?', (name,))
|
||||||
(name,))
|
|
||||||
|
|
||||||
def get_entity_rows_by_id(self, id):
|
def get_entity_rows_by_id(self, id):
|
||||||
return self._fetchone_entity(
|
return self._fetchone_entity(
|
||||||
'select id, hash from entities where id=?',
|
'select id, hash from entities where id=?', (id,))
|
||||||
(id,))
|
|
||||||
|
|
||||||
# File processing
|
# File processing
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user