From a53f817067d2b25f92f33725183a66a9d1598881 Mon Sep 17 00:00:00 2001 From: Janne Lappalainen Date: Tue, 27 Aug 2024 12:35:33 +0200 Subject: [PATCH] fix manual config --- datamate/directory.py | 12 +++++++++--- tests/test_directory.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/datamate/directory.py b/datamate/directory.py index 2f4986f..d8a41f9 100644 --- a/datamate/directory.py +++ b/datamate/directory.py @@ -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): @@ -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: @@ -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 @@ -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") @@ -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) diff --git a/tests/test_directory.py b/tests/test_directory.py index b2367ec..6c95737 100755 --- a/tests/test_directory.py +++ b/tests/test_directory.py @@ -26,6 +26,7 @@ _auto_doc, H5Reader, DirectoryDiff, + read_meta, ) from datamate import directory from datamate.namespaces import namespacify @@ -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}