Add DownloadTarget.suggested_filename

This commit is contained in:
Florian Bruhin 2016-11-09 08:06:57 +01:00
parent 970e4d3e03
commit 80562fbdca
2 changed files with 25 additions and 6 deletions

View File

@ -438,12 +438,9 @@ class DownloadManager(downloads.AbstractDownloadManager):
The created DownloadItem. The created DownloadItem.
""" """
if not suggested_filename: if not suggested_filename:
if isinstance(target, usertypes.FileDownloadTarget): try:
suggested_filename = os.path.basename(target.filename) suggested_filename = target.suggested_filename()
elif (isinstance(target, usertypes.FileObjDownloadTarget) and except usertypes.NoFilenameError:
getattr(target.fileobj, 'name', None)):
suggested_filename = target.fileobj.name
else:
_, suggested_filename = http.parse_content_disposition(reply) _, suggested_filename = http.parse_content_disposition(reply)
log.downloads.debug("fetch: {} -> {}".format(reply.url(), log.downloads.debug("fetch: {} -> {}".format(reply.url(),
suggested_filename)) suggested_filename))

View File

@ -23,6 +23,7 @@ Module attributes:
_UNSET: Used as default argument in the constructor so default can be None. _UNSET: Used as default argument in the constructor so default can be None.
""" """
import os.path
import operator import operator
import collections.abc import collections.abc
import enum as pyenum import enum as pyenum
@ -268,6 +269,11 @@ JsWorld = enum('JsWorld', ['main', 'application', 'user', 'jseval'])
MessageLevel = enum('MessageLevel', ['error', 'warning', 'info']) MessageLevel = enum('MessageLevel', ['error', 'warning', 'info'])
class NoFilenameError(Exception):
"""Raised when we can't find out a filename in DownloadTarget."""
# Where a download should be saved # Where a download should be saved
class DownloadTarget: class DownloadTarget:
@ -276,6 +282,10 @@ class DownloadTarget:
def __init__(self): def __init__(self):
raise NotImplementedError raise NotImplementedError
def suggested_filename(self):
"""Get the suggested filename for this download target."""
raise NotImplementedError
class FileDownloadTarget(DownloadTarget): class FileDownloadTarget(DownloadTarget):
@ -289,6 +299,9 @@ class FileDownloadTarget(DownloadTarget):
# pylint: disable=super-init-not-called # pylint: disable=super-init-not-called
self.filename = filename self.filename = filename
def suggested_filename(self):
return os.path.basename(self.filename)
class FileObjDownloadTarget(DownloadTarget): class FileObjDownloadTarget(DownloadTarget):
@ -302,6 +315,12 @@ class FileObjDownloadTarget(DownloadTarget):
# pylint: disable=super-init-not-called # pylint: disable=super-init-not-called
self.fileobj = fileobj self.fileobj = fileobj
def suggested_filename(self):
try:
return self.fileobj.name
except AttributeError:
raise NoFilenameError
class OpenFileDownloadTarget(DownloadTarget): class OpenFileDownloadTarget(DownloadTarget):
@ -317,6 +336,9 @@ class OpenFileDownloadTarget(DownloadTarget):
# pylint: disable=super-init-not-called # pylint: disable=super-init-not-called
self.cmdline = cmdline self.cmdline = cmdline
def suggested_filename(self):
raise NoFilenameError
class Question(QObject): class Question(QObject):