|
8 | 8 | from filelock import BaseFileLock, FileLock |
9 | 9 | from zarr.abc.store import Store |
10 | 10 | from zarr.core.array import CompressorLike |
11 | | -from zarr.errors import ContainsGroupError, GroupNotFoundError |
| 11 | +from zarr.errors import ContainsGroupError |
12 | 12 | from zarr.storage import FsspecStore, LocalStore, MemoryStore |
13 | 13 |
|
14 | 14 | from ngio.utils import NgioFileExistsError, NgioFileNotFoundError, NgioValueError |
@@ -38,13 +38,12 @@ def _check_store(store) -> NgioSupportedStore: |
38 | 38 |
|
39 | 39 | def _check_group(group: zarr.Group, mode: AccessModeLiteral) -> zarr.Group: |
40 | 40 | """Check the group and return a valid group.""" |
41 | | - is_read_only = getattr(group, "_read_only", False) |
42 | | - if is_read_only and mode in ["w", "w-"]: |
| 41 | + if group.read_only and mode in ["w", "w-"]: |
43 | 42 | raise NgioValueError( |
44 | 43 | "The group is read only. Cannot open in write mode ['w', 'w-']" |
45 | 44 | ) |
46 | 45 |
|
47 | | - if mode == "r" and not is_read_only: |
| 46 | + if mode == "r" and not group.read_only: |
48 | 47 | # let's make sure we don't accidentally write to the group |
49 | 48 | group = zarr.open_group(store=group.store, path=group.path, mode="r") |
50 | 49 |
|
@@ -75,14 +74,19 @@ def open_group_wrapper( |
75 | 74 | _check_store(store) |
76 | 75 | group = zarr.open_group(store=store, mode=mode, zarr_format=zarr_format) |
77 | 76 |
|
78 | | - except ContainsGroupError as e: |
| 77 | + except FileExistsError as e: |
79 | 78 | raise NgioFileExistsError( |
80 | 79 | f"A Zarr group already exists at {store}, consider setting overwrite=True." |
81 | 80 | ) from e |
82 | 81 |
|
83 | | - except GroupNotFoundError as e: |
| 82 | + except FileNotFoundError as e: |
84 | 83 | raise NgioFileNotFoundError(f"No Zarr group found at {store}") from e |
85 | 84 |
|
| 85 | + except ContainsGroupError as e: |
| 86 | + raise NgioFileExistsError( |
| 87 | + f"A Zarr group already exists at {store}, consider setting overwrite=True." |
| 88 | + ) from e |
| 89 | + |
86 | 90 | return group |
87 | 91 |
|
88 | 92 |
|
@@ -215,6 +219,19 @@ def remove_lock(self) -> None: |
215 | 219 | @property |
216 | 220 | def group(self) -> zarr.Group: |
217 | 221 | """Return the group.""" |
| 222 | + if self._parallel_safe: |
| 223 | + # If we are parallel safe, we need to reopen the group |
| 224 | + # to make sure that the attributes are up to date |
| 225 | + if self.mode == "r": |
| 226 | + mode = "r" |
| 227 | + else: |
| 228 | + mode = "r+" |
| 229 | + return zarr.open_group( |
| 230 | + store=self._group.store, |
| 231 | + path=self._group.path, |
| 232 | + mode=mode, |
| 233 | + zarr_format=self._group.metadata.zarr_format, |
| 234 | + ) |
218 | 235 | return self._group |
219 | 236 |
|
220 | 237 | def add_to_cache(self, key: str, value: object) -> None: |
@@ -246,8 +263,7 @@ def load_attrs(self) -> dict: |
246 | 263 |
|
247 | 264 | def _write_attrs(self, attrs: dict, overwrite: bool = False) -> None: |
248 | 265 | """Write the metadata to the store.""" |
249 | | - is_read_only = getattr(self._group, "_read_only", False) |
250 | | - if is_read_only: |
| 266 | + if self.group.read_only: |
251 | 267 | raise NgioValueError("The group is read only. Cannot write metadata.") |
252 | 268 |
|
253 | 269 | # we need to invalidate the current attrs cache |
|
0 commit comments