|
4 | 4 | import zarr |
5 | 5 |
|
6 | 6 | from cubed.types import T_DType, T_RegularChunks, T_Shape, T_Store |
| 7 | +from cubed.utils import is_cloud_storage_path |
| 8 | + |
| 9 | +try: |
| 10 | + import obstore |
| 11 | +except ImportError: |
| 12 | + obstore = None # type: ignore |
7 | 13 |
|
8 | 14 | zarr.config.set( |
9 | 15 | { |
@@ -46,21 +52,35 @@ def open_zarr_v3_array( |
46 | 52 | path: Optional[str] = None, |
47 | 53 | **kwargs, |
48 | 54 | ): |
49 | | - # use obstore if requested |
| 55 | + # use obstore if explicitly requested, or if library is installed and store is a cloud store |
50 | 56 | storage_options = kwargs.pop("storage_options", None) |
51 | | - if storage_options is not None and storage_options.get("use_obstore", False): |
52 | | - import obstore as obs |
| 57 | + obstore_requested = storage_options is not None and storage_options.get( |
| 58 | + "use_obstore", False |
| 59 | + ) |
| 60 | + obstore_installed = obstore is not None |
| 61 | + if obstore_requested and not obstore_installed: |
| 62 | + raise RuntimeError( |
| 63 | + "obstore was requested with 'use_obstore=True' but it is not installed" |
| 64 | + ) |
| 65 | + use_obstore = obstore_requested or ( |
| 66 | + obstore_installed |
| 67 | + and isinstance(store, (str, Path)) |
| 68 | + and is_cloud_storage_path(store) |
| 69 | + ) |
| 70 | + if use_obstore: |
53 | 71 | from zarr.storage import ObjectStore |
54 | 72 |
|
55 | 73 | if isinstance(store, str): |
56 | 74 | if "://" not in store: |
57 | 75 | p = Path(store) |
58 | | - store = ObjectStore(obs.store.from_url(p.as_uri(), mkdir=True)) |
| 76 | + store = ObjectStore(obstore.store.from_url(p.as_uri(), mkdir=True)) |
59 | 77 | else: |
60 | | - store = ObjectStore(obs.store.from_url(store)) |
| 78 | + store = ObjectStore(obstore.store.from_url(store)) |
61 | 79 | elif isinstance(store, Path): |
62 | 80 | p = store |
63 | | - store = ObjectStore(obs.store.from_url(p.as_uri(), mkdir=True)) |
| 81 | + store = ObjectStore(obstore.store.from_url(p.as_uri(), mkdir=True)) |
| 82 | + else: |
| 83 | + raise RuntimeError("Store must be a string or `Path` object for obstore") |
64 | 84 |
|
65 | 85 | if isinstance(chunks, int): |
66 | 86 | chunks = (chunks,) |
|
0 commit comments