-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.py
More file actions
69 lines (55 loc) · 1.86 KB
/
Copy pathsettings.py
File metadata and controls
69 lines (55 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from pathlib import Path
from typing import Literal
from pydantic import BaseModel, HttpUrl, SecretStr
from pydantic_settings import (
BaseSettings,
PydanticBaseSettingsSource,
SettingsConfigDict,
TomlConfigSettingsSource,
)
BASE_DIR = Path(__file__).resolve().parent.parent
class ApiConfig(BaseModel):
url: HttpUrl = "http://127.0.0.1:8000/"
api_key: SecretStr
class GuildConfig(BaseModel):
id: int
news_channel_id: int | None = None
news_role_id: int | None = None
auto_role_channel_id: int | None = None
class BotConfig(BaseModel):
token: SecretStr
log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR", "FATAL", "CRITICAL"] = (
"INFO"
)
command_prefix: str = "/"
class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_file=".env",
toml_file=str(BASE_DIR / "bot.toml"),
env_nested_delimiter="__",
)
bot: BotConfig
guild: GuildConfig
sith_api: ApiConfig
@classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
"""Define the priority for different sources of configuration.
The configuration is loaded from the following sources
(in descending order of priority) :
1. Arguments passed to the Settings class initialiser.
2. Variables loaded from the `bot.toml` file
3. The default field values for the Settings model.
"""
return (
init_settings,
env_settings,
dotenv_settings,
TomlConfigSettingsSource(settings_cls),
)