Skip to content

Commit

Permalink
Extend "optional" flag for ConfZFileSource to ignore more exceptions
Browse files Browse the repository at this point in the history
See PR Zuehlke#50 and issue Zuehlke#52.
"optional" now makes it possible to ignore if a command line argument is not
set (through "file_from_cl") or if an environment variable is not
present ("file_from_env"). If set, this also ignores errors if the
file format is invalid.
  • Loading branch information
ajanon committed Jul 6, 2022
1 parent 02298c6 commit 5f4e7ab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
5 changes: 3 additions & 2 deletions confz/confz_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ class ConfZFileSource(ConfZSource):
encoding: str = "utf-8"
"""The encoding of the file. Default is UTF-8."""
optional: bool = False
"""True if this config file is only optional. If set to True no error is thrown
when the file was not found."""
"""True if this config file is only optional. If set to True no error is
thrown when the file was not found, when the environment variable or the command
line argument was not set, or when the file format is invalid."""


@dataclass
Expand Down
17 changes: 8 additions & 9 deletions confz/loaders/file_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def _read_file(
file_path: Path,
file_format: FileFormat,
file_encoding: str,
optional: bool,
) -> dict:
try:
with file_path.open(encoding=file_encoding) as f:
Expand All @@ -98,9 +97,6 @@ def _read_file(
elif file_format == FileFormat.TOML:
file_content = toml.load(f)
except OSError as e:
if optional:
return {}

raise ConfZFileException(
f"Could not open config file '{file_path}'."
) from e
Expand All @@ -109,9 +105,12 @@ def _read_file(

@classmethod
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, confz_source.encoding, confz_source.optional
)
try:
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, confz_source.encoding)
except ConfZFileException as e:
if confz_source.optional:
return
raise e
cls.update_dict_recursively(config, file_content)
12 changes: 12 additions & 0 deletions tests/loaders/test_file_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,15 @@ def test_from_cl_arg_name(monkeypatch):
)
assert config.inner.attr1 == "1 🎉"
assert config.attr2 == "2"


def test_from_cl_arg_optional():
# if not set, should load the config file without errors
config = OuterConfig(
config_sources=[
ConfZFileSource(file_from_cl="--my_config_file", optional=True),
ConfZFileSource(file=ASSET_FOLDER / "config.yml"),
]
)
assert config.inner.attr1 == "1 🎉"
assert config.attr2 == "2"

0 comments on commit 5f4e7ab

Please sign in to comment.