From 4ad579031160974952f66839f8b05d4018a8deb1 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 10 Dec 2018 14:08:32 +0100 Subject: [PATCH] Add API for temporary downloads Closes https://github.com/qutebrowser/qutebrowser-extensions/issues/9 --- qutebrowser/api/downloads.py | 56 ++++++++++++++++++++++++++++++++++ qutebrowser/browser/adblock.py | 9 ++---- 2 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 qutebrowser/api/downloads.py diff --git a/qutebrowser/api/downloads.py b/qutebrowser/api/downloads.py new file mode 100644 index 000000000..f743f5d7a --- /dev/null +++ b/qutebrowser/api/downloads.py @@ -0,0 +1,56 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2018 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + + +import io + +from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, QUrl + +from qutebrowser.browser import downloads, qtnetworkdownloads +from qutebrowser.utils import objreg + + +class TempDownload(QObject): + + """A download of some data into a file object.""" + + finished = pyqtSignal() + + def __init__(self, item: qtnetworkdownloads.DownloadItem) -> None: + self._item = item + self._item.finished.connect(self._on_download_finished) + self.successful = False + self.fileobj = item.fileobj + + @pyqtSlot() + def _on_download_finished(self) -> None: + self.successful = self._item.successful + self.finished.emit() + + +def download_temp(url: QUrl) -> TempDownload: + """Download the given URL into a file object. + + The download is not saved to disk. + """ + fobj = io.BytesIO() + fobj.name = 'temporary: ' + url.host() + target = downloads.FileObjDownloadTarget(fobj) + download_manager = objreg.get('qtnetwork-download-manager') + return download_manager.get(url, target=target, auto_remove=True) diff --git a/qutebrowser/browser/adblock.py b/qutebrowser/browser/adblock.py index fdec79d0f..917942b7f 100644 --- a/qutebrowser/browser/adblock.py +++ b/qutebrowser/browser/adblock.py @@ -25,7 +25,7 @@ import functools import posixpath import zipfile -from qutebrowser.browser import downloads +from qutebrowser.api import downloads from qutebrowser.config import config from qutebrowser.utils import objreg, standarddir, log, message from qutebrowser.api import cmdutils @@ -173,7 +173,6 @@ class HostBlocker: self._config_blocked_hosts) self._blocked_hosts = set() self._done_count = 0 - download_manager = objreg.get('qtnetwork-download-manager') for url in config.val.content.host_blocking.lists: if url.scheme() == 'file': filename = url.toLocalFile() @@ -184,11 +183,7 @@ class HostBlocker: else: self._import_local(filename) else: - fobj = io.BytesIO() - fobj.name = 'adblock: ' + url.host() - target = downloads.FileObjDownloadTarget(fobj) - download = download_manager.get(url, target=target, - auto_remove=True) + download = downloads.download_temp(url) self._in_progress.append(download) download.finished.connect( functools.partial(self._on_download_finished, download))