From ba696f4c5610cbc36804dcf22c1ec1e3d0eec4e5 Mon Sep 17 00:00:00 2001 From: trueold89 Date: Mon, 5 Aug 2024 15:40:52 +0300 Subject: [PATCH] Add CacheDB abstract class - init tubot.db - init tubot.db.types - init tubot.db.abc - add CacheDBTypes enum - add UserStates enum - add CacheDB abstract class --- setup.py | 2 +- tubot/db/abc.py | 94 +++++++++++++++++++++++++++++++++++++++++++++++ tubot/db/types.py | 22 +++++++++++ 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 tubot/db/abc.py create mode 100644 tubot/db/types.py diff --git a/setup.py b/setup.py index d86025e..302ad49 100644 --- a/setup.py +++ b/setup.py @@ -8,5 +8,5 @@ setup( author_email="root@orudo.ru", description="A simple Telegram bot that will allow you to upload torrent files / magnet links to a remote Torrent server (qBitTorrent, Transmission, etc.)", install_requires=["aiohttp>=3.10.0", "aiofiles>=24.1.0", "aiofiles>=24.1.0"], - packages=["tubot", "tubot.static", "tubot.torrent", "tubot.dirgetter"], + packages=["tubot", "tubot.static", "tubot.torrent", "tubot.dirgetter", "db"], ) diff --git a/tubot/db/abc.py b/tubot/db/abc.py new file mode 100644 index 0000000..783d78c --- /dev/null +++ b/tubot/db/abc.py @@ -0,0 +1,94 @@ +# -*- coding: utf-8 -*- + +#################################### +# DataBase module abstract classes # +#################################### + +# Imports +from abc import ABC, abstractmethod +from tubot.static.abc import IValidatable +from tubot.db.types import CacheDBTypes, UserStates + + +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 + async def add_user(self, tg_id: int, name: str) -> None: + """ + Add user to cache db + + :param tg_id: User telegram id + :param name: User telegram name + """ + raise NotImplementedError + + @abstractmethod + async def user_state(self, tg_id: int) -> UserStates: + """ + Get user state + + :param tg_id: User telegram id + :return: Status of user + """ + raise NotImplementedError + + @abstractmethod + async def change_user_state(self, tg_id: int, status: UserStates) -> None: + """ + Change user state + + :param tg_id: User telegram id + :param status: New user status + """ + raise NotImplementedError + + @abstractmethod + async def auth_user(self, tg_id: int) -> None: + """ + Auth user + + :param tg_id: User telegram id + """ + raise NotImplementedError + + @abstractmethod + async def is_user_auth(self, tg_id: int) -> bool: + """ + Check if user is already auth + + :param tg_id: User telegram id + """ + raise NotImplementedError + + # 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 diff --git a/tubot/db/types.py b/tubot/db/types.py new file mode 100644 index 0000000..b84dc69 --- /dev/null +++ b/tubot/db/types.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- + +############################# +# Types for DataBase module # +############################# + +# Imports +from enum import Enum + + +class CacheDBTypes(Enum): + """ + Types of CacheDB + """ + ... + + +class UserStates(Enum): + """ + Types of User status + """ + ...