diff options
-rw-r--r-- | qutebrowser/components/adblock.py | 6 | ||||
-rw-r--r-- | qutebrowser/components/braveadblock.py | 57 | ||||
-rw-r--r-- | requirements.txt | 2 |
3 files changed, 46 insertions, 19 deletions
diff --git a/qutebrowser/components/adblock.py b/qutebrowser/components/adblock.py index d5f021563..4afd6190d 100644 --- a/qutebrowser/components/adblock.py +++ b/qutebrowser/components/adblock.py @@ -232,7 +232,7 @@ class HostBlocker: f = get_fileobj(byte_io) except (OSError, zipfile.BadZipFile, zipfile.LargeZipFile, LookupError) as e: message.error( - "adblock: Error while reading {}: {} - {}".format( + "hostblock: Error while reading {}: {} - {}".format( byte_io.name, e.__class__.__name__, e ) ) @@ -249,7 +249,7 @@ class HostBlocker: logger.debug("{}: read {} lines".format(byte_io.name, line_count)) if error_count > 0: message.error( - "adblock: {} read errors for {}".format(error_count, byte_io.name) + "hostblock: {} read errors for {}".format(error_count, byte_io.name) ) def _on_lists_downloaded(self) -> None: @@ -258,7 +258,7 @@ class HostBlocker: for host in sorted(self._blocked_hosts): f.write(host + "\n") message.info( - "adblock: Read {} hosts from {} sources.".format( + "hostblock: Read {} hosts from {} sources.".format( len(self._blocked_hosts), self._done_count ) ) diff --git a/qutebrowser/components/braveadblock.py b/qutebrowser/components/braveadblock.py index 886a9a4c2..61c7b78de 100644 --- a/qutebrowser/components/braveadblock.py +++ b/qutebrowser/components/braveadblock.py @@ -95,19 +95,29 @@ class BraveAdBlocker: """Manage blocked hosts based from /etc/hosts-like files. Attributes: - _in_progress: The DownloadItems which are currently downloading. - _done_count: How many files have been read successfully. _has_basedir: Whether a custom --basedir is set. _cache_path: The path of the adblock engine cache file + _in_progress: The DownloadItems which are currently downloading. + _done_count: How many files have been read successfully. + _finished_registering_downloads: + Used to make sure that if all the downloads finish really quickly, + before all of the block-lists have been added to the download + queue, we don't call `_on_lists_downloaded`. + _wip_filter_set: + If we're in midst of updating the block lists, this attribute + contains a "work-in-progress" filter set that will later be used to + create a new instance of `_engine`. Otherwise it's `None`. _engine: Brave ad-blocking engine. """ def __init__(self, *, data_dir: pathlib.Path, has_basedir: bool = False) -> None: self._has_basedir = has_basedir + self._cache_path = data_dir / "adblock-cache.dat" self._in_progress = [] # type: typing.List[downloads.TempDownload] self._done_count = 0 - self._cache_path = data_dir / "adblock-cache.dat" - self._engine = adblock.Engine() + self._finished_registering_downloads = False + self._wip_filter_set: Optional[adblock.FilterSet] = None + self._engine = adblock.Engine(adblock.FilterSet()) def _is_blocked( self, @@ -172,22 +182,38 @@ class BraveAdBlocker: and not self._has_basedir and config.val.content.blocking.adblock.enabled ): - message.info("Run :brave-adblock-update to get adblock lists.") + message.info("Run :adblock-update to get adblock lists.") def adblock_update(self) -> None: """Update the adblock block lists.""" self._done_count = 0 - self._engine = adblock.Engine() + self._wip_filter_set = adblock.FilterSet() logger.info("Downloading adblock filter lists...") - for url in config.val.content.blocking.adblock.lists: - blockutils.download_blocklist_url( - url, self._on_download_finished, self._in_progress - ) + + blocklists = config.val.content.blocking.adblock.lists + if not blocklists: + # Blocklists are None or length zero + self._on_lists_downloaded() + else: + self._finished_registering_downloads = False + for i, url in enumerate(blocklists): + if i == len(blocklists) - 1: + self._finished_registering_downloads = True + blockutils.download_blocklist_url( + url, self._on_download_finished, self._in_progress + ) def _on_lists_downloaded(self) -> None: """Install block lists after files have been downloaded.""" + assert self._wip_filter_set is not None + self._engine = adblock.Engine(self._wip_filter_set) + self._wip_filter_set = None self._engine.serialize_to_file(str(self._cache_path)) - logger.info("Block lists have been successfully imported") + logger.info( + "adblock: Filters successfully read from {} sources".format( + self._done_count + ) + ) def update_files(self) -> None: """Update files when the config changed.""" @@ -197,7 +223,7 @@ class BraveAdBlocker: except FileNotFoundError: pass except OSError as e: - logger.exception("Failed to adblock cache file: {}".format(e)) + logger.exception("Failed to remove adblock cache file: {}".format(e)) def _on_download_finished(self, download: downloads.TempDownload) -> None: """Check if all downloads are finished and if so, trigger reading. @@ -210,16 +236,17 @@ class BraveAdBlocker: self._done_count += 1 assert not isinstance(download.fileobj, downloads.UnsupportedAttribute) assert download.fileobj is not None + assert self._wip_filter_set is not None try: download.fileobj.seek(0) text = io.TextIOWrapper(download.fileobj, encoding="utf-8") - self._engine.add_filter_list(text.read()) + self._wip_filter_set.add_filter_list(text.read()) text.close() except UnicodeDecodeError: message.info("Block list is not valid utf-8") finally: download.fileobj.close() - if not self._in_progress: + if len(self._in_progress) == 0 and self._finished_registering_downloads: try: self._on_lists_downloaded() except OSError: @@ -245,7 +272,7 @@ def init(context: apitypes.InitContext) -> None: return ad_blocker = BraveAdBlocker( - data_dir=context.data_dir, has_basedir=context.args.basedir is not None, + data_dir=context.data_dir, has_basedir=context.args.basedir is not None ) ad_blocker.read_cache() interceptor.register(ad_blocker.filter_request) diff --git a/requirements.txt b/requirements.txt index 83ba4f8f2..d00782fb6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ # This file is automatically generated by scripts/dev/recompile_requirements.py -adblock==0.2.3 +adblock==0.3.0 attrs==19.3.0 colorama==0.4.3 cssutils==1.0.2 |