Skip to content

Commit e68c97f

Browse files
authored
Merge branch 'main' into storage-transformers-and-partial-get-set
2 parents 91f0c2c + 4e633ad commit e68c97f

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

docs/release.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Unreleased
1717
* Fix bug that caused double counting of groups in ``groups()`` and ``group_keys()``
1818
methods with V3 stores.
1919
By :user:`Ryan Abernathey <rabernat>` :issue:`1228`.
20+
* Handle fsspec.FSMap using FSStore store
21+
By :user:`Rafal Wojdyla <ravwojdyla>` :issue:`1304`.
2022
* Improve Zarr V3 support, adding partial store read/write and storage transformers.
2123
Add two features of the [v3 spec](https://zarr-specs.readthedocs.io/en/latest/core/v3.0.html):
2224
* storage transformers

zarr/_storage/v3.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,16 @@ def _normalize_store_arg_v3(store: Any, storage_options=None, mode="r") -> BaseS
567567
return store
568568
if isinstance(store, os.PathLike):
569569
store = os.fspath(store)
570+
if FSStore._fsspec_installed():
571+
import fsspec
572+
if isinstance(store, fsspec.FSMap):
573+
return FSStoreV3(store.root,
574+
fs=store.fs,
575+
mode=mode,
576+
check=store.check,
577+
create=store.create,
578+
missing_exceptions=store.missing_exceptions,
579+
**(storage_options or {}))
570580
if isinstance(store, str):
571581
if "://" in store or "::" in store:
572582
store = FSStoreV3(store, mode=mode, **(storage_options or {}))

zarr/storage.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ def _normalize_store_arg_v2(store: Any, storage_options=None, mode="r") -> BaseS
139139
return store
140140
if isinstance(store, os.PathLike):
141141
store = os.fspath(store)
142+
if FSStore._fsspec_installed():
143+
import fsspec
144+
if isinstance(store, fsspec.FSMap):
145+
return FSStore(store.root,
146+
fs=store.fs,
147+
mode=mode,
148+
check=store.check,
149+
create=store.create,
150+
missing_exceptions=store.missing_exceptions,
151+
**(storage_options or {}))
142152
if isinstance(store, str):
143153
if "://" in store or "::" in store:
144154
return FSStore(store, mode=mode, **(storage_options or {}))
@@ -1313,6 +1323,8 @@ def __init__(self, url, normalize_keys=False, key_separator=None,
13131323
create=False,
13141324
missing_exceptions=None,
13151325
**storage_options):
1326+
if not self._fsspec_installed(): # pragma: no cover
1327+
raise ImportError("`fsspec` is required to use zarr's FSStore")
13161328
import fsspec
13171329

13181330
mapper_options = {"check": check, "create": create}
@@ -1484,6 +1496,13 @@ def clear(self):
14841496
raise ReadOnlyError()
14851497
self.map.clear()
14861498

1499+
@classmethod
1500+
def _fsspec_installed(cls):
1501+
"""Returns true if fsspec is installed"""
1502+
import importlib.util
1503+
1504+
return importlib.util.find_spec("fsspec") is not None
1505+
14871506

14881507
class TempStore(DirectoryStore):
14891508
"""Directory store using a temporary directory for storage.

zarr/tests/test_storage.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,10 +2556,15 @@ def test_normalize_store_arg(tmpdir):
25562556
assert isinstance(store, Class)
25572557

25582558
if have_fsspec:
2559+
import fsspec
2560+
25592561
path = tempfile.mkdtemp()
25602562
store = normalize_store_arg("file://" + path, zarr_version=2, mode='w')
25612563
assert isinstance(store, FSStore)
25622564

2565+
store = normalize_store_arg(fsspec.get_mapper("file://" + path))
2566+
assert isinstance(store, FSStore)
2567+
25632568

25642569
def test_meta_prefix_6853():
25652570

zarr/tests/test_storage_v3.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,11 +572,16 @@ def test_normalize_store_arg_v3(tmpdir):
572572
normalize_store_arg(str(fn), zarr_version=3, mode='w', storage_options={"some": "kwargs"})
573573

574574
if have_fsspec:
575+
import fsspec
576+
575577
path = tempfile.mkdtemp()
576578
store = normalize_store_arg("file://" + path, zarr_version=3, mode='w')
577579
assert isinstance(store, FSStoreV3)
578580
assert 'zarr.json' in store
579581

582+
store = normalize_store_arg(fsspec.get_mapper("file://" + path), zarr_version=3)
583+
assert isinstance(store, FSStoreV3)
584+
580585
fn = tmpdir.join('store.n5')
581586
with pytest.raises(NotImplementedError):
582587
normalize_store_arg(str(fn), zarr_version=3, mode='w')

0 commit comments

Comments
 (0)