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

Conversation

onerandomusername
Copy link
Member

No description provided.

@onerandomusername onerandomusername added the enhancement New feature or request label May 25, 2021
@onerandomusername onerandomusername self-assigned this May 25, 2021
@railway-app
Copy link

railway-app bot commented May 25, 2021

This PR is being deployed to Railway 🚅

[View Deployment]

Comment on lines +40 to +59
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))
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()
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.

Comment on lines +21 to +33

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
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.

"./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?

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants