@@ -257,6 +257,7 @@ def download(
257257 field_name : Optional [str ] = None ,
258258 download_dir : Optional [str ] = None ,
259259 show_progress : bool = True ,
260+ return_dir_names : bool = False ,
260261 ) -> list [str ]:
261262 """Download Data object's files and directories.
262263
@@ -270,6 +271,12 @@ def download(
270271 * re.data.get(42).download(file_name='alignment7.bam')
271272 * re.data.get(42).download(field_name='bam')
272273
274+ The function returns a list of downloaded file names. In case the
275+ Data object contains directories, the user can specify to return directory names
276+ instead of file names contained in those directories via the return_dir_names argument.
277+ WARNING: If neither file_name nor field_name is specified and return_dir_names is set
278+ to True, the method will download both directories and files when the Data object
279+ contains both. However, the returned list will include only the directory names.
273280 """
274281 if file_name and field_name :
275282 raise ValueError ("Only one of file_name or field_name may be given." )
@@ -281,41 +288,52 @@ def download(
281288 files = files , download_dir = download_dir , show_progress = show_progress
282289 )
283290
284- return file_names
291+ dir_names = self ._files_dirs ("dir" , file_name , field_name )
292+ if dir_names and return_dir_names :
293+ asset_names = dir_names
294+ else :
295+ asset_names = file_names
296+
297+ return asset_names
285298
286299 def download_and_rename (
287300 self ,
288- custom_file_name : str ,
301+ custom_asset_name : str ,
289302 overwrite_existing : bool = False ,
290303 field_name : Optional [str ] = None ,
291304 file_name : Optional [str ] = None ,
292305 download_dir : Optional [str ] = None ,
293306 ):
294- """Download and rename a single file from the Data object."""
307+ """Download and rename an asset from the Data object."""
295308
296309 if not field_name and not file_name :
297310 raise ValueError ("Either 'file_name' or 'field_name' must be given." )
298311
299312 if download_dir is None :
300313 download_dir = os .getcwd ()
301- destination_file_path = os .path .join (download_dir , custom_file_name )
302- if os .path .exists (destination_file_path ) and not overwrite_existing :
303- raise FileExistsError (
304- f"File with path '{ destination_file_path } ' already exists. Skipping download."
314+ destination_asset_path = os .path .join (download_dir , custom_asset_name )
315+ if os .path .exists (destination_asset_path ) and not overwrite_existing :
316+ logging .warning (
317+ f"File or directory with path '{ destination_asset_path } ' already exists. "
318+ "Skipping download."
305319 )
320+ return
306321
307- source_file_name = self .download (
322+ source_asset_name = self .download (
308323 file_name = file_name ,
309324 field_name = field_name ,
310325 download_dir = download_dir ,
326+ return_dir_names = True ,
311327 )[0 ]
312328
313- source_file_path = os .path .join (download_dir , source_file_name )
329+ source_asset_path = os .path .join (download_dir , source_asset_name )
314330
315- logging .info (f"Renaming file '{ source_file_name } ' to '{ custom_file_name } '." )
331+ logging .info (
332+ f"Renaming the downloaded asset from '{ source_asset_name } ' to '{ custom_asset_name } '."
333+ )
316334 os .rename (
317- source_file_path ,
318- destination_file_path ,
335+ source_asset_path ,
336+ destination_asset_path ,
319337 )
320338
321339 def stdout (self ) -> str :
0 commit comments