11
11
from querent .storage .storage_errors import StorageError , StorageErrorKind
12
12
from querent .storage .storage_base import Storage
13
13
from querent .storage .storage_factory import StorageFactory
14
+ from querent .storage .storage_result import StorageResult
14
15
15
16
class AsyncDebouncer :
16
17
def __init__ (self ):
@@ -94,8 +95,8 @@ async def get_all(self, path):
94
95
async def file_num_bytes (self , path ):
95
96
return await self .underlying .file_num_bytes (path )
96
97
97
- def uri (self ):
98
- return self .underlying .uri ()
98
+ def get_uri (self ):
99
+ return self .underlying .get_uri ()
99
100
100
101
class LocalFileStorage (Storage ):
101
102
def __init__ (self , uri : Uri , root = None ):
@@ -128,7 +129,7 @@ async def check_connectivity(self):
128
129
f"Failed to create directories at { self .root } : { e } " ,
129
130
)
130
131
131
- async def put (self , path : Path , payload : PutPayload ):
132
+ async def put (self , path : Path , payload : PutPayload )-> StorageResult :
132
133
full_path = await self .full_path (path )
133
134
parent_dir = full_path .parent
134
135
try :
@@ -139,32 +140,35 @@ async def put(self, path: Path, payload: PutPayload):
139
140
for i in range (0 , payload_len , 1024 ):
140
141
chunk = await payload .range_byte_stream (i , i + 1024 )
141
142
file .write (chunk )
143
+ return StorageResult .success (None )
142
144
except Exception as e :
143
145
raise StorageError (
144
146
StorageErrorKind .Io ,
145
- f"Failed to write file to { full_path } : { e } " ,
147
+ f"Failed to write file { full_path } : { e } " ,
146
148
)
147
149
148
- async def copy_to (self , path , output ):
150
+ async def copy_to (self , path , output ) -> StorageResult :
149
151
full_path = await self .full_path (path )
150
152
with open (full_path , "rb" ) as file :
151
153
await asyncio .to_thread (shutil .copyfileobj , file , output )
154
+ return StorageResult .success (None )
152
155
153
- async def get_slice (self , path , start , end ):
156
+ async def get_slice (self , path , start , end )-> StorageResult :
154
157
full_path = await self .full_path (path )
155
158
with open (full_path , "rb" ) as file :
156
159
file .seek (start )
157
- return file .read (end - start )
160
+ return StorageResult . success ( file .read (end - start ) )
158
161
159
- async def get_all (self , path ):
162
+ async def get_all (self , path )-> StorageResult :
160
163
full_path = await self .full_path (path )
161
164
with open (full_path , "rb" ) as file :
162
- return file .read ()
165
+ return StorageResult . success ( file .read () )
163
166
164
- async def delete (self , path ):
167
+ async def delete (self , path )-> StorageResult :
165
168
full_path = await self .full_path (path )
166
169
try :
167
170
full_path .unlink ()
171
+ return StorageResult .success (None )
168
172
except FileNotFoundError :
169
173
pass
170
174
except Exception as e :
@@ -177,16 +181,23 @@ async def bulk_delete(self, paths):
177
181
for path in paths :
178
182
await self .delete (path )
179
183
180
- async def exists (self , path ):
184
+ async def exists (self , path )-> StorageResult :
181
185
full_path = await self .full_path (path )
182
- return full_path .exists ()
186
+ return StorageResult . success ( full_path .exists () )
183
187
184
- async def file_num_bytes (self , path ):
188
+ async def file_num_bytes (self , path )-> StorageResult :
185
189
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
+ )
187
197
188
- def uri (self ):
189
- return str (self .uri )
198
+ @property
199
+ def get_uri (self )-> Uri :
200
+ return self .uri
190
201
191
202
class LocalStorageFactory (StorageFactory ):
192
203
def backend (self ) -> StorageBackend :
0 commit comments