|
5 | 5 | import os
|
6 | 6 | import shutil
|
7 | 7 | from pathlib import Path
|
8 |
| -from typing import TYPE_CHECKING |
| 8 | +from typing import TYPE_CHECKING, Self |
9 | 9 |
|
10 | 10 | from zarr.abc.store import (
|
11 | 11 | ByteRequest,
|
|
16 | 16 | )
|
17 | 17 | from zarr.core.buffer import Buffer
|
18 | 18 | from zarr.core.buffer.core import default_buffer_prototype
|
19 |
| -from zarr.core.common import concurrent_map |
| 19 | +from zarr.core.common import AccessModeLiteral, concurrent_map |
20 | 20 |
|
21 | 21 | if TYPE_CHECKING:
|
22 | 22 | from collections.abc import AsyncIterator, Iterable
|
@@ -102,16 +102,50 @@ def __init__(self, root: Path | str, *, read_only: bool = False) -> None:
|
102 | 102 | )
|
103 | 103 | self.root = root
|
104 | 104 |
|
105 |
| - def with_read_only(self, read_only: bool = False) -> LocalStore: |
| 105 | + def with_read_only(self, read_only: bool = False) -> Self: |
106 | 106 | # docstring inherited
|
107 | 107 | return type(self)(
|
108 | 108 | root=self.root,
|
109 | 109 | read_only=read_only,
|
110 | 110 | )
|
111 | 111 |
|
112 |
| - async def _open(self) -> None: |
| 112 | + @classmethod |
| 113 | + async def open( |
| 114 | + cls, root: Path | str, *, read_only: bool = False, mode: AccessModeLiteral | None = None |
| 115 | + ) -> Self: |
| 116 | + """ |
| 117 | + Create and open the store. |
| 118 | +
|
| 119 | + Parameters |
| 120 | + ---------- |
| 121 | + root : str or Path |
| 122 | + Directory to use as root of store. |
| 123 | + read_only : bool |
| 124 | + Whether the store is read-only |
| 125 | + mode : |
| 126 | + Mode in which to create the store. This only affects opening the store, |
| 127 | + and the final read-only state of the store is controlled through the |
| 128 | + read_only parameter. |
| 129 | +
|
| 130 | + Returns |
| 131 | + ------- |
| 132 | + Store |
| 133 | + The opened store instance. |
| 134 | + """ |
| 135 | + if mode is not None: |
| 136 | + read_only_creation = mode in ["r", "r+"] |
| 137 | + else: |
| 138 | + read_only_creation = read_only |
| 139 | + store = cls(root, read_only=read_only_creation) |
| 140 | + await store._open() |
| 141 | + return store.with_read_only(read_only) |
| 142 | + |
| 143 | + async def _open(self, *, mode: AccessModeLiteral | None = None) -> None: |
113 | 144 | if not self.read_only:
|
114 | 145 | self.root.mkdir(parents=True, exist_ok=True)
|
| 146 | + |
| 147 | + if not self.root.exists(): |
| 148 | + raise FileNotFoundError(f"{self.root} does not exist") |
115 | 149 | return await super()._open()
|
116 | 150 |
|
117 | 151 | async def clear(self) -> None:
|
|
0 commit comments