diff --git a/CHANGELOG.md b/CHANGELOG.md index acb3b0fb..595f43a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ### Minor enhancements * Updated handling of references on read to simplify future integration of file-based Zarr stores (e.g., ZipStore or database stores) @oruebel [#62](https://github.com/hdmf-dev/hdmf-zarr/pull/62) +* Added can_read classmethod to ZarrIO. @bendichter [#97](https://github.com/hdmf-dev/hdmf-zarr/pull/97) ### Test suite enhancements * Modularized unit tests to simplify running tests for multiple Zarr storage backends diff --git a/src/hdmf_zarr/backend.py b/src/hdmf_zarr/backend.py index 7c364864..153b703b 100644 --- a/src/hdmf_zarr/backend.py +++ b/src/hdmf_zarr/backend.py @@ -75,6 +75,14 @@ class ZarrIO(HDMFIO): + @staticmethod + def can_read(path): + try: + zarr.open(path, mode="r") + return True + except Exception: + return False + @docval({'name': 'path', 'type': (str, *SUPPORTED_ZARR_STORES), 'doc': 'the path to the Zarr file or a supported Zarr store'}, diff --git a/tests/unit/base_tests_zarrio.py b/tests/unit/base_tests_zarrio.py index 3e157413..6b1c7005 100644 --- a/tests/unit/base_tests_zarrio.py +++ b/tests/unit/base_tests_zarrio.py @@ -170,6 +170,9 @@ def createReferenceCompoundBuilder(self): 'ref_dataset': dataset_ref}) return builder + def test_cannot_read(self): + assert not ZarrIO.can_read("incorrect_path") + def read_test_dataset(self): reader = ZarrIO(self.store, manager=self.manager, mode='r') self.root = reader.read_builder() @@ -209,6 +212,7 @@ def test_write_int(self, test_data=None): writer = ZarrIO(self.store, manager=self.manager, mode='a') writer.write_builder(self.builder) writer.close() + assert ZarrIO.can_read(self.store) def test_write_compound(self, test_data=None): """