-
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?
Conversation
This PR is being deployed to Railway 🚅 |
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() |
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.
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.
|
||
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 |
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.
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")] |
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?
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 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
No description provided.