コード例 #1
0
ファイル: adblock.py プロジェクト: julianuu/qutebrowser
    def adblock_update(self, win_id):
        """Update the adblock block lists.

        This updates ~/.local/share/qutebrowser/blocked-hosts with downloaded
        host lists and re-reads ~/.config/qutebrowser/blocked-hosts.
        """
        self._read_hosts_file(self._config_hosts_file,
                              self._config_blocked_hosts)
        if self._local_hosts_file is None:
            raise cmdexc.CommandError("No data storage is configured!")
        self._blocked_hosts = set()
        self._done_count = 0
        urls = config.get('content', 'host-block-lists')
        download_manager = objreg.get('download-manager',
                                      scope='window',
                                      window='last-focused')
        if urls is None:
            return
        for url in urls:
            if url.scheme() == 'file':
                try:
                    fileobj = open(url.path(), 'rb')
                except OSError as e:
                    message.error(
                        win_id, "adblock: Error while reading {}: "
                        "{}".format(url.path(), e.strerror))
                    continue
                download = FakeDownload(fileobj)
                self._in_progress.append(download)
                self.on_download_finished(download)
            else:
                fobj = io.BytesIO()
                fobj.name = 'adblock: ' + url.host()
                target = usertypes.FileObjDownloadTarget(fobj)
                download = download_manager.get(url,
                                                target=target,
                                                auto_remove=True)
                self._in_progress.append(download)
                download.finished.connect(
                    functools.partial(self.on_download_finished, download))
コード例 #2
0
    def _fetch_url(self, url):
        """Download the given url and add the file to the collection.

        Args:
            url: The file to download as QUrl.
        """
        if url.scheme() not in ['http', 'https']:
            return
        # Prevent loading an asset twice
        if url in self.loaded_urls:
            return
        self.loaded_urls.add(url)

        log.downloads.debug("loading asset at {}".format(url))

        # Using the download manager to download host-blocked urls might crash
        # qute, see the comments/discussion on
        # https://github.com/The-Compiler/qutebrowser/pull/962#discussion_r40256987
        # and https://github.com/The-Compiler/qutebrowser/issues/1053
        host_blocker = objreg.get('host-blocker')
        if host_blocker.is_blocked(url):
            log.downloads.debug("Skipping {}, host-blocked".format(url))
            # We still need an empty file in the output, QWebView can be pretty
            # picky about displaying a file correctly when not all assets are
            # at least referenced in the mhtml file.
            self.writer.add_file(urlutils.encoded_url(url), b'')
            return

        download_manager = objreg.get('download-manager', scope='window',
                                      window=self._win_id)
        target = usertypes.FileObjDownloadTarget(_NoCloseBytesIO())
        item = download_manager.get(url, target=target,
                                    auto_remove=True)
        self.pending_downloads.add((url, item))
        item.finished.connect(functools.partial(self._finished, url, item))
        item.error.connect(functools.partial(self._error, url, item))
        item.cancelled.connect(functools.partial(self._cancelled, url, item))
コード例 #3
0
def test_fileobj():
    fobj = object()
    target = usertypes.FileObjDownloadTarget(fobj)
    assert target.fileobj is fobj
コード例 #4
0

def test_filename():
    target = usertypes.FileDownloadTarget("/foo/bar")
    assert target.filename == "/foo/bar"


def test_fileobj():
    fobj = object()
    target = usertypes.FileObjDownloadTarget(fobj)
    assert target.fileobj is fobj


def test_openfile():
    target = usertypes.OpenFileDownloadTarget()
    assert target.cmdline is None


def test_openfile_custom_command():
    target = usertypes.OpenFileDownloadTarget('echo')
    assert target.cmdline == 'echo'


@pytest.mark.parametrize('obj', [
    usertypes.FileDownloadTarget('foobar'),
    usertypes.FileObjDownloadTarget(None),
    usertypes.OpenFileDownloadTarget(),
])
def test_class_hierarchy(obj):
    assert isinstance(obj, usertypes.DownloadTarget)