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:
Daniel Schadt 2016-07-12 16:46:03 +02:00
parent ed5fb4de4a
commit a088f238e5
6 changed files with 32 additions and 37 deletions

View File

@ -210,7 +210,7 @@ class HostBlocker:
else: else:
fobj = io.BytesIO() fobj = io.BytesIO()
fobj.name = 'adblock: ' + url.host() fobj.name = 'adblock: ' + url.host()
target = usertypes.DownloadTarget.FileObj(fobj) target = usertypes.FileObjDownloadTarget(fobj)
download = download_manager.get(url, target=target, download = download_manager.get(url, target=target,
auto_remove=True) auto_remove=True)
self._in_progress.append(download) self._in_progress.append(download)

View File

@ -1224,7 +1224,7 @@ class CommandDispatcher:
if dest is None: if dest is None:
target = None target = None
else: else:
target = usertypes.DownloadTarget.File(dest) target = usertypes.FileDownloadTarget(dest)
download_manager.get(url, target=target) download_manager.get(url, target=target)
elif mhtml_: elif mhtml_:
self._download_mhtml(dest) self._download_mhtml(dest)
@ -1235,7 +1235,7 @@ class CommandDispatcher:
if dest is None: if dest is None:
target = None target = None
else: else:
target = usertypes.DownloadTarget.File(dest) target = usertypes.FileDownloadTarget(dest)
download_manager.get(self._current_url(), page=page, download_manager.get(self._current_url(), page=page,
target=target) target=target)

View File

@ -874,9 +874,9 @@ class DownloadManager(QAbstractListModel):
The created DownloadItem. The created DownloadItem.
""" """
if not suggested_filename: if not suggested_filename:
if isinstance(target, usertypes.DownloadTarget.File): if isinstance(target, usertypes.FileDownloadTarget):
suggested_filename = os.path.basename(target.filename) 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)): getattr(target.fileobj, 'name', None)):
suggested_filename = target.fileobj.name suggested_filename = target.fileobj.name
else: else:
@ -922,7 +922,7 @@ class DownloadManager(QAbstractListModel):
# User doesn't want to be asked, so just use the download_dir # User doesn't want to be asked, so just use the download_dir
if filename is not None: if filename is not None:
target = usertypes.DownloadTarget.File(filename) target = usertypes.FileDownloadTarget(filename)
self._set_download_target(download, suggested_filename, target) self._set_download_target(download, suggested_filename, target)
return download return download
@ -946,12 +946,12 @@ class DownloadManager(QAbstractListModel):
suggested_filename: The suggested filename. suggested_filename: The suggested filename.
target: The usertypes.DownloadTarget for this download. target: The usertypes.DownloadTarget for this download.
""" """
if isinstance(target, usertypes.DownloadTarget.FileObj): if isinstance(target, usertypes.FileObjDownloadTarget):
download.set_fileobj(target.fileobj) download.set_fileobj(target.fileobj)
download.autoclose = False download.autoclose = False
elif isinstance(target, usertypes.DownloadTarget.File): elif isinstance(target, usertypes.FileDownloadTarget):
download.set_filename(target.filename) download.set_filename(target.filename)
elif isinstance(target, usertypes.DownloadTarget.OpenDownload): elif isinstance(target, usertypes.OpenFileDownloadTarget):
tmp_manager = objreg.get('temporary-downloads') tmp_manager = objreg.get('temporary-downloads')
fobj = tmp_manager.get_tmpfile(suggested_filename) fobj = tmp_manager.get_tmpfile(suggested_filename)
download.finished.connect(download.open_file) download.finished.connect(download.open_file)

View File

@ -343,7 +343,7 @@ class _Downloader:
download_manager = objreg.get('download-manager', scope='window', download_manager = objreg.get('download-manager', scope='window',
window=self._win_id) window=self._win_id)
target = usertypes.DownloadTarget.FileObj(_NoCloseBytesIO()) target = usertypes.FileObjDownloadTarget(_NoCloseBytesIO())
item = download_manager.get(url, target=target, item = download_manager.get(url, target=target,
auto_remove=True) auto_remove=True)
self.pending_downloads.add((url, item)) self.pending_downloads.add((url, item))

View File

@ -248,7 +248,7 @@ class Prompter(QObject):
self._question.done() self._question.done()
elif self._question.mode == usertypes.PromptMode.download: elif self._question.mode == usertypes.PromptMode.download:
# User just entered a path for a 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 self._question.answer = target
modeman.maybe_leave(self._win_id, usertypes.KeyMode.prompt, modeman.maybe_leave(self._win_id, usertypes.KeyMode.prompt,
'prompt accept') 'prompt accept')
@ -299,7 +299,7 @@ class Prompter(QObject):
if self._question.mode != usertypes.PromptMode.download: if self._question.mode != usertypes.PromptMode.download:
# We just ignore this if we don't have a download question. # We just ignore this if we don't have a download question.
return return
self._question.answer = usertypes.DownloadTarget.OpenDownload() self._question.answer = usertypes.OpenFileDownloadTarget()
modeman.maybe_leave(self._win_id, usertypes.KeyMode.prompt, modeman.maybe_leave(self._win_id, usertypes.KeyMode.prompt,
'download open') 'download open')
self._question.done() self._question.done()

View File

@ -259,46 +259,41 @@ Backend = enum('Backend', ['QtWebKit', 'QtWebEngine'])
# Where a download should be saved # Where a download should be saved
class DownloadTarget: class DownloadTarget:
"""Augmented enum that directs how a download should be saved. """Abstract base class for different download targets."""
Objects of this class cannot be instantiated directly, use the "subclasses"
instead.
"""
def __init__(self): def __init__(self):
raise NotImplementedError 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: Attributes:
filename: Filename where the download should be saved. filename: Filename where the download should be saved.
""" """
def __init__(self, filename): def __init__(self, filename):
self.filename = filename self.filename = filename
class FileObj:
"""Save the download to the given file-like object. class FileObjDownloadTarget(DownloadTarget):
Attributes: """Save the download to the given file-like object.
fileobj: File-like object where the download should be written to.
"""
def __init__(self, fileobj): Attributes:
self.fileobj = fileobj 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 pass