@@ -221,8 +221,8 @@ def logout(self):
221
221
def _upload_files_parallel (
222
222
self ,
223
223
files : Union [str , Path , list ],
224
+ id : str ,
224
225
n : int = 2 ,
225
- attach_to : str = None ,
226
226
progress = True ,
227
227
):
228
228
"""Upload files in parallel.
@@ -231,10 +231,10 @@ def _upload_files_parallel(
231
231
----------
232
232
files : Union[str, Path, list]
233
233
A path to a file, or a list of paths.
234
+ id : str
235
+ Module output id to attach to, by default None.
234
236
n : int, optional
235
237
Number of threads to use, by default 2.
236
- attach_to : str, optional
237
- Module output id to attach to, by default None.
238
238
239
239
Returns
240
240
-------
@@ -248,16 +248,17 @@ def _upload_files_parallel(
248
248
# Do the parallel upload
249
249
responses = None
250
250
responses = meop .parallelise (
251
- self ._upload_file , n , files = files , attach_to = attach_to , progress = progress
251
+ self ._upload_file , n , filepath = files , id = id , progress = progress
252
252
)
253
253
254
+ # These should already be a list as per the parallelise function.
254
255
return responses
255
256
256
257
def upload_files (
257
258
self ,
258
259
files : Union [str , Path , list ],
260
+ id : str ,
259
261
n : int = 1 ,
260
- attach_to : str = None ,
261
262
progress = True ,
262
263
) -> list :
263
264
"""Upload files.
@@ -266,10 +267,11 @@ def upload_files(
266
267
----------
267
268
files : Union[str, Path, list]
268
269
A filepath, or a list of filepaths.
270
+ id : str
271
+ Model output ID to immediately attach to.
269
272
n : int, optional
270
273
Number of threads to parallelise over, by default 1
271
- attach_to : str, optional
272
- Model output ID to immediately attach to, by default None
274
+
273
275
274
276
Returns
275
277
-------
@@ -288,28 +290,27 @@ def upload_files(
288
290
responses = list ()
289
291
if n == 1 :
290
292
for fp in tqdm (files , total = len (files )):
291
- response = self ._upload_file (fp , attach_to = attach_to )
293
+ response = self ._upload_file (fp , id = id )
292
294
responses .append (response )
293
295
else :
294
- responses = self ._upload_files_parallel (
295
- files , n = n , attach_to = attach_to , progress = progress
296
+ responses + = self ._upload_files_parallel (
297
+ files , n = n , id = id , progress = progress
296
298
)
297
299
298
- return mu .ensure_list (responses )
300
+ # return mu.ensure_list(responses)
301
+ return responses
299
302
300
303
def _upload_file (
301
- self ,
302
- files : Union [str , Path , list ],
303
- attach_to : str = None ,
304
+ self , filepath : Union [str , Path ], id : str
304
305
) -> Union [dict , requests .Response ]:
305
- """Upload a file.
306
+ """Upload a single file.
306
307
307
308
Parameters
308
309
----------
309
- files : path-like, list
310
- Path to the file, or a list containing paths.
311
- attach_to : str, optional
312
- Optional model_output_id to attach the files to, by default None
310
+ filepath : path-like
311
+ Path to the file
312
+ id : str
313
+ model_output_id to attach the files to
313
314
314
315
Returns
315
316
-------
@@ -319,56 +320,42 @@ def _upload_file(
319
320
Raises
320
321
------
321
322
TypeError
322
- When supplied file(s) are neither path-like nor readable.
323
+ When supplied file is neither path-like nor readable.
323
324
FileNotFoundError
324
- When supplied file(s) cannot be found.
325
+ When supplied file is cannot be found.
325
326
"""
326
327
327
- # Cast as list for iterative upload
328
- files = mu .ensure_list (files )
328
+ file_obj = None
329
329
330
- # Prepare the files
331
- _files = list ()
332
- for ix , f in enumerate (files ):
333
- # Path-like
334
- if isinstance (f , (str , Path )) and os .path .isfile (f ):
335
- _files .append (open (f , "rb" ))
330
+ if isinstance (filepath , (str , Path )) and os .path .isfile (filepath ):
331
+ file_obj = open (filepath , "rb" )
336
332
337
- # Bail out
338
- else :
339
- dtype = type (f )
340
- raise TypeError (
341
- f"File at index { ix } is neither path-like nor readable ({ dtype } )."
342
- )
333
+ # Bail out
334
+ else :
335
+ dtype = type (f )
336
+ raise TypeError (f"File is neither path-like nor readable ({ dtype } )." )
343
337
344
338
# Prepare the payload from the files
345
339
payload = list ()
346
340
347
- for _f in _files :
348
- filename = os .path .basename (_f .name )
349
- ext = filename .split ("." )[- 1 ]
350
- mimetype = mt .types_map [f".{ ext } " ]
351
- payload .append (("file" , (filename , _f , mimetype )))
341
+ filename = os .path .basename (file_obj .name )
342
+ ext = filename .split ("." )[- 1 ]
343
+ mimetype = mt .types_map [f".{ ext } " ]
344
+ payload .append (("file" , (filename , file_obj , mimetype )))
352
345
353
346
# Make the request
354
347
response = self ._make_request (
355
348
method = mcc .HTTP_POST ,
356
349
endpoint = endpoints .FILE_UPLOAD ,
357
350
files = payload ,
351
+ url_params = dict (id = id ),
358
352
return_json = True ,
359
353
)
360
354
361
355
# Close all the file descriptors (requests should do this, but just to be sure)
362
356
for fd in payload :
363
357
fd [1 ][1 ].close ()
364
358
365
- # Automatically attach to a model output
366
- if attach_to :
367
-
368
- _ = self .attach_files_to_model_output (
369
- attach_to , files = mu .get_uploaded_file_ids (response )
370
- )
371
-
372
359
return mu .ensure_list (response )
373
360
374
361
def list_files (self , id : str ) -> Union [dict , requests .Response ]:
@@ -388,42 +375,29 @@ def list_files(self, id: str) -> Union[dict, requests.Response]:
388
375
method = mcc .HTTP_GET , endpoint = endpoints .FILE_LIST , url_params = dict (id = id )
389
376
)
390
377
391
- def attach_files_to_model_output (
392
- self , id : str , files : list
393
- ) -> Union [dict , requests .Response ]:
394
- """Attach files to a model output.
378
+ def delete_file_from_model_output (self , id : str , file_id : str ):
379
+ """Delete file from model output
395
380
396
381
Parameters
397
382
----------
398
383
id : str
399
384
Model output ID.
400
- files : list
401
- List of file IDs .
385
+ file_id : str
386
+ File ID .
402
387
403
388
Returns
404
389
-------
405
- Union[dict, requests.Response ]
406
- Response from ME.org.
390
+ Union[dict, requests.Request ]
391
+ Response from ME.org
407
392
"""
408
-
409
- # Get a list of files for the model output
410
- current_files = self .list_files (id ).get ("data" ).get ("files" )
411
-
412
- # Attach the new files to this list
413
- new_files = current_files + files
414
-
415
- # Update the resource
416
393
return self ._make_request (
417
- mcc .HTTP_PATCH ,
418
- endpoint = endpoints .FILE_LIST ,
419
- url_params = dict (id = id ),
420
- json = new_files ,
394
+ method = mcc .HTTP_DELETE ,
395
+ endpoint = endpoints .FILE_DELETE ,
396
+ url_params = dict (id = id , fileId = file_id ),
421
397
)
422
398
423
- def detach_all_files_from_model_output (
424
- self , id : str
425
- ) -> Union [dict , requests .Response ]:
426
- """Detach all files from a model output.
399
+ def delete_all_files_from_model_output (self , id : str ):
400
+ """Delete file from model output
427
401
428
402
Parameters
429
403
----------
@@ -432,17 +406,22 @@ def detach_all_files_from_model_output(
432
406
433
407
Returns
434
408
-------
435
- Union[dict, requests.Response ]
409
+ Union[dict, requests.Request ]
436
410
Response from ME.org
437
411
"""
438
412
439
- # Update the resource with an empty file list
440
- return self ._make_request (
441
- mcc .HTTP_PATCH ,
442
- endpoint = endpoints .FILE_LIST ,
443
- url_params = dict (id = id ),
444
- json = [],
445
- )
413
+ # Get a list of the files currently on the model output
414
+ files = self .list_files (id )
415
+ file_ids = [f .get ("id" ) for f in files .get ("data" ).get ("files" )]
416
+
417
+ responses = list ()
418
+
419
+ # Do the delete one at a time
420
+ for file_id in file_ids :
421
+ response = self .delete_file_from_model_output (id = id , file_id = file_id )
422
+ responses .append (response )
423
+
424
+ return responses
446
425
447
426
def start_analysis (self , id : str ) -> Union [dict , requests .Response ]:
448
427
"""Start the analysis chain.
0 commit comments