-
-
Notifications
You must be signed in to change notification settings - Fork 282
/
zip.py
267 lines (225 loc) · 8.23 KB
/
zip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
from __future__ import annotations
import os
import threading
import time
import zipfile
from pathlib import Path
from typing import TYPE_CHECKING, Any, Literal, Self
from zarr.abc.store import ByteRangeRequest, Store
from zarr.core.buffer import Buffer, BufferPrototype
if TYPE_CHECKING:
from collections.abc import AsyncGenerator, Iterable
ZipStoreAccessModeLiteral = Literal["r", "w", "a"]
class ZipStore(Store):
"""
Storage class using a ZIP file.
Parameters
----------
path : str
Location of file.
mode : str, optional
One of 'r' to read an existing file, 'w' to truncate and write a new
file, 'a' to append to an existing file, or 'x' to exclusively create
and write a new file.
compression : int, optional
Compression method to use when writing to the archive.
allowZip64 : bool, optional
If True (the default) will create ZIP files that use the ZIP64
extensions when the zipfile is larger than 2 GiB. If False
will raise an exception when the ZIP file would require ZIP64
extensions.
Attributes
----------
allowed_exceptions
supports_writes
supports_deletes
supports_partial_writes
supports_listing
path
compression
allowZip64
"""
supports_writes: bool = True
supports_deletes: bool = False
supports_partial_writes: bool = False
supports_listing: bool = True
path: Path
compression: int
allowZip64: bool
_zf: zipfile.ZipFile
_lock: threading.RLock
def __init__(
self,
path: Path | str,
*,
mode: ZipStoreAccessModeLiteral = "r",
compression: int = zipfile.ZIP_STORED,
allowZip64: bool = True,
) -> None:
super().__init__(mode=mode)
if isinstance(path, str):
path = Path(path)
assert isinstance(path, Path)
self.path = path # root?
self._zmode = mode
self.compression = compression
self.allowZip64 = allowZip64
def _sync_open(self) -> None:
if self._is_open:
raise ValueError("store is already open")
self._lock = threading.RLock()
self._zf = zipfile.ZipFile(
self.path,
mode=self._zmode,
compression=self.compression,
allowZip64=self.allowZip64,
)
self._is_open = True
async def _open(self) -> None:
self._sync_open()
def __getstate__(self) -> tuple[Path, ZipStoreAccessModeLiteral, int, bool]:
return self.path, self._zmode, self.compression, self.allowZip64
def __setstate__(self, state: Any) -> None:
self.path, self._zmode, self.compression, self.allowZip64 = state
self._is_open = False
self._sync_open()
def close(self) -> None:
# docstring inherited
super().close()
with self._lock:
self._zf.close()
async def clear(self) -> None:
# docstring inherited
with self._lock:
self._check_writable()
self._zf.close()
os.remove(self.path)
self._zf = zipfile.ZipFile(
self.path, mode="w", compression=self.compression, allowZip64=self.allowZip64
)
async def empty(self) -> bool:
# docstring inherited
with self._lock:
return not self._zf.namelist()
def with_mode(self, mode: ZipStoreAccessModeLiteral) -> Self: # type: ignore[override]
# docstring inherited
raise NotImplementedError("ZipStore cannot be reopened with a new mode.")
def __str__(self) -> str:
return f"zip://{self.path}"
def __repr__(self) -> str:
return f"ZipStore({str(self)!r})"
def __eq__(self, other: object) -> bool:
return isinstance(other, type(self)) and self.path == other.path
def _get(
self,
key: str,
prototype: BufferPrototype,
byte_range: ByteRangeRequest | None = None,
) -> Buffer | None:
# docstring inherited
try:
with self._zf.open(key) as f: # will raise KeyError
if byte_range is None:
return prototype.buffer.from_bytes(f.read())
start, length = byte_range
if start:
if start < 0:
start = f.seek(start, os.SEEK_END) + start
else:
start = f.seek(start, os.SEEK_SET)
if length:
return prototype.buffer.from_bytes(f.read(length))
else:
return prototype.buffer.from_bytes(f.read())
except KeyError:
return None
async def get(
self,
key: str,
prototype: BufferPrototype,
byte_range: ByteRangeRequest | None = None,
) -> Buffer | None:
# docstring inherited
assert isinstance(key, str)
with self._lock:
return self._get(key, prototype=prototype, byte_range=byte_range)
async def get_partial_values(
self,
prototype: BufferPrototype,
key_ranges: Iterable[tuple[str, ByteRangeRequest]],
) -> list[Buffer | None]:
# docstring inherited
out = []
with self._lock:
for key, byte_range in key_ranges:
out.append(self._get(key, prototype=prototype, byte_range=byte_range))
return out
def _set(self, key: str, value: Buffer) -> None:
# generally, this should be called inside a lock
keyinfo = zipfile.ZipInfo(filename=key, date_time=time.localtime(time.time())[:6])
keyinfo.compress_type = self.compression
if keyinfo.filename[-1] == os.sep:
keyinfo.external_attr = 0o40775 << 16 # drwxrwxr-x
keyinfo.external_attr |= 0x10 # MS-DOS directory flag
else:
keyinfo.external_attr = 0o644 << 16 # ?rw-r--r--
self._zf.writestr(keyinfo, value.to_bytes())
async def set(self, key: str, value: Buffer) -> None:
# docstring inherited
self._check_writable()
assert isinstance(key, str)
if not isinstance(value, Buffer):
raise TypeError("ZipStore.set(): `value` must a Buffer instance")
with self._lock:
self._set(key, value)
async def set_partial_values(self, key_start_values: Iterable[tuple[str, int, bytes]]) -> None:
raise NotImplementedError
async def set_if_not_exists(self, key: str, value: Buffer) -> None:
self._check_writable()
with self._lock:
members = self._zf.namelist()
if key not in members:
self._set(key, value)
async def delete(self, key: str) -> None:
# docstring inherited
# we choose to only raise NotImplementedError here if the key exists
# this allows the array/group APIs to avoid the overhead of existence checks
if await self.exists(key):
raise NotImplementedError
async def exists(self, key: str) -> bool:
# docstring inherited
with self._lock:
try:
self._zf.getinfo(key)
except KeyError:
return False
else:
return True
async def list(self) -> AsyncGenerator[str, None]:
# docstring inherited
with self._lock:
for key in self._zf.namelist():
yield key
async def list_prefix(self, prefix: str) -> AsyncGenerator[str, None]:
# docstring inherited
async for key in self.list():
if key.startswith(prefix):
yield key.removeprefix(prefix)
async def list_dir(self, prefix: str) -> AsyncGenerator[str, None]:
# docstring inherited
prefix = prefix.rstrip("/")
keys = self._zf.namelist()
seen = set()
if prefix == "":
keys_unique = {k.split("/")[0] for k in keys}
for key in keys_unique:
if key not in seen:
seen.add(key)
yield key
else:
for key in keys:
if key.startswith(prefix + "/") and key != prefix:
k = key.removeprefix(prefix + "/").split("/")[0]
if k not in seen:
seen.add(k)
yield k