Skip to content
This repository has been archived by the owner on Aug 1, 2021. It is now read-only.

feat: add enviroment configuration system #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CLIENT_ID = ''
CLIENT_SECRET = ''
99 changes: 99 additions & 0 deletions config.py
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")]
Copy link
Contributor

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?



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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 (file_exists). I don't think there's a need to specialize here.



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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toml.load already returns a dictionary, there's no need to wrap it again

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 toml_user_config_source or {} to signify this is where we handle empty config.



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")
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates

from .config import CONFIG # noqa: F401 This should be removed once configuration is used.

templates = Jinja2Templates(directory="templates")

Expand Down
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ license = "MIT"

[tool.poetry.dependencies]
python = "^3.8"
aiofiles = "^0.7.0"
coloredlogs = "^15.0"
pydantic = "^1.8.2"
starlette = "^0.14.2"
jinja2 = "^3.0.1"
aiofiles = "^0.7.0"
pydantic = "^1.8.2"
python-dotenv = "^0.17.1"
starlette = "^0.14.2"
toml = "^0.10.2"
uvicorn = {extras = ["standard"], version = "^0.13.4"}

[tool.poetry.extras]
Expand Down