-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add env loader and configure auto loader to accept .env * add tests for env parsing * fix redundant env var loading I have added the env var loader as well as tests. The loader will not override any system environment variables, so system env vars are prioritized over the ones passed into the loader. This is consistent with python-dotenv's methods. Currently tests cover that env vars in a .env file that is passed to the autoloader will load the correct values with the correct names. The tests do not ensure that correct env vars from the system are loaded in. The test also does not cover whether multiple configs can be loaded together using this method, but those tests should be covered elsewhere and I do not expect the env loader to interact with multiple configs in a breaking manner. * fix too many spaces in typehint * fix return type hint to be str str * remove unused import * make enumerate more pythonic * remove unused raw_config import * add os.environ adding env var to test, add docstring * remove redundant if check A filepath must always be given and will be validated by the config.register_config function. Therefore no check is needed that the filepath or file exists.
- Loading branch information
Showing
6 changed files
with
70 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from os import environ | ||
from pathlib import Path | ||
from typing import MutableMapping | ||
|
||
from dotenv import load_dotenv | ||
|
||
|
||
def env_loader(filepath: Path) -> MutableMapping[str, str]: | ||
""" | ||
Load the given *.env file and the user's environment variables. | ||
Prioritizes system/user environment variables. Will not override any existing environment variables from the | ||
system. | ||
:param filepath: The path of the file to load from. | ||
:return: The mapping of keys to value loaded from the env file and user's environment. | ||
""" | ||
load_dotenv(filepath) | ||
|
||
return environ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
test_1=1 | ||
test_2=2 | ||
test_3=3 | ||
test_4=4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from os import environ | ||
|
||
from classy_config import ConfigValue | ||
from classy_config.config import register_config | ||
|
||
test_values = [ | ||
"1", | ||
"2", | ||
"3", | ||
"4" | ||
] | ||
|
||
def test_env_config(): | ||
""" | ||
Asserts that an integer can be set to the OS environment and can be properly loaded. | ||
Asserts that multiple string values can be loaded from a .env file. | ||
Does not check whether multipe configs are working together. | ||
Does not cover most typing on .env values. | ||
""" | ||
register_config(filepath="tests/configs/test-config.env", prefix="env") | ||
|
||
environ["test_5"] = "5" | ||
|
||
assert ConfigValue("env.test_5", int) == 5 | ||
|
||
for count, test_val in enumerate(test_values, start=1): | ||
assert ConfigValue(f"env.test_{count}", str) == test_val |