Make some stuff in adblock.py private

This commit is contained in:
Florian Bruhin 2018-09-18 20:27:58 +02:00
parent 1a0c6964e3
commit 93ec3e3fad

View File

@ -31,7 +31,7 @@ from qutebrowser.utils import objreg, standarddir, log, message
from qutebrowser.commands import cmdutils from qutebrowser.commands import cmdutils
def guess_zip_filename(zf): def _guess_zip_filename(zf):
"""Guess which file to use inside a zip file. """Guess which file to use inside a zip file.
Args: Args:
@ -53,14 +53,14 @@ def get_fileobj(byte_io):
if zipfile.is_zipfile(byte_io): if zipfile.is_zipfile(byte_io):
byte_io.seek(0) # rewind what zipfile.is_zipfile did byte_io.seek(0) # rewind what zipfile.is_zipfile did
zf = zipfile.ZipFile(byte_io) zf = zipfile.ZipFile(byte_io)
filename = guess_zip_filename(zf) filename = _guess_zip_filename(zf)
byte_io = zf.open(filename, mode='r') byte_io = zf.open(filename, mode='r')
else: else:
byte_io.seek(0) # rewind what zipfile.is_zipfile did byte_io.seek(0) # rewind what zipfile.is_zipfile did
return byte_io return byte_io
def is_whitelisted_url(url): def _is_whitelisted_url(url):
"""Check if the given URL is on the adblock whitelist. """Check if the given URL is on the adblock whitelist.
Args: Args:
@ -72,7 +72,7 @@ def is_whitelisted_url(url):
return False return False
class FakeDownload: class _FakeDownload:
"""A download stub to use on_download_finished with local files.""" """A download stub to use on_download_finished with local files."""
@ -117,7 +117,7 @@ class HostBlocker:
host = url.host() host = url.host()
return ((host in self._blocked_hosts or return ((host in self._blocked_hosts or
host in self._config_blocked_hosts) and host in self._config_blocked_hosts) and
not is_whitelisted_url(url)) not _is_whitelisted_url(url))
def _read_hosts_file(self, filename, target): def _read_hosts_file(self, filename, target):
"""Read hosts from the given filename. """Read hosts from the given filename.
@ -179,9 +179,9 @@ class HostBlocker:
message.error("adblock: Error while reading {}: {}".format( message.error("adblock: Error while reading {}: {}".format(
filename, e.strerror)) filename, e.strerror))
continue continue
download = FakeDownload(fileobj) download = _FakeDownload(fileobj)
self._in_progress.append(download) self._in_progress.append(download)
self.on_download_finished(download) self._on_download_finished(download)
else: else:
fobj = io.BytesIO() fobj = io.BytesIO()
fobj.name = 'adblock: ' + url.host() fobj.name = 'adblock: ' + url.host()
@ -190,7 +190,7 @@ class HostBlocker:
auto_remove=True) auto_remove=True)
self._in_progress.append(download) self._in_progress.append(download)
download.finished.connect( download.finished.connect(
functools.partial(self.on_download_finished, download)) functools.partial(self._on_download_finished, download))
def _parse_line(self, line): def _parse_line(self, line):
"""Parse a line from a host file. """Parse a line from a host file.
@ -270,7 +270,7 @@ class HostBlocker:
message.error("adblock: {} read errors for {}".format( message.error("adblock: {} read errors for {}".format(
error_count, byte_io.name)) error_count, byte_io.name))
def on_lists_downloaded(self): def _on_lists_downloaded(self):
"""Install block lists after files have been downloaded.""" """Install block lists after files have been downloaded."""
with open(self._local_hosts_file, 'w', encoding='utf-8') as f: with open(self._local_hosts_file, 'w', encoding='utf-8') as f:
for host in sorted(self._blocked_hosts): for host in sorted(self._blocked_hosts):
@ -289,7 +289,7 @@ class HostBlocker:
except OSError as e: except OSError as e:
log.misc.exception("Failed to delete hosts file: {}".format(e)) log.misc.exception("Failed to delete hosts file: {}".format(e))
def on_download_finished(self, download): def _on_download_finished(self, download):
"""Check if all downloads are finished and if so, trigger reading. """Check if all downloads are finished and if so, trigger reading.
Arguments: Arguments:
@ -304,6 +304,6 @@ class HostBlocker:
download.fileobj.close() download.fileobj.close()
if not self._in_progress: if not self._in_progress:
try: try:
self.on_lists_downloaded() self._on_lists_downloaded()
except OSError: except OSError:
log.misc.exception("Failed to write host block list!") log.misc.exception("Failed to write host block list!")