Skip to content

Commit

Permalink
fix: missing file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Pajot authored and gpajot committed Sep 23, 2022
1 parent 574f730 commit 61aa522
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ When creating your config, you can specify at least one of those two attributes:
> 💡 When supplying both, if the env var is not set, it will use `PATH`.
User constructs will be expanded.
If the file does not exist it will be created (not parent directories though).
If the file does not exist it will be created.
You can specify the file mode via `Config.FILE_MODE`.

The config can be loaded from multiple files, see [fnmatch](https://docs.python.org/3/library/fnmatch.html) for syntax.
Expand Down
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 = "zenconfig"
version = "1.4.0"
version = "1.4.1"
description = "Simple configuration loader for python."
authors = ["Gabriel Pajot <gab@les-cactus.co>"]
license = "MIT"
Expand Down
6 changes: 6 additions & 0 deletions tests/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ class Cfg(ReadOnlyConfig):
SCHEMA = schema

assert Cfg.load() == expected

def test_should_not_fail_if_file_does_not_exist(self):
class Cfg(ReadOnlyConfig, dict):
PATH = "test_config.json"

assert Cfg.load() == {}
2 changes: 2 additions & 0 deletions zenconfig/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def load(cls: Type[C]) -> C:
"""Load the configuration class from file(s)."""
dict_config: Dict[str, Any] = {}
for path in cls._paths():
if not path.exists():
continue
fmt = cls._format(path)
logger.debug(
"using %s to load %s from %s",
Expand Down
1 change: 1 addition & 0 deletions zenconfig/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def save(self) -> None:
path = paths[0]
if not path.exists():
logger.debug("creating file at path %s", path)
path.parent.mkdir(parents=True, exist_ok=True)
path.touch(mode=self.FILE_MODE)
fmt = self._format()
logger.debug(
Expand Down

0 comments on commit 61aa522

Please sign in to comment.