From fa069be341160b0db52179ec064f80aba228a357 Mon Sep 17 00:00:00 2001 From: KurimuzonAkuma Date: Sat, 13 Jan 2024 18:11:50 +0300 Subject: [PATCH] Make Storage class an abstract base class --- pyrogram/storage/storage.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/pyrogram/storage/storage.py b/pyrogram/storage/storage.py index 6af890d0..8f0938e4 100644 --- a/pyrogram/storage/storage.py +++ b/pyrogram/storage/storage.py @@ -16,12 +16,20 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +from abc import ABC, abstractmethod import base64 import struct from typing import List, Tuple -class Storage: +class Storage(ABC): + """ + Abstract class for storage engines. + + Parameters: + name (``str``): + The name of the session. + """ OLD_SESSION_STRING_FORMAT = ">B?256sI?" OLD_SESSION_STRING_FORMAT_64 = ">B?256sQ?" SESSION_STRING_SIZE = 351 @@ -32,22 +40,27 @@ class Storage: def __init__(self, name: str): self.name = name + @abstractmethod async def open(self): """Opens the storage engine.""" raise NotImplementedError + @abstractmethod async def save(self): """Saves the current state of the storage engine.""" raise NotImplementedError + @abstractmethod async def close(self): """Closes the storage engine.""" raise NotImplementedError + @abstractmethod async def delete(self): """Deletes the storage.""" raise NotImplementedError + @abstractmethod async def update_peers(self, peers: List[Tuple[int, int, str, List[str], str]]): """ Update the peers table with the provided information. @@ -64,6 +77,7 @@ class Storage: """ raise NotImplementedError + @abstractmethod async def get_peer_by_id(self, peer_id: int): """Retrieve a peer by its ID. @@ -73,6 +87,7 @@ class Storage: """ raise NotImplementedError + @abstractmethod async def get_peer_by_username(self, username: str): """Retrieve a peer by its username. @@ -82,6 +97,7 @@ class Storage: """ raise NotImplementedError + @abstractmethod async def get_peer_by_phone_number(self, phone_number: str): """Retrieve a peer by its phone number. @@ -91,6 +107,7 @@ class Storage: """ raise NotImplementedError + @abstractmethod async def dc_id(self, value: int = object): """Get or set the DC ID of the current session. @@ -100,6 +117,7 @@ class Storage: """ raise NotImplementedError + @abstractmethod async def api_id(self, value: int = object): """Get or set the API ID of the current session. @@ -109,6 +127,7 @@ class Storage: """ raise NotImplementedError + @abstractmethod async def test_mode(self, value: bool = object): """Get or set the test mode of the current session. @@ -118,6 +137,7 @@ class Storage: """ raise NotImplementedError + @abstractmethod async def auth_key(self, value: bytes = object): """Get or set the authorization key of the current session. @@ -127,6 +147,7 @@ class Storage: """ raise NotImplementedError + @abstractmethod async def date(self, value: int = object): """Get or set the date of the current session. @@ -136,6 +157,7 @@ class Storage: """ raise NotImplementedError + @abstractmethod async def user_id(self, value: int = object): """Get or set the user ID of the current session. @@ -145,6 +167,7 @@ class Storage: """ raise NotImplementedError + @abstractmethod async def is_bot(self, value: bool = object): """Get or set the bot flag of the current session. @@ -156,7 +179,7 @@ class Storage: async def export_session_string(self): """Exports the session string for the current session. - + Returns: ``str``: The session string for the current session. """