Skip to content

Commit 440d097

Browse files
grlee77Carreaujoshmoore
authored
Move metadata handling to a class (updated) (#839)
* fix conflicts * cleanup naming * zip move * fix erasability test * test for warning * please flake * remove uncovered lines * remove uncovered lines in tests * pragma no cover for exceptional case * minor docstring fixes add assert statements to test_capabilities * pep8 fix * avoid NumPy 1.21.0 due to numpy/numpy#19325 * move Store class and some helper functions to zarr._storage.store update version in Store docstring * BUG: ABSStore should inherit from Store * pep8 fix * TST: make CustomMapping a subclass of Store TST: initialize stores with KVStore(dict()) instead of bare dict() * update version mentioned in Store docstring * update version mentioned in warning message * use Store._ensure_store in Attributes class ensures Attributes.store is a Store * TST: add Attributes test case ensuring store gets coerced to a Store * use Store._ensure_store in normalize_store_arg ensures open_array, etc can work when the user supplies a dict * TST: make sure high level creation functions also work when passed a dict for store * TST: add test case with group initialized from dict * TST: add test case with Array initialized from dict * change CustomMapping back to type object, not Store want to test the non-Store code path in _ensure_store * pep8 fixes * update/fix new hierarchy test case to complete code coverage * create a BaseStore parent for Store BaseStore does not have the listdir or rmdir methods cleaned up some type declerations, making sure mypy passes * Convert functions in zarr.meta to class methods This is done to ease transition to Zarr v3 support. When adding v3 support, we can override encode and decode methods to account for changes in the metadata format. The classmethods are also exported under the old function names for backwards compatibility. Co-authored-by: Matthias Bussonier <[email protected]> * Add a _metadata_class attribute to the Store class Because existing functions allow coerce of dict to store, there are a lot of hasattr calls here. We can remove these checks if we start enforcing that the input MUST be a Store. Use of this _metadata_class will ease the transition to v3 * Use _metadata_class attribute in test_storage.py This will make it easier to reuse existing testing code when adding v3 support * remove unused imports use _metadata_class methods * remove unnecessary hasattr checks In these cases self._store was created using _ensure_store, so it will always have the attribute * test migrate1to2 with store=dict() and store=KVStore(dict()) * pep8: remove unused imports * misc * s * remove unused FLOAT_FILLS * mypy fixes * sync metadata methods with current master branch * flake8 * restore is_erasable check to rmdir function Otherwise the save_array doc example fails to write to a ZipStore Co-authored-by: Matthias Bussonnier <[email protected]> Co-authored-by: Josh Moore <[email protected]> Co-authored-by: jmoore <[email protected]>
1 parent 58468ca commit 440d097

File tree

7 files changed

+265
-246
lines changed

7 files changed

+265
-246
lines changed

zarr/_storage/store.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections.abc import MutableMapping
22
from typing import Any, List, Optional, Union
33

4+
from zarr.meta import Metadata2
45
from zarr.util import normalize_storage_path
56

67
# v2 store keys
@@ -32,6 +33,8 @@ class BaseStore(MutableMapping):
3233
_writeable = True
3334
_erasable = True
3435
_listable = True
36+
_store_version = 2
37+
_metadata_class = Metadata2
3538

3639
def is_readable(self):
3740
return self._readable
@@ -114,6 +117,7 @@ class Store(BaseStore):
114117
.. added: 2.11.0
115118
116119
"""
120+
117121
def listdir(self, path: str = "") -> List[str]:
118122
path = normalize_storage_path(path)
119123
return _listdir_from_keys(self, path)

zarr/attrs.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from collections.abc import MutableMapping
22

3-
from zarr.meta import parse_metadata
43
from zarr._storage.store import Store
54
from zarr.util import json_dumps
65

@@ -40,7 +39,7 @@ def _get_nosync(self):
4039
except KeyError:
4140
d = dict()
4241
else:
43-
d = parse_metadata(data)
42+
d = self.store._metadata_class.parse_metadata(data)
4443
return d
4544

4645
def asdict(self):

zarr/core.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
is_scalar,
3232
pop_fields,
3333
)
34-
from zarr.meta import decode_array_metadata, encode_array_metadata
3534
from zarr.storage import array_meta_key, attrs_key, getsize, listdir, BaseStore
3635
from zarr.util import (
3736
all_equal,
@@ -210,7 +209,7 @@ def _load_metadata_nosync(self):
210209
else:
211210

212211
# decode and store metadata as instance members
213-
meta = decode_array_metadata(meta_bytes)
212+
meta = self._store._metadata_class.decode_array_metadata(meta_bytes)
214213
self._meta = meta
215214
self._shape = meta['shape']
216215
self._chunks = meta['chunks']
@@ -267,7 +266,7 @@ def _flush_metadata_nosync(self):
267266
compressor=compressor_config, fill_value=self._fill_value,
268267
order=self._order, filters=filters_config)
269268
mkey = self._key_prefix + array_meta_key
270-
self._store[mkey] = encode_array_metadata(meta)
269+
self._store[mkey] = self._store._metadata_class.encode_array_metadata(meta)
271270

272271
@property
273272
def store(self):

zarr/hierarchy.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
GroupNotFoundError,
1515
ReadOnlyError,
1616
)
17-
from zarr.meta import decode_group_metadata
1817
from zarr.storage import (
1918
BaseStore,
2019
MemoryStore,
@@ -134,8 +133,7 @@ def __init__(self, store, path=None, read_only=False, chunk_store=None,
134133
except KeyError:
135134
raise GroupNotFoundError(path)
136135
else:
137-
meta = decode_group_metadata(meta_bytes)
138-
self._meta = meta
136+
self._meta = self._store._metadata_class.decode_group_metadata(meta_bytes)
139137

140138
# setup attributes
141139
akey = self._key_prefix + attrs_key

0 commit comments

Comments
 (0)