Skip to content

Commit

Permalink
Handle fsspec.FSMap as zarr's store (use FSStore)
Browse files Browse the repository at this point in the history
  • Loading branch information
ravwojdyla committed Dec 20, 2022
1 parent 1af77b6 commit 3c173f9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions zarr/_storage/v3.py
Expand Up @@ -567,6 +567,16 @@ def _normalize_store_arg_v3(store: Any, storage_options=None, mode="r") -> BaseS
return store
if isinstance(store, os.PathLike):
store = os.fspath(store)
if FSStore._fsspec_installed():
import fsspec
if isinstance(store, fsspec.FSMap):
return FSStoreV3(store.root,
fs=store.fs,
mode=mode,
check=store.check,
create=store.create,
missing_exceptions=store.missing_exceptions,
**(storage_options or {}))
if isinstance(store, str):
if "://" in store or "::" in store:
store = FSStoreV3(store, mode=mode, **(storage_options or {}))
Expand Down
19 changes: 19 additions & 0 deletions zarr/storage.py
Expand Up @@ -139,6 +139,16 @@ def _normalize_store_arg_v2(store: Any, storage_options=None, mode="r") -> BaseS
return store
if isinstance(store, os.PathLike):
store = os.fspath(store)
if FSStore._fsspec_installed():
import fsspec
if isinstance(store, fsspec.FSMap):
return FSStore(store.root,
fs=store.fs,
mode=mode,
check=store.check,
create=store.create,
missing_exceptions=store.missing_exceptions,
**(storage_options or {}))
if isinstance(store, str):
if "://" in store or "::" in store:
return FSStore(store, mode=mode, **(storage_options or {}))
Expand Down Expand Up @@ -1308,6 +1318,8 @@ def __init__(self, url, normalize_keys=False, key_separator=None,
create=False,
missing_exceptions=None,
**storage_options):
if not self._fsspec_installed(): # pragma: no cover
raise ImportError("`fsspec` is required to use zarr's FSStore")
import fsspec

mapper_options = {"check": check, "create": create}
Expand Down Expand Up @@ -1479,6 +1491,13 @@ def clear(self):
raise ReadOnlyError()
self.map.clear()

@classmethod
def _fsspec_installed(cls):
"""Returns true if fsspec is installed"""
import importlib.util

return importlib.util.find_spec("fsspec") is not None


class TempStore(DirectoryStore):
"""Directory store using a temporary directory for storage.
Expand Down
5 changes: 5 additions & 0 deletions zarr/tests/test_storage.py
Expand Up @@ -2556,10 +2556,15 @@ def test_normalize_store_arg(tmpdir):
assert isinstance(store, Class)

if have_fsspec:
import fsspec

path = tempfile.mkdtemp()
store = normalize_store_arg("file://" + path, zarr_version=2, mode='w')
assert isinstance(store, FSStore)

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


def test_meta_prefix_6853():

Expand Down
5 changes: 5 additions & 0 deletions zarr/tests/test_storage_v3.py
Expand Up @@ -467,11 +467,16 @@ def test_normalize_store_arg_v3(tmpdir):
normalize_store_arg(str(fn), zarr_version=3, mode='w', storage_options={"some": "kwargs"})

if have_fsspec:
import fsspec

path = tempfile.mkdtemp()
store = normalize_store_arg("file://" + path, zarr_version=3, mode='w')
assert isinstance(store, FSStoreV3)
assert 'zarr.json' in store

store = normalize_store_arg(fsspec.get_mapper("file://" + path), zarr_version=3)
assert isinstance(store, FSStoreV3)

fn = tmpdir.join('store.n5')
with pytest.raises(NotImplementedError):
normalize_store_arg(str(fn), zarr_version=3, mode='w')
Expand Down

0 comments on commit 3c173f9

Please sign in to comment.