mirror of
https://github.com/MrSedan/neuro-reply-bot-reworked.git
synced 2026-01-15 05:59:43 +03:00
Use pydantic settings instead of python-dotenv
This commit is contained in:
@@ -1,45 +1,10 @@
|
|||||||
import os
|
from typing import Optional
|
||||||
import tomllib
|
|
||||||
from typing import List, Optional
|
|
||||||
|
|
||||||
from attr import dataclass
|
from pydantic import Field
|
||||||
from dotenv import load_dotenv
|
from pydantic_settings import BaseSettings
|
||||||
|
|
||||||
from neuroapi.types import Singleton
|
|
||||||
|
|
||||||
from .types._helpers import *
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
class GlobalConfig(BaseSettings):
|
||||||
class Settings:
|
api_url: str = Field("http://localhost:3000", alias='API_URL')
|
||||||
time: List[str]
|
token: Optional[str] = Field(None, alias='TOKEN')
|
||||||
|
proxy_token: Optional[str] = Field(None, alias='PROXY_TOKEN')
|
||||||
@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
|
|
||||||
token: Optional[str]
|
|
||||||
proxy_token: Optional[str]
|
|
||||||
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')
|
|
||||||
self.token = os.environ.get('TOKEN')
|
|
||||||
if self.token == '':
|
|
||||||
self.token = None
|
|
||||||
self.proxy_token = os.environ.get('PROXY_TOKEN')
|
|
||||||
if self.proxy_token == '':
|
|
||||||
self.proxy_token = None
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
aiogram==3.1.1
|
aiogram==3.1.1
|
||||||
aioschedule @ https://github.com/AleksHeller/python-aioschedule/archive/refs/heads/master.zip
|
aioschedule @ https://github.com/AleksHeller/python-aioschedule/archive/refs/heads/master.zip
|
||||||
python-dotenv==1.0.0
|
|
||||||
requests==2.31.0
|
requests==2.31.0
|
||||||
python-dateutil==2.8.2
|
python-dateutil==2.8.2
|
||||||
aiohttp==3.8.6
|
aiohttp==3.8.6
|
||||||
|
pydantic==2.3.0
|
||||||
|
pydantic-settings==2.1.0
|
||||||
Reference in New Issue
Block a user