-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add enviroment configuration system #3
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
CLIENT_ID = '' | ||
CLIENT_SECRET = '' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import logging | ||
import os | ||
import typing | ||
from pathlib import Path | ||
from typing import Any, Dict, Tuple | ||
|
||
import toml | ||
from pydantic import BaseSettings as PydanticBaseSettings | ||
from pydantic.env_settings import SettingsSourceCallable | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
CONFIG_PATHS: list = [ | ||
f"{os.getcwd()}/config.toml", | ||
f"{os.getcwd()}/site/config.toml", | ||
"./config.toml", | ||
] | ||
|
||
DEFAULT_CONFIG_PATHS = [os.path.join(os.path.dirname(__file__), "config-default.toml")] | ||
|
||
|
||
def determine_file_path( | ||
paths: typing.Union[list, tuple], config_type: str = "default" | ||
) -> typing.Union[str, None]: | ||
"""Determine the location of a configuration file, given a list of paths.""" | ||
path = None | ||
for file_path in paths: | ||
config_file = Path(file_path) | ||
if (config_file).exists(): | ||
path = config_file | ||
log.debug(f"Found {config_type} config at {file_path}") | ||
break | ||
return path or None | ||
Comment on lines
+21
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be improved. If you check the later comment I made, we may not even need this, or we could treat it as a basic utility function and move it elsewhere ( |
||
|
||
|
||
DEFAULT_CONFIG_PATH = determine_file_path(DEFAULT_CONFIG_PATHS) | ||
USER_CONFIG_PATH = determine_file_path(CONFIG_PATHS, config_type="") | ||
|
||
|
||
def toml_default_config_source(settings: PydanticBaseSettings) -> Dict[str, Any]: | ||
""" | ||
A simple settings source that loads variables from a toml file. | ||
|
||
from within the module's source folder. | ||
""" | ||
if DEFAULT_CONFIG_PATH is not None: | ||
return dict(**toml.load(DEFAULT_CONFIG_PATH)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return dict() | ||
|
||
|
||
def toml_user_config_source(settings: PydanticBaseSettings) -> Dict[str, Any]: | ||
""" | ||
A simple settings source that loads variables from a toml file. | ||
|
||
from within the module's source folder. | ||
""" | ||
if USER_CONFIG_PATH is not None: | ||
return dict(**toml.load(USER_CONFIG_PATH)) | ||
return dict() | ||
Comment on lines
+40
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These do literally the same thing with two words changed. That is a lot of boiler bloat we absolutely do not need. Just loop over the available config paths and load from them. Even better if you use a dictionary here: CONFIG_PATHS = {
"THE_PATH_ONE": toml.load,
} And then just loop and call the function. If someone needs a custom loader they can drop in a function. Also this "return empty dict" feels wrong. It should return None, so it's really easy to tell if it was provided. If we're just passing this through BaseSettings then in BaseSettings we can do |
||
|
||
|
||
class BaseSettings(PydanticBaseSettings): | ||
""" | ||
Custom Pydantic base class for settings, with a Config that also loads directly from toml. | ||
|
||
Loading methods are from toml config files, class defaults, passed arguments, and env variables. | ||
""" | ||
|
||
class Config: | ||
"""BaseSettings custom default Config.""" | ||
|
||
extra = "ignore" | ||
env_file_encoding = "utf-8" | ||
|
||
@classmethod | ||
def customise_sources( | ||
cls, | ||
init_settings: SettingsSourceCallable, | ||
env_settings: SettingsSourceCallable, | ||
file_secret_settings: SettingsSourceCallable, | ||
) -> Tuple[SettingsSourceCallable, ...]: | ||
"""Modify Pydantic default settings to add toml as a source and priortise env variables.""" | ||
return ( | ||
env_settings, | ||
init_settings, | ||
file_secret_settings, | ||
toml_user_config_source, | ||
toml_default_config_source, | ||
) | ||
|
||
|
||
class SiteConfig(BaseSettings): | ||
"""Pydantic model for Site Settings.""" | ||
|
||
CLIENT_ID: str | ||
CLIENT_SECRET: str | ||
|
||
|
||
CONFIG = SiteConfig(_env_file="./.env") |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason this is a list? I feel like if we want multiple default config paths like we have multiple regular config paths, then we should be parsing over
CONFIG_PATHS
and mirroring it, rather than having a single element list?