Added: settings file with time to post

This commit is contained in:
2023-11-27 18:54:40 +03:00
parent 388ebd58c3
commit b14eeb8342
3 changed files with 27 additions and 2 deletions

View File

@@ -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')

View File

@@ -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')

1
settings.toml Normal file
View File

@@ -0,0 +1 @@
time = ["12:00", "14:00", "16:00", "18:00"]