|
19 | 19 |
|
20 | 20 | import base64 |
21 | 21 | import struct |
| 22 | +from abc import ABC, abstractmethod |
22 | 23 | from typing import List, Tuple |
23 | 24 |
|
24 | 25 |
|
25 | | -class BaseStorage: |
26 | | - OLD_SESSION_STRING_FORMAT = ">B?256sI?" |
27 | | - OLD_SESSION_STRING_FORMAT_64 = ">B?256sQ?" |
28 | | - SESSION_STRING_SIZE = 351 |
29 | | - SESSION_STRING_SIZE_64 = 356 |
| 26 | +class BaseStorage(ABC): |
| 27 | + """The BaseStorage class is an abstract base class that defines the interface |
| 28 | + for different storage engines used by Hyrogram. |
| 29 | +
|
| 30 | + Parameters: |
| 31 | + name (``str``): |
| 32 | + The name of the session. |
| 33 | + """ |
30 | 34 |
|
31 | 35 | SESSION_STRING_FORMAT = ">BI?256sQ?" |
32 | 36 |
|
33 | 37 | def __init__(self, name: str): |
34 | 38 | self.name = name |
35 | 39 |
|
| 40 | + @abstractmethod |
36 | 41 | async def open(self): |
| 42 | + """Opens the storage engine.""" |
37 | 43 | raise NotImplementedError |
38 | 44 |
|
| 45 | + @abstractmethod |
39 | 46 | async def save(self): |
| 47 | + """Saves the current state of the storage engine.""" |
40 | 48 | raise NotImplementedError |
41 | 49 |
|
| 50 | + @abstractmethod |
42 | 51 | async def close(self): |
| 52 | + """Closes the storage engine.""" |
43 | 53 | raise NotImplementedError |
44 | 54 |
|
| 55 | + @abstractmethod |
45 | 56 | async def delete(self): |
| 57 | + """Deletes the storage.""" |
46 | 58 | raise NotImplementedError |
47 | 59 |
|
| 60 | + @abstractmethod |
48 | 61 | async def update_peers(self, peers: List[Tuple[int, int, str, str, str]]): |
| 62 | + """ |
| 63 | + Update the peers table with the provided information. |
| 64 | +
|
| 65 | + Parameters: |
| 66 | + peers (``List[Tuple[int, int, str, str, str]]``): A list of tuples containing the |
| 67 | + information of the peers to be updated. Each tuple must contain the following |
| 68 | + information: |
| 69 | +
|
| 70 | + - ``int``: The peer id. |
| 71 | + - ``int``: The peer access hash. |
| 72 | + - ``str``: The peer type (user, chat or channel). |
| 73 | + - ``str``: The peer username (if any). |
| 74 | + - ``str``: The peer phone number (if any). |
| 75 | + """ |
49 | 76 | raise NotImplementedError |
50 | 77 |
|
| 78 | + @abstractmethod |
51 | 79 | async def get_peer_by_id(self, peer_id: int): |
| 80 | + """Retrieve a peer by its ID. |
| 81 | +
|
| 82 | + Parameters: |
| 83 | + peer_id (``int``): |
| 84 | + The ID of the peer to retrieve. |
| 85 | + """ |
52 | 86 | raise NotImplementedError |
53 | 87 |
|
| 88 | + @abstractmethod |
54 | 89 | async def get_peer_by_username(self, username: str): |
| 90 | + """Retrieve a peer by its username. |
| 91 | +
|
| 92 | + Parameters: |
| 93 | + username (``str``): |
| 94 | + The username of the peer to retrieve. |
| 95 | + """ |
55 | 96 | raise NotImplementedError |
56 | 97 |
|
| 98 | + @abstractmethod |
57 | 99 | async def get_peer_by_phone_number(self, phone_number: str): |
| 100 | + """Retrieve a peer by its phone number. |
| 101 | +
|
| 102 | + Parameters: |
| 103 | + phone_number (``str``): |
| 104 | + The phone number of the peer to retrieve. |
| 105 | + """ |
58 | 106 | raise NotImplementedError |
59 | 107 |
|
| 108 | + @abstractmethod |
60 | 109 | async def dc_id(self, value: int = object): |
| 110 | + """Get or set the DC ID of the current session. |
| 111 | +
|
| 112 | + Parameters: |
| 113 | + value (``int``, *optional*): |
| 114 | + The DC ID to set. |
| 115 | + """ |
61 | 116 | raise NotImplementedError |
62 | 117 |
|
| 118 | + @abstractmethod |
63 | 119 | async def api_id(self, value: int = object): |
| 120 | + """Get or set the API ID of the current session. |
| 121 | +
|
| 122 | + Parameters: |
| 123 | + value (``int``, *optional*): |
| 124 | + The API ID to set. |
| 125 | + """ |
64 | 126 | raise NotImplementedError |
65 | 127 |
|
| 128 | + @abstractmethod |
66 | 129 | async def test_mode(self, value: bool = object): |
| 130 | + """Get or set the test mode of the current session. |
| 131 | +
|
| 132 | + Parameters: |
| 133 | + value (``bool``, *optional*): |
| 134 | + The test mode to set. |
| 135 | + """ |
67 | 136 | raise NotImplementedError |
68 | 137 |
|
| 138 | + @abstractmethod |
69 | 139 | async def auth_key(self, value: bytes = object): |
| 140 | + """Get or set the authorization key of the current session. |
| 141 | +
|
| 142 | + Parameters: |
| 143 | + value (``bytes``, *optional*): |
| 144 | + The authorization key to set. |
| 145 | + """ |
70 | 146 | raise NotImplementedError |
71 | 147 |
|
| 148 | + @abstractmethod |
72 | 149 | async def date(self, value: int = object): |
| 150 | + """Get or set the date of the current session. |
| 151 | +
|
| 152 | + Parameters: |
| 153 | + value (``int``, *optional*): |
| 154 | + The date to set. |
| 155 | + """ |
73 | 156 | raise NotImplementedError |
74 | 157 |
|
| 158 | + @abstractmethod |
75 | 159 | async def user_id(self, value: int = object): |
| 160 | + """Get or set the user ID of the current session. |
| 161 | +
|
| 162 | + Parameters: |
| 163 | + value (``int``, *optional*): |
| 164 | + The user ID to set. |
| 165 | + """ |
76 | 166 | raise NotImplementedError |
77 | 167 |
|
| 168 | + @abstractmethod |
78 | 169 | async def is_bot(self, value: bool = object): |
| 170 | + """Get or set the bot flag of the current session. |
| 171 | +
|
| 172 | + Parameters: |
| 173 | + value (``bool``, *optional*): |
| 174 | + The bot flag to set. |
| 175 | + """ |
79 | 176 | raise NotImplementedError |
80 | 177 |
|
81 | 178 | async def export_session_string(self): |
| 179 | + """Exports the session string for the current session. |
| 180 | +
|
| 181 | + Returns: |
| 182 | + ``str``: The session string for the current session. |
| 183 | + """ |
82 | 184 | packed = struct.pack( |
83 | 185 | self.SESSION_STRING_FORMAT, |
84 | 186 | await self.dc_id(), |
|
0 commit comments