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