Skip to content

Commit 053cab2

Browse files
Merge pull request #13 from Querent-ai/use-storage_result-type
add storage result type for cleanly handling the result of a storage …
2 parents f19aac8 + 0314f1b commit 053cab2

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

querent/storage/local/local_file_storage.py

+27-16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from querent.storage.storage_errors import StorageError, StorageErrorKind
1212
from querent.storage.storage_base import Storage
1313
from querent.storage.storage_factory import StorageFactory
14+
from querent.storage.storage_result import StorageResult
1415

1516
class AsyncDebouncer:
1617
def __init__(self):
@@ -94,8 +95,8 @@ async def get_all(self, path):
9495
async def file_num_bytes(self, path):
9596
return await self.underlying.file_num_bytes(path)
9697

97-
def uri(self):
98-
return self.underlying.uri()
98+
def get_uri(self):
99+
return self.underlying.get_uri()
99100

100101
class LocalFileStorage(Storage):
101102
def __init__(self, uri: Uri, root=None):
@@ -128,7 +129,7 @@ async def check_connectivity(self):
128129
f"Failed to create directories at {self.root}: {e}",
129130
)
130131

131-
async def put(self, path: Path, payload: PutPayload):
132+
async def put(self, path: Path, payload: PutPayload)-> StorageResult:
132133
full_path = await self.full_path(path)
133134
parent_dir = full_path.parent
134135
try:
@@ -139,32 +140,35 @@ async def put(self, path: Path, payload: PutPayload):
139140
for i in range(0, payload_len, 1024):
140141
chunk = await payload.range_byte_stream(i, i + 1024)
141142
file.write(chunk)
143+
return StorageResult.success(None)
142144
except Exception as e:
143145
raise StorageError(
144146
StorageErrorKind.Io,
145-
f"Failed to write file to {full_path}: {e}",
147+
f"Failed to write file {full_path}: {e}",
146148
)
147149

148-
async def copy_to(self, path, output):
150+
async def copy_to(self, path, output) -> StorageResult:
149151
full_path = await self.full_path(path)
150152
with open(full_path, "rb") as file:
151153
await asyncio.to_thread(shutil.copyfileobj, file, output)
154+
return StorageResult.success(None)
152155

153-
async def get_slice(self, path, start, end):
156+
async def get_slice(self, path, start, end)-> StorageResult:
154157
full_path = await self.full_path(path)
155158
with open(full_path, "rb") as file:
156159
file.seek(start)
157-
return file.read(end - start)
160+
return StorageResult.success(file.read(end - start))
158161

159-
async def get_all(self, path):
162+
async def get_all(self, path)-> StorageResult:
160163
full_path = await self.full_path(path)
161164
with open(full_path, "rb") as file:
162-
return file.read()
165+
return StorageResult.success(file.read())
163166

164-
async def delete(self, path):
167+
async def delete(self, path)-> StorageResult:
165168
full_path = await self.full_path(path)
166169
try:
167170
full_path.unlink()
171+
return StorageResult.success(None)
168172
except FileNotFoundError:
169173
pass
170174
except Exception as e:
@@ -177,16 +181,23 @@ async def bulk_delete(self, paths):
177181
for path in paths:
178182
await self.delete(path)
179183

180-
async def exists(self, path):
184+
async def exists(self, path)-> StorageResult:
181185
full_path = await self.full_path(path)
182-
return full_path.exists()
186+
return StorageResult.success(full_path.exists())
183187

184-
async def file_num_bytes(self, path):
188+
async def file_num_bytes(self, path)-> StorageResult:
185189
full_path = await self.full_path(path)
186-
return full_path.stat().st_size
190+
try:
191+
return StorageResult.success(full_path.stat().st_size)
192+
except FileNotFoundError:
193+
raise StorageError(
194+
StorageErrorKind.NotFound,
195+
f"File {full_path} not found",
196+
)
187197

188-
def uri(self):
189-
return str(self.uri)
198+
@property
199+
def get_uri(self)-> Uri:
200+
return self.uri
190201

191202
class LocalStorageFactory(StorageFactory):
192203
def backend(self) -> StorageBackend:

querent/storage/storage_base.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,52 @@
22
from pathlib import Path
33
from typing import IO
44

5+
from querent.common.uri import Uri
56
from querent.storage.payload import PutPayload
7+
from querent.storage.storage_result import StorageResult
68

79
class Storage(ABC):
810
@abstractmethod
911
async def check_connectivity(self) -> None:
1012
pass
1113

1214
@abstractmethod
13-
async def put(self, path: Path, payload: PutPayload) -> None:
15+
async def put(self, path: Path, payload: PutPayload) -> StorageResult:
1416
pass
1517

1618
@abstractmethod
17-
async def copy_to(self, path: Path, output: IO[bytes]) -> None:
19+
async def copy_to(self, path: Path, output: IO[bytes]) -> StorageResult:
1820
pass
1921

20-
async def copy_to_file(self, path: Path, output_path: Path) -> None:
22+
async def copy_to_file(self, path: Path, output_path: Path) -> StorageResult:
2123
async with open(output_path, "wb") as output_file:
2224
await self.copy_to(path, output_file)
2325

2426
@abstractmethod
25-
async def get_slice(self, path: Path, start: int, end: int) -> bytes:
27+
async def get_slice(self, path: Path, start: int, end: int) -> StorageResult:
2628
pass
2729

2830
@abstractmethod
29-
async def get_all(self, path: Path) -> bytes:
31+
async def get_all(self, path: Path) -> StorageResult:
3032
pass
3133

3234
@abstractmethod
33-
async def delete(self, path: Path) -> None:
35+
async def delete(self, path: Path) -> StorageResult:
3436
pass
3537

3638
@abstractmethod
3739
async def bulk_delete(self, paths: list[Path]) -> None:
3840
pass
3941

4042
@abstractmethod
41-
async def exists(self, path: Path) -> bool:
43+
async def exists(self, path: Path) -> StorageResult:
4244
pass
4345

4446
@abstractmethod
45-
async def file_num_bytes(self, path: Path) -> int:
47+
async def file_num_bytes(self, path: Path) -> StorageResult:
4648
pass
4749

4850
@property
4951
@abstractmethod
50-
def uri(self) -> str:
52+
def get_uri(self) -> Uri:
5153
pass

tests/test_local_storage.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ def test_storage_resolver(temp_dir):
3838

3939
storage = asyncio.run(resolver.resolve(uri))
4040

41-
payload = querent_payload.BytesPayload(b"ok")
41+
payload = querent_payload.BytesPayload(b"ok testing")
4242
asyncio.run(storage.put(Path(temp_dir + "/test.txt"), payload))
4343

4444
file_path = Path(temp_dir, "test.txt")
4545
assert file_path.exists()
4646

4747
with open(file_path, "rb") as file:
4848
content = file.read()
49-
assert content == b"ok"
49+
assert content == b"ok testing"

0 commit comments

Comments
 (0)