Skip to content

Commit

Permalink
Feature/dotenv loading (#42)
Browse files Browse the repository at this point in the history
* 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
fisher60 authored May 8, 2022
1 parent 52013fb commit 4486c33
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 36 deletions.
2 changes: 2 additions & 0 deletions classy_config/loader/auto_loader.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from pathlib import Path
from typing import Any, Dict, MutableMapping

from .env_loader import env_loader
from .json_loader import json_loader
from .loader import Loader
from .toml_loader import toml_loader

__parser_mapping: Dict[str, Loader] = {
".json": json_loader,
".toml": toml_loader,
".env": env_loader,
}


Expand Down
20 changes: 20 additions & 0 deletions classy_config/loader/env_loader.py
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
52 changes: 16 additions & 36 deletions poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ python = "^3.6.2"
pydantic = "^1.9.0"
typing-inspect = "^0.7.1"
toml = "^0.10.2"
python-dotenv = "^0.20.0"

[tool.poetry.dev-dependencies]
pyright = "^0.0.13"
Expand Down
4 changes: 4 additions & 0 deletions tests/configs/test-config.env
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
27 changes: 27 additions & 0 deletions tests/test_env_parsing.py
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

0 comments on commit 4486c33

Please sign in to comment.