Call _set_fileobj in AbstractDownloadItem.set_target

This commit is contained in:
Florian Bruhin 2016-11-08 20:33:25 +01:00
parent 19c7d747dd
commit b00c889dd1
2 changed files with 8 additions and 11 deletions

View File

@ -481,10 +481,14 @@ class AbstractDownloadItem(QObject):
"""Ask a confirmation question for the download."""
raise NotImplementedError
def _set_fileobj(self, fileobj):
def _set_fileobj(self, fileobj, *, autoclose=True):
"""Set a file object to save the download to.
Not supported by QtWebEngine.
Args:
fileobj: The file object to download to.
autoclose: Close the file object automatically when it's done.
"""
raise NotImplementedError
@ -567,8 +571,7 @@ class AbstractDownloadItem(QObject):
target: The usertypes.DownloadTarget for this download.
"""
if isinstance(target, usertypes.FileObjDownloadTarget):
raise UnsupportedOperationError("FileObjDownloadTarget is "
"unsupported")
self._set_fileobj(target.fileobj, autoclose=False)
elif isinstance(target, usertypes.FileDownloadTarget):
self._set_filename(target.filename)
elif isinstance(target, usertypes.OpenFileDownloadTarget):

View File

@ -186,7 +186,7 @@ class DownloadItem(downloads.AbstractDownloadItem):
no_action=no_action, cancel_action=no_action,
abort_on=[self.cancelled, self.error])
def _set_fileobj(self, fileobj):
def _set_fileobj(self, fileobj, *, autoclose=True):
""""Set the file object to write the download to.
Args:
@ -196,6 +196,7 @@ class DownloadItem(downloads.AbstractDownloadItem):
raise ValueError("fileobj was already set! Old: {}, new: "
"{}".format(self.fileobj, fileobj))
self.fileobj = fileobj
self.autoclose = autoclose
try:
self._read_timer.stop()
log.downloads.debug("buffer: {} bytes".format(self._buffer.tell()))
@ -333,13 +334,6 @@ class DownloadItem(downloads.AbstractDownloadItem):
old_reply.deleteLater()
return True
def set_target(self, target):
if isinstance(target, usertypes.FileObjDownloadTarget):
self._set_fileobj(target.fileobj)
self.autoclose = False
else:
super().set_target(target)
class DownloadManager(downloads.AbstractDownloadManager):