From 1e3ce3a55e58748d8fa1a5536772b0c1e75cfb69 Mon Sep 17 00:00:00 2001 From: trueold89 Date: Mon, 5 Aug 2024 17:43:58 +0300 Subject: [PATCH] Add PN_CACHE env value - Add ENV abstract class to static - Init static.env - Add PN_CACHE env --- tubot/db/cache.py | 18 +++++++++--------- tubot/static/abc.py | 26 ++++++++++++++++++++++++++ tubot/static/env.py | 17 +++++++++++++++++ 3 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 tubot/static/env.py diff --git a/tubot/db/cache.py b/tubot/db/cache.py index 2a4fbc6..806c657 100644 --- a/tubot/db/cache.py +++ b/tubot/db/cache.py @@ -7,20 +7,20 @@ # Imports from tubot.db.abc import CacheDB from tubot.db.types import CacheDBTypes, UserStates, User +from tubot.static.env import PN_CACHE from pickle import loads, dumps from aiofiles.ospath import isdir, isfile from aiofiles.os import mkdir from aiofiles import open from asyncio import sleep -CACHE_DIR = "foo" # УДАЛИ ПОТОМ ЕБЛАН - class PythonCache(CacheDB): """ Native python implementation of Cache DataBase """ + CACHE_DIR = PN_CACHE()() _ctype = CacheDBTypes.PythonPKL users: dict dirs: dict @@ -31,7 +31,7 @@ class PythonCache(CacheDB): async def _init(self) -> bool: self.users = {} self.dirs = {} - if await isfile(f"{CACHE_DIR}/user_cache.pkl"): + if await isfile(f"{self.CACHE_DIR}/user_cache.pkl"): try: await self._load_pkl() except Exception: @@ -39,17 +39,17 @@ class PythonCache(CacheDB): return True async def _load_pkl(self) -> None: - if not await isdir(CACHE_DIR): - await mkdir(CACHE_DIR) - async with open(f"{CACHE_DIR}/user_cache.pkl", "rb") as file: + if not await isdir(self.CACHE_DIR): + await mkdir(self.CACHE_DIR) + async with open(f"{self.CACHE_DIR}/user_cache.pkl", "rb") as file: buffer = await file.read() pkl = loads(buffer) self.users = pkl async def _save_pkl(self) -> None: - if not await isdir(CACHE_DIR): - await mkdir(CACHE_DIR) - async with open(f"{CACHE_DIR}/user_cache.pkl", "wb") as file: + if not await isdir(self.CACHE_DIR): + await mkdir(self.CACHE_DIR) + async with open(f"{self.CACHE_DIR}/user_cache.pkl", "wb") as file: await file.write(dumps(self.users)) async def __validate__(self) -> bool: diff --git a/tubot/static/abc.py b/tubot/static/abc.py index d79f1ce..d2e3f58 100644 --- a/tubot/static/abc.py +++ b/tubot/static/abc.py @@ -6,6 +6,7 @@ # Imports from abc import ABC, abstractmethod +from os import environ class IValidatable(ABC): @@ -22,3 +23,28 @@ class IValidatable(ABC): :return: Object validity boolean """ raise NotImplementedError + + +class ENV(object): + + _name: str | None = None + DEFAULT: str + + def __init__(self) -> None: + if self._name is None or self.DEFAULT is None: + raise NotImplementedError + + @property + def from_os(self) -> str | None: + if self._name is not None: + return environ.get(self._name) + + @property + def value(self) -> str: + val = self.from_os + if val is not None: + return val + return self.DEFAULT + + def __call__(self) -> str: + return self.value diff --git a/tubot/static/env.py b/tubot/static/env.py new file mode 100644 index 0000000..9a4e66d --- /dev/null +++ b/tubot/static/env.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- + +############ +# ENV Vars # +############ + +# Imports +from tubot.static.abc import ENV + + +class PN_CACHE(ENV): + """ + Python Native Cache dir + """ + + _name = "PN_CACHE" + DEFAULT = "/etc/tubot"