Skip to content

Commit

Permalink
Merge pull request #349 from berombau/master
Browse files Browse the repository at this point in the history
Support init of ome_zarr_py.io.ZarrLocation with zarr.storage.FSStore
  • Loading branch information
joshmoore authored May 22, 2024
2 parents aa178a0 + 03d8f53 commit 0022f0e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[settings]
known_third_party = dask,numcodecs,numpy,pytest,scipy,setuptools,skimage,zarr
known_third_party = dask,fsspec,numcodecs,numpy,pytest,scipy,setuptools,skimage,zarr
multi_line_output = 3
include_trailing_comma = True
force_grid_wrap = 0
Expand Down
11 changes: 9 additions & 2 deletions ome_zarr/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ class ZarrLocation:
"""

def __init__(
self, path: Union[Path, str], mode: str = "r", fmt: Format = CurrentFormat()
self,
path: Union[Path, str, FSStore],
mode: str = "r",
fmt: Format = CurrentFormat(),
) -> None:
LOGGER.debug("ZarrLocation.__init__ path: %s, fmt: %s", path, fmt.version)
self.__fmt = fmt
Expand All @@ -37,13 +40,17 @@ def __init__(
self.__path = str(path.resolve())
elif isinstance(path, str):
self.__path = path
elif isinstance(path, FSStore):
self.__path = path.path
else:
raise TypeError(f"not expecting: {type(path)}")

loader = fmt
if loader is None:
loader = CurrentFormat()
self.__store = loader.init_store(self.__path, mode)
self.__store: FSStore = (
path if isinstance(path, FSStore) else loader.init_store(self.__path, mode)
)

self.__init_metadata()
detected = detect_format(self.__metadata, loader)
Expand Down
38 changes: 38 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from pathlib import Path

import fsspec
import pytest
import zarr

from ome_zarr.data import create_zarr
from ome_zarr.io import ZarrLocation, parse_url


class TestIO:
@pytest.fixture(autouse=True)
def initdir(self, tmpdir):
self.path = tmpdir.mkdir("data")
create_zarr(str(self.path))
self.store = parse_url(str(self.path), mode="w").store
self.root = zarr.group(store=self.store)

def test_parse_url(self):
assert parse_url(str(self.path))

def test_parse_nonexistent_url(self):
assert parse_url(self.path + "/does-not-exist") is None

def test_loc_str(self):
assert ZarrLocation(str(self.path))

def test_loc_path(self):
assert ZarrLocation(Path(self.path))

def test_loc_store(self):
assert ZarrLocation(self.store)

def test_loc_fs(self):
fs = fsspec.filesystem("memory")
fsstore = zarr.storage.FSStore(url="/", fs=fs)
loc = ZarrLocation(fsstore)
assert loc

0 comments on commit 0022f0e

Please sign in to comment.