mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
1155de82b9
* feat: Add the ability to add extra settings sources * doc: Document "customise settings sources" feature * tests: Add missing test and add change file * Update changes/2107-kozlek.md Co-authored-by: Eric Jolibois <em.jolibois@gmail.com> * improve docs for settings customise_sources * fix docs building * fix test :-( Co-authored-by: Thomas Berdy <thomas.berdy@outlook.com> Co-authored-by: Eric Jolibois <em.jolibois@gmail.com> Co-authored-by: Samuel Colvin <s@muelcolvin.com>
42 lines
978 B
Python
42 lines
978 B
Python
import json
|
|
from pathlib import Path
|
|
from typing import Dict, Any
|
|
|
|
from pydantic import BaseSettings
|
|
|
|
|
|
def json_config_settings_source(settings: BaseSettings) -> Dict[str, Any]:
|
|
"""
|
|
A simple settings source that loads variables from a JSON file
|
|
at the project's root.
|
|
|
|
Here we happen to choose to use the `env_file_encoding` from Config
|
|
when reading `config.json`
|
|
"""
|
|
encoding = settings.__config__.env_file_encoding
|
|
return json.loads(Path('config.json').read_text(encoding))
|
|
|
|
|
|
class Settings(BaseSettings):
|
|
foobar: str
|
|
|
|
class Config:
|
|
env_file_encoding = 'utf-8'
|
|
|
|
@classmethod
|
|
def customise_sources(
|
|
cls,
|
|
init_settings,
|
|
env_settings,
|
|
file_secret_settings,
|
|
):
|
|
return (
|
|
init_settings,
|
|
json_config_settings_source,
|
|
env_settings,
|
|
file_secret_settings,
|
|
)
|
|
|
|
|
|
print(Settings())
|