From 47248f10ab045d699d68cea454f3ec41a7440b5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Kr=C4=8Dek?= Date: Sun, 13 Oct 2024 21:05:51 +0200 Subject: [PATCH] Add computed properties to settings --- requirements.txt | 3 ++- telegram_downloader_bot/security.py | 3 --- telegram_downloader_bot/settings.py | 16 +++++++++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/requirements.txt b/requirements.txt index a2dd334..8e74b3a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,5 @@ tiktok_downloader==0.3.5 uvloop==0.19.0 tgcrypto==1.2.5 sentry-sdk==2.15.0 -pydantic-settings==2.5.2 \ No newline at end of file +pydantic-settings==2.5.2 +pydantic==2.9.2 \ No newline at end of file diff --git a/telegram_downloader_bot/security.py b/telegram_downloader_bot/security.py index 6c4ccc8..9e6fb4d 100644 --- a/telegram_downloader_bot/security.py +++ b/telegram_downloader_bot/security.py @@ -3,9 +3,6 @@ from functools import wraps from telegram_downloader_bot.logger import log from telegram_downloader_bot.settings import settings -allowed_ids = settings.allowed_ids.split(",") -allowed_ids = [int(x) for x in allowed_ids] - def protected(func): @wraps(func) diff --git a/telegram_downloader_bot/settings.py b/telegram_downloader_bot/settings.py index 3cdc0b2..52017ff 100644 --- a/telegram_downloader_bot/settings.py +++ b/telegram_downloader_bot/settings.py @@ -1,5 +1,7 @@ import os +from functools import cached_property from pydantic_settings import BaseSettings +from pydantic import computed_field class Settings(BaseSettings): @@ -46,10 +48,22 @@ class Settings(BaseSettings): api_id: int api_hash: str bot_token: str - storage: os.path + storage: str allowed_ids: str log_level: str + @computed_field + @property + def tt_hash_file(self) -> str: + return os.path.join(settings.storage, "tt_hashes.pickle") + + @computed_field + @cached_property + def allowed_ids_list(self) -> list: + allowed_ids = settings.allowed_ids.split(",") + allowed_ids = [int(x) for x in allowed_ids] + return allowed_ids + class Config: env_file = ".env"