Skip to content

Commit

Permalink
fix manual config
Browse files Browse the repository at this point in the history
  • Loading branch information
lappalainenj committed Aug 27, 2024
1 parent 6b9afc2 commit a53f817
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
12 changes: 9 additions & 3 deletions datamate/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,8 @@ def config(self):

@config.setter
def config(self, value):
self._override_config(value)
# unreachable code executred in setattr
self.__manual_config(value)

@property
def status(self):
Expand Down Expand Up @@ -838,6 +839,10 @@ def __setattr__(self, key: str, value: object) -> None:
if key.startswith("__") and key.endswith("__"):
object.__setattr__(self, key, value)
return
# allow manual config writing
if key == "config":
self.__manual_config(value)
return
self.__setitem__(key.replace("__", "."), value)

def __delattr__(self, key: str) -> None:
Expand Down Expand Up @@ -867,7 +872,7 @@ def __dir__(self) -> List[str]:

# -- Convenience methods

def _override_config(self, config, status=None):
def __manual_config(self, config, status=None):
"""Overriding config stored in _meta.yaml.
config (Dict): update for meta.config
Expand All @@ -877,6 +882,7 @@ def _override_config(self, config, status=None):
meta_path = self.path / "_meta.yaml"

current_config = self.config
config = namespacify(config)
if current_config is not None:
with warnings.catch_warnings():
warnings.simplefilter("always")
Expand All @@ -888,7 +894,7 @@ def _override_config(self, config, status=None):
ConfigWarning,
stacklevel=2,
)
write_meta(path=meta_path, config=config, status="overridden")
write_meta(path=meta_path, config=config, status="manually written")
else:
write_meta(path=meta_path, config=config, status=status or self.status)

Expand Down
18 changes: 18 additions & 0 deletions tests/test_directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
_auto_doc,
H5Reader,
DirectoryDiff,
read_meta,
)
from datamate import directory
from datamate.namespaces import namespacify
Expand Down Expand Up @@ -1310,3 +1311,20 @@ def test_read_meta(tmp_path):

with pytest.raises(AssertionError, match=f"meta has non-string status"):
assert directory.status


def test_write_config(tmp_path):

set_root_dir(tmp_path)

directory = Directory("test")
directory.path.mkdir()
assert directory.meta == {"config": None, "status": "done"}
assert not (directory.path / "_meta.yaml").exists()

with pytest.warns(ConfigWarning):
directory.config = {"x": 2}

assert read_meta(directory.path).config.to_dict() == {"x": 2}
assert read_meta(directory.path).status == "manually written"
assert directory.config == {"x": 2}

0 comments on commit a53f817

Please sign in to comment.