From b14eeb8342fe2889439c7ac6dda46ab984e02dd6 Mon Sep 17 00:00:00 2001 From: Sergey Elpashev Date: Mon, 27 Nov 2023 18:54:40 +0300 Subject: [PATCH] Added: settings file with time to post --- main.py | 2 +- neuroapi/config.py | 26 +++++++++++++++++++++++++- settings.toml | 1 + 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 settings.toml diff --git a/main.py b/main.py index e4f8657..2cff85e 100644 --- a/main.py +++ b/main.py @@ -18,7 +18,7 @@ dp = Dispatcher() @dp.message(CommandStart()) async def start_message(message: types.Message): - await message.answer('Абоба') + await message.answer('Добро пожаловать в бота ') handlers_dir = join(dirname(__file__), 'handlers') diff --git a/neuroapi/config.py b/neuroapi/config.py index 6beabeb..9dd571d 100644 --- a/neuroapi/config.py +++ b/neuroapi/config.py @@ -1,8 +1,12 @@ import os -from typing import Self +import tomllib +from typing import List, Self, TypeVar +from attr import dataclass from dotenv import load_dotenv +from .types._helpers import * + class _Singleton: _instances = {} @@ -11,9 +15,29 @@ class _Singleton: if cls not in cls._instances: cls._instances[cls] = super(_Singleton, cls).__new__(cls) return cls._instances[cls] + +@dataclass +class Settings: + time: List[str] + + @staticmethod + def from_dict(obj: Any) -> 'Settings': + assert isinstance(obj, dict) + time = from_list(from_str, obj.get("time", [])) + return Settings(time) + + def to_dict(self) -> dict: + result: dict = {} + result['time'] = from_list(from_str, self.time) + return result class Config(_Singleton): api_url: str + settings: Settings def __init__(self): load_dotenv(os.path.join(os.path.dirname(__file__), '..', '.env')) + if not os.path.exists(os.path.join(os.path.dirname(__file__), '..', 'settings.toml')): raise Exception('Settings.toml must be in root folder') + with open(os.path.join(os.path.dirname(__file__), '..', 'settings.toml'), 'rb') as f: + settings = tomllib.load(f) + self.settings = Settings.from_dict(settings) self.api_url = os.environ.get('API_URL') \ No newline at end of file diff --git a/settings.toml b/settings.toml new file mode 100644 index 0000000..5e0b040 --- /dev/null +++ b/settings.toml @@ -0,0 +1 @@ +time = ["12:00", "14:00", "16:00", "18:00"] \ No newline at end of file