Telethon/telethon/sessions/memory.py

48 lines
1.4 KiB
Python
Raw Normal View History

2021-09-19 14:45:19 +03:00
from .types import DataCenter, ChannelState, SessionState, Entity
from .abstract import Session
2021-09-12 17:58:06 +03:00
from .._misc import utils, tlobject
2021-09-12 14:27:13 +03:00
from .. import _tl
2021-09-19 14:45:19 +03:00
from typing import List, Optional
class MemorySession(Session):
2021-09-19 14:45:19 +03:00
__slots__ = ('dcs', 'state', 'channel_states', 'entities')
2021-09-19 14:45:19 +03:00
def __init__(self):
self.dcs = {}
self.state = None
self.channel_states = {}
self.entities = {}
2021-09-19 14:45:19 +03:00
async def insert_dc(self, dc: DataCenter):
self.dcs[dc.id] = dc
2021-09-19 14:45:19 +03:00
async def get_all_dc(self) -> List[DataCenter]:
return list(self.dcs.values())
2021-09-19 14:45:19 +03:00
async def set_state(self, state: SessionState):
self.state = state
2021-09-19 14:45:19 +03:00
async def get_state(self) -> Optional[SessionState]:
return self.state
2021-09-19 14:45:19 +03:00
async def insert_channel_state(self, state: ChannelState):
self.channel_states[state.channel_id] = state
2021-09-19 14:45:19 +03:00
async def get_all_channel_states(self) -> List[ChannelState]:
return list(self.channel_states.values())
2021-09-19 14:45:19 +03:00
async def insert_entities(self, entities: List[Entity]):
self.entities.update((e.id, (e.ty, e.access_hash)) for e in entities)
async def get_entity(self, ty: Optional[int], id: int) -> Optional[Entity]:
try:
2021-09-19 14:45:19 +03:00
ty, access_hash = self.entities[id]
return Entity(ty, id, access_hash)
except KeyError:
return None
2021-09-19 14:45:19 +03:00
async def save(self):
pass