Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend "optional" flag for ConfZFileSource to ignore more exceptions #53

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 or when the environment variable or the
command line argument were not set."""


@dataclass
Expand Down
20 changes: 12 additions & 8 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,17 @@ def _read_file(

@classmethod
def populate_config(cls, config: dict, confz_source: ConfZFileSource):
file_path = cls._get_filename(confz_source)
try:
file_path = cls._get_filename(confz_source)
except ConfZFileException as e:
if confz_source.optional:
return
raise e
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_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"