2024-08-05 12:40:52 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
####################################
|
|
|
|
# DataBase module abstract classes #
|
|
|
|
####################################
|
|
|
|
|
|
|
|
# Imports
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
from tubot.static.abc import IValidatable
|
2024-08-06 10:58:19 +00:00
|
|
|
from tubot.db.types import CacheDBTypes, User
|
2024-08-05 12:40:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CacheDB(IValidatable, ABC):
|
|
|
|
"""
|
|
|
|
Abstract class for CacheDB
|
|
|
|
"""
|
|
|
|
|
|
|
|
_ctype: CacheDBTypes
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
if self._ctype is None:
|
|
|
|
raise NotImplementedError("CacheDB type not implemented")
|
|
|
|
|
|
|
|
# Users
|
|
|
|
|
|
|
|
@abstractmethod
|
2024-08-06 10:58:19 +00:00
|
|
|
async def write_user(self, tg_id: int, user: User) -> None:
|
2024-08-05 12:40:52 +00:00
|
|
|
"""
|
2024-08-06 10:58:19 +00:00
|
|
|
Writes user to cache db
|
2024-08-05 12:40:52 +00:00
|
|
|
|
|
|
|
:param tg_id: User telegram id
|
2024-08-06 10:58:19 +00:00
|
|
|
:param user: User object
|
2024-08-05 12:40:52 +00:00
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@abstractmethod
|
2024-08-06 10:58:19 +00:00
|
|
|
async def read_user(self, tg_id: int) -> User:
|
2024-08-05 12:40:52 +00:00
|
|
|
"""
|
2024-08-06 10:58:19 +00:00
|
|
|
Writes user to cache db
|
2024-08-05 12:40:52 +00:00
|
|
|
|
|
|
|
:param tg_id: User telegram id
|
2024-08-06 10:58:19 +00:00
|
|
|
:return: User object
|
2024-08-05 12:40:52 +00:00
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2024-08-07 15:48:04 +00:00
|
|
|
@abstractmethod
|
|
|
|
async def chech_user_existing(self, tg_id: int):
|
|
|
|
"""
|
|
|
|
Checks if user exist in db
|
|
|
|
|
|
|
|
:param tg_id: User telegram id
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2024-08-05 12:40:52 +00:00
|
|
|
# Dirs
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
async def cache_dirs(self, dirs: dict, expire: int) -> None:
|
|
|
|
"""
|
|
|
|
Cache dirs from DirectoryGetter
|
|
|
|
|
|
|
|
:param dirs: Dirs dict
|
|
|
|
:param expire: Expire time (in seconds)
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@property
|
|
|
|
@abstractmethod
|
|
|
|
async def get_dirs(self) -> dict:
|
|
|
|
"""
|
|
|
|
Returns precached dirs
|
|
|
|
|
|
|
|
:return: Dirs dict
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|