Skip to content

Commit

Permalink
Merge pull request #32 from Zuehlke/bugfix/unicode-encoding
Browse files Browse the repository at this point in the history
Bugfix/unicode encoding
  • Loading branch information
silvanmelchior authored Dec 16, 2021
2 parents 233e550 + cabcde2 commit 8021c84
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 22 deletions.
4 changes: 3 additions & 1 deletion confz/confz_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ConfZFileSource(ConfZSource):
"""The file specified above can optionally be relative to this folder."""
format: Optional[FileFormat] = None
"""The format of the config file. If not specified, it will be inferred from the file ending."""
encoding: str = 'utf-8'
"""The encoding of the file. Default is UTF-8."""


@dataclass
Expand All @@ -56,7 +58,7 @@ class ConfZEnvSource(ConfZSource):
remap: Optional[Dict[str, str]] = None
"""Certain environment variables can be mapped to config arguments with a different name."""
file: Optional[Path] = None
"""Built in .env file loading with lower than environment precedence."""
"""Built in .env file loading with lower than environment precedence. Uses UTF-8 for decoding."""


@dataclass
Expand Down
1 change: 1 addition & 0 deletions confz/loaders/env_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def populate_config(cls, config: dict, confz_source: ConfZEnvSource):
origin_env_vars = os.environ
if confz_source.file is not None:
origin_env_vars = {**dotenv_values(confz_source.file), **origin_env_vars}

env_vars = dict()
for env_var in origin_env_vars:
var_name = env_var
Expand Down
6 changes: 3 additions & 3 deletions confz/loaders/file_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ def _get_format(cls, file_path: Path, file_format: Optional[FileFormat]) -> File
return suffix_format

@classmethod
def _read_file(cls, file_path: Path, file_format: FileFormat) -> dict:
def _read_file(cls, file_path: Path, file_format: FileFormat, file_encoding: str) -> dict:
try:
with file_path.open() as f:
with file_path.open(encoding=file_encoding) as f:
if file_format == FileFormat.YAML:
file_content = yaml.safe_load(f)
elif file_format == FileFormat.JSON:
Expand All @@ -81,5 +81,5 @@ def _read_file(cls, file_path: Path, file_format: FileFormat) -> dict:
def populate_config(cls, config: dict, confz_source: ConfZFileSource):
file_path = cls._get_filename(confz_source)
file_format = cls._get_format(file_path, confz_source.format)
file_content = cls._read_file(file_path, file_format)
file_content = cls._read_file(file_path, file_format, confz_source.encoding)
cls.update_dict_recursively(config, file_content)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "confz"
version = "1.2.1"
version = "1.2.2"
description = "ConfZ is a configuration management library for Python based on pydantic."
license = "MIT"
authors = ["Zühlke"]
Expand Down
2 changes: 1 addition & 1 deletion tests/assets/config.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"inner": {"attr1": "1"}, "attr2": "2"}
{"inner": {"attr1": "1 🎉"}, "attr2": "2"}
2 changes: 1 addition & 1 deletion tests/assets/config.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
inner:
attr1: 1
attr1: 1 🎉
attr2: 2
2 changes: 1 addition & 1 deletion tests/assets/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
inner:
attr1: 1
attr1: 1 🎉
attr2: 2
28 changes: 14 additions & 14 deletions tests/loaders/test_file_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@


class InnerConfig(ConfZ):
attr1: int
attr1: str


class OuterConfig(ConfZ):
attr2: int
attr2: str
inner: InnerConfig


def test_json_file():
config = OuterConfig(config_sources=ConfZFileSource(
file=ASSET_FOLDER / 'config.json'
))
assert config.inner.attr1 == 1
assert config.attr2 == 2
assert config.inner.attr1 == '1 🎉'
assert config.attr2 == '2'


def test_yaml_file():
config = OuterConfig(config_sources=ConfZFileSource(
file=ASSET_FOLDER / 'config.yml'
))
assert config.inner.attr1 == 1
assert config.attr2 == 2
assert config.inner.attr1 == '1 🎉'
assert config.attr2 == '2'


def test_custom_file():
Expand All @@ -44,8 +44,8 @@ def test_custom_file():
file=ASSET_FOLDER / 'config.txt',
format=FileFormat.YAML
))
assert config.inner.attr1 == 1
assert config.attr2 == 2
assert config.inner.attr1 == '1 🎉'
assert config.attr2 == '2'


def test_invalid_file():
Expand All @@ -71,8 +71,8 @@ def test_from_env(monkeypatch):
file_from_env=env_var,
folder=ASSET_FOLDER
))
assert config.inner.attr1 == 1
assert config.attr2 == 2
assert config.inner.attr1 == '1 🎉'
assert config.attr2 == '2'


def test_from_cl_arg_idx(monkeypatch):
Expand All @@ -92,8 +92,8 @@ def test_from_cl_arg_idx(monkeypatch):
file_from_cl=cl_arg_idx,
folder=ASSET_FOLDER
))
assert config.inner.attr1 == 1
assert config.attr2 == 2
assert config.inner.attr1 == '1 🎉'
assert config.attr2 == '2'


def test_from_cl_arg_name(monkeypatch):
Expand All @@ -113,5 +113,5 @@ def test_from_cl_arg_name(monkeypatch):
file_from_cl=cl_arg_name,
folder=ASSET_FOLDER
))
assert config.inner.attr1 == 1
assert config.attr2 == 2
assert config.inner.attr1 == '1 🎉'
assert config.attr2 == '2'

0 comments on commit 8021c84

Please sign in to comment.