change required parameter type to file-like objects

This commit is contained in:
Mike Fährmann
2015-12-21 22:46:49 +01:00
parent 5a8541afa5
commit ecc6542fc8
2 changed files with 13 additions and 10 deletions

View File

@@ -15,16 +15,18 @@ class BasicDownloader():
max_tries = 5 max_tries = 5
def download(self, url, path): def download(self, url, fileobj):
"""Download the resource at 'url' and write it to a file given by 'path'""" """Download the resource at 'url' and write it to a file-like object"""
with open(path, "wb") as file: try:
return self.download_impl(url, fileobj)
except:
# remove file if download failed
try: try:
return self.download_impl(url, file) fileobj.close()
except: os.unlink(fileobj.name)
#remove file if download failed except AttributeError:
file.close() pass
os.unlink(path) raise
raise
def download_impl(self, url, file_handle): def download_impl(self, url, file_handle):
"""Actual implementaion of the download process""" """Actual implementaion of the download process"""

View File

@@ -93,7 +93,8 @@ class DownloadJob(Job):
return return
dlinstance = self.get_downloader(url) dlinstance = self.get_downloader(url)
self.printer.start(path) self.printer.start(path)
tries = dlinstance.download(url, path) with open(path, "wb") as file:
tries = dlinstance.download(url, file)
self.printer.success(path, tries) self.printer.success(path, tries)
def set_directory(self, msg): def set_directory(self, msg):