3030]
3131
3232
33+ class InvalidDownloadDirectoryError (BaseException ):
34+ """Used for signaling a non-existent download directory"""
35+
36+
3337class DownloadJob :
3438 def __init__ (self , api , resource_id , download_path , condensed = False ):
3539 """Wrapper for resource downloads
@@ -155,7 +159,13 @@ def get_dataset_dict(self):
155159 return self ._dataset_dict
156160
157161 def get_download_path (self ):
158- """Return the final location to which the file is downloaded"""
162+ """Return the final location to which the file is downloaded
163+
164+ Raises
165+ ------
166+ InvalidDownloadDirectoryError
167+ When the target directory does not exist
168+ """
159169 if self ._download_path is None :
160170 if self ._user_path .is_dir ():
161171 # Compute the resource path from the dataset dictionary
@@ -178,7 +188,7 @@ def get_download_path(self):
178188 # user specified an actual file
179189 self ._download_path = self ._user_path
180190 else :
181- raise ValueError (
191+ raise InvalidDownloadDirectoryError (
182192 f"The `download_path` passed in __init__ is invalid. "
183193 f"Please make sure the target directory for "
184194 f"{ self ._user_path } exists." )
@@ -309,7 +319,7 @@ def get_status(self):
309319 data ["bytes local" ] = size_temp
310320 else :
311321 data ["bytes local" ] = 0
312- except api_errors .APINotFoundError :
322+ except ( api_errors .APINotFoundError , InvalidDownloadDirectoryError ) :
313323 # The user likely tried to download the file from a different
314324 # host or an evil admin deleted a file.
315325 self .traceback = traceback .format_exc ()
@@ -326,7 +336,11 @@ def get_status(self):
326336 def retry_download (self ):
327337 """Retry downloading resources when an error occured"""
328338 if self .state in ["abort" , "error" ]:
339+ # Create the target download directory
340+ if self ._user_path is not None :
341+ self ._user_path .parent .mkdir (parents = True , exist_ok = True )
329342 self .set_state ("init" )
343+ self .traceback = None
330344 else :
331345 raise ValueError ("Can only retry download in error state!" )
332346
0 commit comments