Add InitBuilder

This commit is contained in:
trueold89 2024-08-06 14:59:50 +03:00
parent 697dac6922
commit c2ab00680f
Signed by: trueold89
GPG Key ID: C122E85DD49E6B30
4 changed files with 178 additions and 3 deletions

View File

@ -13,5 +13,5 @@ class GetterTypes(Enum):
Types of getters Types of getters
""" """
OS = "Python os module" OS = "os"
Jellyfin = "Jelyfin API" Jellyfin = "jellyfin"

View File

@ -24,3 +24,84 @@ class REDIS_HOST(ENV):
_name = "REDIS_HOST" _name = "REDIS_HOST"
DEFAULT = "localhost:6379" DEFAULT = "localhost:6379"
class CACHE_TYPE(ENV):
"""
CacheDB Type
"""
_name = "CACHE_TYPE"
DEFAULT = "python"
class DIR_GETTER(ENV):
"""
DirGetter Type
"""
_name = "DIR_GETTER"
DEFAULT = "os"
class DG_OS_FOLDER(ENV):
"""
Path to parent directory for OS_DirGetter
"""
_name = "DG_OS_FOLDER"
DEFAULT = "/mnt/Media"
class DG_JELLYFIN_HOST(ENV):
"""
Jellyfin Server API host
"""
_name = "DG_JELLYFIN_HOST"
DEFAULT = "http://localhost:8096"
class DG_JELLYFIN_TOKEN(ENV):
"""
Jellyfin API key
"""
_name = "DG_JELLYFIN_TOKEN"
DEFAULT = ""
class TORRENT_SERVER(ENV):
"""
Torrent Server Type
"""
_name = "TORRENT_SERVER"
DEFAULT = "qbit"
class TS_USER(ENV):
"""
Torrent Server auth username
"""
_name = "TS_USER"
DEFAULT = ""
class TS_PASSWORD(ENV):
"""
Torrent Server auth password
"""
_name = "TS_PASSWORD"
DEFAULT = ""
class TS_HOST(ENV):
"""
Torrent Server host
"""
_name = "TS_HOST"
DEFAULT = "http://localhost"

94
tubot/static/init.py Normal file
View File

@ -0,0 +1,94 @@
# -*- coding: utf-8 -*-
######################
# Init static module #
######################
# Imports
from typing import Iterable
from tubot.static import env
from tubot.static.functions import validate
from tubot.torrent.apis import qBitTorrent
from tubot.torrent.types import ServerTypes
from tubot.torrent.abc import TorrentAPI
from tubot.dirgetter.types import GetterTypes
from tubot.dirgetter.abc import DirGetter
from tubot.dirgetter.getter import OSGetter, Jellyfin
from tubot.db.types import CacheDBTypes
from tubot.db.abc import CacheDB
from tubot.db.cache import PythonCache, RedisCache
from asyncio import create_task, gather
class InitBuilder(object):
"""
Init all bot modules
"""
TORRENT_SERVER: ServerTypes | TorrentAPI
DG: GetterTypes | DirGetter
CACHE: CacheDBTypes | CacheDB
def set_torrent_server(self, server_type: ServerTypes) -> "InitBuilder":
self.TORRENT_SERVER = server_type
return self
def set_directory_getter(self, dg_type: GetterTypes) -> "InitBuilder":
self.DG = dg_type
return self
def set_cache_type(self, cache_type: CacheDBTypes) -> "InitBuilder":
self.CACHE = cache_type
return self
async def init_ts(self) -> None:
host = env.TS_HOST()()
user = env.TS_USER()()
pwd = env.TS_PASSWORD()()
match self.TORRENT_SERVER:
case ServerTypes.qBitTorrent:
self.TORRENT_SERVER = qBitTorrent(host, user, pwd)
case _:
raise TypeError
await validate(self.TORRENT_SERVER)
async def init_dg(self) -> None:
match self.DG:
case GetterTypes.OS:
base_dir = env.DG_OS_FOLDER()()
self.DG = OSGetter(base_dir)
case GetterTypes.Jellyfin:
host = env.DG_JELLYFIN_HOST()()
key = env.DG_JELLYFIN_TOKEN()()
self.DG = Jellyfin(host, key)
case _:
raise TypeError
await validate(self.DG)
async def init_cache(self) -> None:
match self.CACHE:
case CacheDBTypes.PythonPKL:
self.CACHE = PythonCache()
case CacheDBTypes.Redis:
host = env.REDIS_HOST()()
self.CACHE = RedisCache(host)
case _:
raise TypeError
await validate(self.CACHE)
async def init_all_modules(self) -> None:
tasks = (create_task(self.init_ts()), create_task(self.init_dg()), create_task(self.init_cache()))
await gather(*tasks)
@property
def tuple(self) -> Iterable:
return (self.TORRENT_SERVER, self.DG, self.CACHE)
async def init_modules() -> Iterable:
ts = ServerTypes(env.TORRENT_SERVER()())
dg = GetterTypes(env.DIR_GETTER()())
cache = CacheDBTypes(env.CACHE_TYPE()())
builder = InitBuilder().set_torrent_server(ts).set_directory_getter(dg).set_cache_type(cache)
await builder.init_all_modules()
return builder.tuple

View File

@ -24,7 +24,7 @@ class ServerTypes(Enum):
Types of Torrent servers API's Types of Torrent servers API's
""" """
qBitTorrent = "qBitTorrent Remote API" qBitTorrent = "qbit"
class TorrentFromServer(object): class TorrentFromServer(object):