mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-16 02:50:48 +00:00
✨ new common config loading from .env
This commit is contained in:
@ -20,12 +20,41 @@ from datetime import timedelta
|
||||
from ipaddress import IPv4Address
|
||||
from typing import Any, Set, Dict, Union, Mapping, Optional
|
||||
|
||||
from pydantic.utils import deep_update
|
||||
from pydantic import BaseSettings, IPvAnyAddress
|
||||
from pydantic.env_settings import SettingsError, env_file_sentinel, read_env_file
|
||||
|
||||
|
||||
class BaseConfig(BaseSettings):
|
||||
|
||||
def __init__(
|
||||
__pydantic_self__, # type: ignore
|
||||
_common_config: Optional[Dict[Any, Any]] = None,
|
||||
_env_file: Union[Path, str, None] = env_file_sentinel,
|
||||
_env_file_encoding: Optional[str] = None,
|
||||
_secrets_dir: Union[Path, str, None] = None,
|
||||
**values: Any) -> None:
|
||||
super(BaseSettings,
|
||||
__pydantic_self__).__init__(**__pydantic_self__._build_values(
|
||||
values,
|
||||
_common_config=_common_config,
|
||||
_env_file=_env_file,
|
||||
_env_file_encoding=_env_file_encoding,
|
||||
_secrets_dir=_secrets_dir))
|
||||
|
||||
def _build_values(
|
||||
self,
|
||||
init_kwargs: Dict[str, Any],
|
||||
_common_config: Optional[Dict[Any, Any]] = None,
|
||||
_env_file: Union[Path, str, None] = None,
|
||||
_env_file_encoding: Optional[str] = None,
|
||||
_secrets_dir: Union[Path, str, None] = None,
|
||||
) -> Dict[str, Any]:
|
||||
return deep_update(self._build_secrets_files(_secrets_dir),
|
||||
_common_config or {},
|
||||
self._build_environ(_env_file,
|
||||
_env_file_encoding), init_kwargs)
|
||||
|
||||
def _build_environ(
|
||||
self,
|
||||
_env_file: Union[Path, str, None] = None,
|
||||
@ -92,7 +121,7 @@ class BaseConfig(BaseSettings):
|
||||
return self.__dict__.get(name)
|
||||
|
||||
|
||||
class Env(BaseSettings):
|
||||
class Env(BaseConfig):
|
||||
"""
|
||||
运行环境配置。大小写不敏感。
|
||||
|
||||
@ -109,6 +138,7 @@ class Env(BaseSettings):
|
||||
"""
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
env_file = ".env"
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user