Fix more lint issues related to content blocking

This commit is contained in:
Árni Dagur 2020-08-27 12:50:26 +00:00 committed by Árni Dagur
parent fd155628e1
commit ffc73980f6
8 changed files with 51 additions and 29 deletions

View File

@ -46,6 +46,7 @@ disable=locally-disabled,
too-many-statements,
too-few-public-methods,
import-outside-toplevel,
bad-continuation # This lint disagrees with Black
[BASIC]
function-rgx=[a-z_][a-z0-9_]{2,50}$

View File

@ -312,7 +312,7 @@ class HostBlocker:
@hook.config_changed("content.blocking.hosts.lists")
def on_config_changed() -> None:
def on_lists_changed() -> None:
host_blocker.update_files()

View File

@ -21,7 +21,6 @@
import io
import os.path
import functools
import logging
import typing
import pathlib
@ -29,7 +28,6 @@ import pathlib
from PyQt5.QtCore import QUrl
from qutebrowser.api import (
cmdutils,
hook,
config,
message,
@ -79,9 +77,8 @@ def _possibly_show_missing_dependency_warning() -> None:
method = config.val.content.blocking.method
if method in ("adblock", "both"):
message.warning(
"Ad blocking method is set to '{}' but 'adblock' dependency is not installed.".format(
method
)
"Ad blocking method is set to '{}' but 'adblock' dependency is"
" not installed.".format(method)
)
@ -178,16 +175,16 @@ class BraveAdBlocker:
return False
if result.exception is not None and not result.important:
logger.debug(
"Excepting {} from being blocked by {} because of {}".format(
request_url.toDisplayString(), result.filter, result.exception
)
"Excepting %s from being blocked by %s because of %s",
request_url.toDisplayString(),
result.filter,
result.exception,
)
return False
if _is_whitelisted_url(request_url):
logger.debug(
"Request to {} is whitelisted, thus not blocked".format(
request_url.toDisplayString()
)
"Request to %s is whitelisted, thus not blocked",
request_url.toDisplayString(),
)
return False
return True
@ -196,15 +193,15 @@ class BraveAdBlocker:
"""Block the given request if necessary."""
if self._is_blocked(info.request_url, info.first_party_url, info.resource_type):
logger.debug(
"Request to {} blocked by ad blocker.".format(
info.request_url.toDisplayString()
)
"Request to %s blocked by ad blocker.",
info.request_url.toDisplayString(),
)
info.block()
def read_cache(self) -> None:
"""Initialize the adblocking engine from cache file."""
if self._cache_path.is_file():
logger.debug("Loading cached adblock data: {}".format(self._cache_path))
logger.debug("Loading cached adblock data: %s", self._cache_path)
self._engine.deserialize_from_file(str(self._cache_path))
else:
if (
@ -227,7 +224,7 @@ class BraveAdBlocker:
self._on_lists_downloaded()
else:
self._finished_registering_downloads = False
for i, url in enumerate(blocklists):
for url in blocklists:
blockutils.download_blocklist_url(
url, self._on_download_finished, self._in_progress
)
@ -287,12 +284,14 @@ class BraveAdBlocker:
@hook.config_changed("content.blocking.adblock.lists")
def on_lists_changed() -> None:
"""Remove cached blocker from disk when blocklist changes."""
if ad_blocker is not None:
ad_blocker.update_files()
@hook.config_changed("content.blocking.method")
def on_method_changed() -> None:
"""When the adblocking method changes, update blocker accordingly."""
if ad_blocker is not None:
# This implies the 'adblock' dependency is satisfied
ad_blocker.enabled = _should_be_used()

View File

@ -1,3 +1,23 @@
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2020 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# 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 <http://www.gnu.org/licenses/>.
"""Code that is shared between the host blocker and Brave ad blocker."""
import typing
@ -14,8 +34,9 @@ class FakeDownload(downloads.TempDownload):
"""A download stub to use on_download_finished with local files."""
def __init__(
self, fileobj: typing.IO[bytes] # pylint: disable=super-init-not-called
self, fileobj: typing.IO[bytes]
) -> None:
# pylint: disable=super-init-not-called
self.fileobj = fileobj
self.successful = True
@ -25,8 +46,7 @@ def download_blocklist_url(
on_download_finished: typing.Callable[[downloads.TempDownload], None],
in_progress: typing.List[downloads.TempDownload],
) -> None:
"""
Take a blocklist url and queue it for download.
"""Take a blocklist url and queue it for download.
Args:
url: url to download
@ -65,7 +85,7 @@ def _import_local(
fileobj = open(filename, "rb")
except OSError as e:
message.error(
"adblock: Error while reading {}: {}".format(filename, e.strerror)
"blockutils: Error while reading {}: {}".format(filename, e.strerror)
)
return
download = FakeDownload(fileobj)

View File

@ -681,9 +681,9 @@ content.blocking.whitelist:
Note this whitelists otherwise blocked requests, not first-party URLs. As
an example, if `example.org` loads an ad from `ads.example.org`, the
whitelist entry could be `https://ads.example.org/*`. If you want to disable the
adblocker on a given page, use the `content.host_blocking.enabled` setting
with a URL pattern instead.
whitelist entry could be `https://ads.example.org/*`. If you want to
disable the adblocker on a given page, use the
`content.host_blocking.enabled` setting with a URL pattern instead.
content.hyperlink_auditing:
default: false

View File

@ -132,7 +132,10 @@ def whitelist_generator(): # noqa
yield 'scripts.importer.import_moz_places.places.row_factory'
# component hooks
yield 'qutebrowser.components.adblock.on_config_changed'
yield 'qutebrowser.components.adblock.on_lists_changed'
yield 'qutebrowser.components.braveadblock.on_lists_changed'
yield 'qutebrowser.components.adblock.on_method_changed'
yield 'qutebrowser.components.braveadblock.on_method_changed'
# used in type comments
yield 'pending_download_type'

View File

@ -545,7 +545,7 @@ Feature: Various utility commands.
Scenario: Simple adblock update
When I set up "simple" as block lists
And I run :adblock-update
Then the message "adblock: Read 1 hosts from 1 sources." should be shown
Then the message "hostblock: Read 1 hosts from 1 sources." should be shown
Scenario: Resource with invalid URL
When I open data/invalid_resource.html

View File

@ -26,7 +26,6 @@ import typing
from PyQt5.QtCore import QUrl
import pytest
import adblock
from qutebrowser.api.interceptor import ResourceType
from qutebrowser.components import braveadblock
@ -148,7 +147,7 @@ def blocklist_invalid_utf8(tmpdir):
@pytest.fixture
def easylist_easyprivacy_both(tmpdir):
"""Put easyprivacy and easylist blocklists into a tempdir
"""Put easyprivacy and easylist blocklists into a tempdir.
Copy the easyprivacy and easylist blocklists into a temporary directory,
then return both a list containing `file://` urls, and the residing dir.
@ -177,7 +176,7 @@ def empty_dir(tmpdir):
@pytest.fixture
def easylist_easyprivacy(easylist_easyprivacy_both):
"""The first return value of `easylist_easyprivacy_both`"""
"""The first return value of `easylist_easyprivacy_both`."""
return easylist_easyprivacy_both[0]