@@ -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,40 +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 )
292
- responses . append ( response )
293
+ response = self ._upload_file (fp , id = id )
294
+ responses += response
293
295
else :
294
-
295
- # Disable the auto attach to avoid race condition
296
- responses = self ._upload_files_parallel (
297
- files , n = n , attach_to = None , progress = progress
296
+ responses += self ._upload_files_parallel (
297
+ files , n = n , id = id , progress = progress
298
298
)
299
299
300
- if attach_to :
301
-
302
- file_ids = list ()
303
-
304
- for response in responses :
305
- file_id = response .get ("data" ).get ("files" )[0 ].get ("file" )
306
- file_ids .append (file_id )
307
-
308
- self .attach_files_to_model_output (id = attach_to , files = file_ids )
309
-
310
- return mu .ensure_list (responses )
300
+ # return mu.ensure_list(responses)
301
+ return responses
311
302
312
303
def _upload_file (
313
- self ,
314
- files : Union [str , Path , list ],
315
- attach_to : str = None ,
304
+ self , filepath : Union [str , Path ], id : str
316
305
) -> Union [dict , requests .Response ]:
317
- """Upload a file.
306
+ """Upload a single file.
318
307
319
308
Parameters
320
309
----------
321
- files : path-like, list
322
- Path to the file, or a list containing paths.
323
- attach_to : str, optional
324
- 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
325
314
326
315
Returns
327
316
-------
@@ -331,56 +320,42 @@ def _upload_file(
331
320
Raises
332
321
------
333
322
TypeError
334
- When supplied file(s) are neither path-like nor readable.
323
+ When supplied file is neither path-like nor readable.
335
324
FileNotFoundError
336
- When supplied file(s) cannot be found.
325
+ When supplied file cannot be found.
337
326
"""
338
327
339
- # Cast as list for iterative upload
340
- files = mu .ensure_list (files )
328
+ file_obj = None
341
329
342
- # Prepare the files
343
- _files = list ()
344
- for ix , f in enumerate (files ):
345
- # Path-like
346
- if isinstance (f , (str , Path )) and os .path .isfile (f ):
347
- _files .append (open (f , "rb" ))
330
+ if isinstance (filepath , (str , Path )) and os .path .isfile (filepath ):
331
+ file_obj = open (filepath , "rb" )
348
332
349
- # Bail out
350
- else :
351
- dtype = type (f )
352
- raise TypeError (
353
- f"File at index { ix } is neither path-like nor readable ({ dtype } )."
354
- )
333
+ # Bail out
334
+ else :
335
+ dtype = type (file_obj )
336
+ raise TypeError (f"File is neither path-like nor readable ({ dtype } )." )
355
337
356
338
# Prepare the payload from the files
357
339
payload = list ()
358
340
359
- for _f in _files :
360
- filename = os .path .basename (_f .name )
361
- ext = filename .split ("." )[- 1 ]
362
- mimetype = mt .types_map [f".{ ext } " ]
363
- 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 )))
364
345
365
346
# Make the request
366
347
response = self ._make_request (
367
348
method = mcc .HTTP_POST ,
368
349
endpoint = endpoints .FILE_UPLOAD ,
369
350
files = payload ,
351
+ url_params = dict (id = id ),
370
352
return_json = True ,
371
353
)
372
354
373
355
# Close all the file descriptors (requests should do this, but just to be sure)
374
356
for fd in payload :
375
357
fd [1 ][1 ].close ()
376
358
377
- # Automatically attach to a model output
378
- if attach_to :
379
-
380
- _ = self .attach_files_to_model_output (
381
- attach_to , files = mu .get_uploaded_file_ids (response )
382
- )
383
-
384
359
return mu .ensure_list (response )
385
360
386
361
def list_files (self , id : str ) -> Union [dict , requests .Response ]:
@@ -400,42 +375,29 @@ def list_files(self, id: str) -> Union[dict, requests.Response]:
400
375
method = mcc .HTTP_GET , endpoint = endpoints .FILE_LIST , url_params = dict (id = id )
401
376
)
402
377
403
- def attach_files_to_model_output (
404
- self , id : str , files : list
405
- ) -> Union [dict , requests .Response ]:
406
- """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
407
380
408
381
Parameters
409
382
----------
410
383
id : str
411
384
Model output ID.
412
- files : list
413
- List of file IDs .
385
+ file_id : str
386
+ File ID .
414
387
415
388
Returns
416
389
-------
417
- Union[dict, requests.Response ]
418
- Response from ME.org.
390
+ Union[dict, requests.Request ]
391
+ Response from ME.org
419
392
"""
420
-
421
- # Get a list of files for the model output
422
- current_files = self .list_files (id ).get ("data" ).get ("files" )
423
-
424
- # Attach the new files to this list
425
- new_files = current_files + files
426
-
427
- # Update the resource
428
393
return self ._make_request (
429
- mcc .HTTP_PATCH ,
430
- endpoint = endpoints .FILE_LIST ,
431
- url_params = dict (id = id ),
432
- json = new_files ,
394
+ method = mcc .HTTP_DELETE ,
395
+ endpoint = endpoints .FILE_DELETE ,
396
+ url_params = dict (id = id , fileId = file_id ),
433
397
)
434
398
435
- def detach_all_files_from_model_output (
436
- self , id : str
437
- ) -> Union [dict , requests .Response ]:
438
- """Detach all files from a model output.
399
+ def delete_all_files_from_model_output (self , id : str ):
400
+ """Delete file from model output
439
401
440
402
Parameters
441
403
----------
@@ -444,17 +406,22 @@ def detach_all_files_from_model_output(
444
406
445
407
Returns
446
408
-------
447
- Union[dict, requests.Response ]
409
+ Union[dict, requests.Request ]
448
410
Response from ME.org
449
411
"""
450
412
451
- # Update the resource with an empty file list
452
- return self ._make_request (
453
- mcc .HTTP_PATCH ,
454
- endpoint = endpoints .FILE_LIST ,
455
- url_params = dict (id = id ),
456
- json = [],
457
- )
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
458
425
459
426
def start_analysis (self , id : str ) -> Union [dict , requests .Response ]:
460
427
"""Start the analysis chain.
0 commit comments