Add PN_CACHE env value

- Add ENV abstract class to static
- Init static.env
- Add PN_CACHE env
This commit is contained in:
trueold89 2024-08-05 17:43:58 +03:00
parent 015e01b6be
commit 1e3ce3a55e
Signed by: trueold89
GPG Key ID: C122E85DD49E6B30
3 changed files with 52 additions and 9 deletions

View File

@ -7,20 +7,20 @@
# Imports # Imports
from tubot.db.abc import CacheDB from tubot.db.abc import CacheDB
from tubot.db.types import CacheDBTypes, UserStates, User from tubot.db.types import CacheDBTypes, UserStates, User
from tubot.static.env import PN_CACHE
from pickle import loads, dumps from pickle import loads, dumps
from aiofiles.ospath import isdir, isfile from aiofiles.ospath import isdir, isfile
from aiofiles.os import mkdir from aiofiles.os import mkdir
from aiofiles import open from aiofiles import open
from asyncio import sleep from asyncio import sleep
CACHE_DIR = "foo" # УДАЛИ ПОТОМ ЕБЛАН
class PythonCache(CacheDB): class PythonCache(CacheDB):
""" """
Native python implementation of Cache DataBase Native python implementation of Cache DataBase
""" """
CACHE_DIR = PN_CACHE()()
_ctype = CacheDBTypes.PythonPKL _ctype = CacheDBTypes.PythonPKL
users: dict users: dict
dirs: dict dirs: dict
@ -31,7 +31,7 @@ class PythonCache(CacheDB):
async def _init(self) -> bool: async def _init(self) -> bool:
self.users = {} self.users = {}
self.dirs = {} self.dirs = {}
if await isfile(f"{CACHE_DIR}/user_cache.pkl"): if await isfile(f"{self.CACHE_DIR}/user_cache.pkl"):
try: try:
await self._load_pkl() await self._load_pkl()
except Exception: except Exception:
@ -39,17 +39,17 @@ class PythonCache(CacheDB):
return True return True
async def _load_pkl(self) -> None: async def _load_pkl(self) -> None:
if not await isdir(CACHE_DIR): if not await isdir(self.CACHE_DIR):
await mkdir(CACHE_DIR) await mkdir(self.CACHE_DIR)
async with open(f"{CACHE_DIR}/user_cache.pkl", "rb") as file: async with open(f"{self.CACHE_DIR}/user_cache.pkl", "rb") as file:
buffer = await file.read() buffer = await file.read()
pkl = loads(buffer) pkl = loads(buffer)
self.users = pkl self.users = pkl
async def _save_pkl(self) -> None: async def _save_pkl(self) -> None:
if not await isdir(CACHE_DIR): if not await isdir(self.CACHE_DIR):
await mkdir(CACHE_DIR) await mkdir(self.CACHE_DIR)
async with open(f"{CACHE_DIR}/user_cache.pkl", "wb") as file: async with open(f"{self.CACHE_DIR}/user_cache.pkl", "wb") as file:
await file.write(dumps(self.users)) await file.write(dumps(self.users))
async def __validate__(self) -> bool: async def __validate__(self) -> bool:

View File

@ -6,6 +6,7 @@
# Imports # Imports
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from os import environ
class IValidatable(ABC): class IValidatable(ABC):
@ -22,3 +23,28 @@ class IValidatable(ABC):
:return: Object validity boolean :return: Object validity boolean
""" """
raise NotImplementedError 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

17
tubot/static/env.py Normal file
View File

@ -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"