usertypes: remove Frankenstein's enum
This approach is not as weird in Python and still works. DownloadTarget.OpenDownload has been renamed to OpenFileDownloadTarget, since OpenDownloadDownloadTarget didn't look as nice.
This commit is contained in:
parent
ed5fb4de4a
commit
a088f238e5
@ -210,7 +210,7 @@ class HostBlocker:
|
||||
else:
|
||||
fobj = io.BytesIO()
|
||||
fobj.name = 'adblock: ' + url.host()
|
||||
target = usertypes.DownloadTarget.FileObj(fobj)
|
||||
target = usertypes.FileObjDownloadTarget(fobj)
|
||||
download = download_manager.get(url, target=target,
|
||||
auto_remove=True)
|
||||
self._in_progress.append(download)
|
||||
|
@ -1224,7 +1224,7 @@ class CommandDispatcher:
|
||||
if dest is None:
|
||||
target = None
|
||||
else:
|
||||
target = usertypes.DownloadTarget.File(dest)
|
||||
target = usertypes.FileDownloadTarget(dest)
|
||||
download_manager.get(url, target=target)
|
||||
elif mhtml_:
|
||||
self._download_mhtml(dest)
|
||||
@ -1235,7 +1235,7 @@ class CommandDispatcher:
|
||||
if dest is None:
|
||||
target = None
|
||||
else:
|
||||
target = usertypes.DownloadTarget.File(dest)
|
||||
target = usertypes.FileDownloadTarget(dest)
|
||||
download_manager.get(self._current_url(), page=page,
|
||||
target=target)
|
||||
|
||||
|
@ -874,9 +874,9 @@ class DownloadManager(QAbstractListModel):
|
||||
The created DownloadItem.
|
||||
"""
|
||||
if not suggested_filename:
|
||||
if isinstance(target, usertypes.DownloadTarget.File):
|
||||
if isinstance(target, usertypes.FileDownloadTarget):
|
||||
suggested_filename = os.path.basename(target.filename)
|
||||
elif (isinstance(target, usertypes.DownloadTarget.FileObj) and
|
||||
elif (isinstance(target, usertypes.FileObjDownloadTarget) and
|
||||
getattr(target.fileobj, 'name', None)):
|
||||
suggested_filename = target.fileobj.name
|
||||
else:
|
||||
@ -922,7 +922,7 @@ class DownloadManager(QAbstractListModel):
|
||||
|
||||
# User doesn't want to be asked, so just use the download_dir
|
||||
if filename is not None:
|
||||
target = usertypes.DownloadTarget.File(filename)
|
||||
target = usertypes.FileDownloadTarget(filename)
|
||||
self._set_download_target(download, suggested_filename, target)
|
||||
return download
|
||||
|
||||
@ -946,12 +946,12 @@ class DownloadManager(QAbstractListModel):
|
||||
suggested_filename: The suggested filename.
|
||||
target: The usertypes.DownloadTarget for this download.
|
||||
"""
|
||||
if isinstance(target, usertypes.DownloadTarget.FileObj):
|
||||
if isinstance(target, usertypes.FileObjDownloadTarget):
|
||||
download.set_fileobj(target.fileobj)
|
||||
download.autoclose = False
|
||||
elif isinstance(target, usertypes.DownloadTarget.File):
|
||||
elif isinstance(target, usertypes.FileDownloadTarget):
|
||||
download.set_filename(target.filename)
|
||||
elif isinstance(target, usertypes.DownloadTarget.OpenDownload):
|
||||
elif isinstance(target, usertypes.OpenFileDownloadTarget):
|
||||
tmp_manager = objreg.get('temporary-downloads')
|
||||
fobj = tmp_manager.get_tmpfile(suggested_filename)
|
||||
download.finished.connect(download.open_file)
|
||||
|
@ -343,7 +343,7 @@ class _Downloader:
|
||||
|
||||
download_manager = objreg.get('download-manager', scope='window',
|
||||
window=self._win_id)
|
||||
target = usertypes.DownloadTarget.FileObj(_NoCloseBytesIO())
|
||||
target = usertypes.FileObjDownloadTarget(_NoCloseBytesIO())
|
||||
item = download_manager.get(url, target=target,
|
||||
auto_remove=True)
|
||||
self.pending_downloads.add((url, item))
|
||||
|
@ -248,7 +248,7 @@ class Prompter(QObject):
|
||||
self._question.done()
|
||||
elif self._question.mode == usertypes.PromptMode.download:
|
||||
# User just entered a path for a download.
|
||||
target = usertypes.DownloadTarget.File(prompt.lineedit.text())
|
||||
target = usertypes.FileDownloadTarget(prompt.lineedit.text())
|
||||
self._question.answer = target
|
||||
modeman.maybe_leave(self._win_id, usertypes.KeyMode.prompt,
|
||||
'prompt accept')
|
||||
@ -299,7 +299,7 @@ class Prompter(QObject):
|
||||
if self._question.mode != usertypes.PromptMode.download:
|
||||
# We just ignore this if we don't have a download question.
|
||||
return
|
||||
self._question.answer = usertypes.DownloadTarget.OpenDownload()
|
||||
self._question.answer = usertypes.OpenFileDownloadTarget()
|
||||
modeman.maybe_leave(self._win_id, usertypes.KeyMode.prompt,
|
||||
'download open')
|
||||
self._question.done()
|
||||
|
@ -259,46 +259,41 @@ Backend = enum('Backend', ['QtWebKit', 'QtWebEngine'])
|
||||
# Where a download should be saved
|
||||
class DownloadTarget:
|
||||
|
||||
"""Augmented enum that directs how a download should be saved.
|
||||
|
||||
Objects of this class cannot be instantiated directly, use the "subclasses"
|
||||
instead.
|
||||
"""
|
||||
"""Abstract base class for different download targets."""
|
||||
|
||||
def __init__(self):
|
||||
raise NotImplementedError
|
||||
|
||||
# Due to technical limitations, these can't be actual subclasses without a
|
||||
# workaround. But they should still be part of DownloadTarget to get the
|
||||
# enum-like access (usertypes.DownloadTarget.File, like
|
||||
# usertypes.PromptMode.download).
|
||||
|
||||
class File:
|
||||
class FileDownloadTarget(DownloadTarget):
|
||||
|
||||
"""Save the download to the given file.
|
||||
"""Save the download to the given file.
|
||||
|
||||
Attributes:
|
||||
filename: Filename where the download should be saved.
|
||||
"""
|
||||
Attributes:
|
||||
filename: Filename where the download should be saved.
|
||||
"""
|
||||
|
||||
def __init__(self, filename):
|
||||
self.filename = filename
|
||||
def __init__(self, filename):
|
||||
self.filename = filename
|
||||
|
||||
class FileObj:
|
||||
|
||||
"""Save the download to the given file-like object.
|
||||
class FileObjDownloadTarget(DownloadTarget):
|
||||
|
||||
Attributes:
|
||||
fileobj: File-like object where the download should be written to.
|
||||
"""
|
||||
"""Save the download to the given file-like object.
|
||||
|
||||
def __init__(self, fileobj):
|
||||
self.fileobj = fileobj
|
||||
Attributes:
|
||||
fileobj: File-like object where the download should be written to.
|
||||
"""
|
||||
|
||||
class OpenDownload:
|
||||
def __init__(self, fileobj):
|
||||
self.fileobj = fileobj
|
||||
|
||||
"""Save the download in a temp dir and directly open it."""
|
||||
|
||||
class OpenFileDownloadTarget(DownloadTarget):
|
||||
|
||||
"""Save the download in a temp dir and directly open it."""
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user