From bdc41db6013ab2da181c5eb9d62a0d7a0cf81b99 Mon Sep 17 00:00:00 2001 From: zaowen Date: Tue, 4 Sep 2018 20:53:26 -0500 Subject: [PATCH] Adblock import handles local directories and paths Fixes #464 * Handles local Directories * Handles local paths without file:// --- qutebrowser/browser/adblock.py | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/qutebrowser/browser/adblock.py b/qutebrowser/browser/adblock.py index affd80eaf..fc7de9e37 100644 --- a/qutebrowser/browser/adblock.py +++ b/qutebrowser/browser/adblock.py @@ -174,15 +174,14 @@ class HostBlocker: for url in config.val.content.host_blocking.lists: if url.scheme() == 'file': filename = url.toLocalFile() - try: - fileobj = open(filename, 'rb') - except OSError as e: - message.error("adblock: Error while reading {}: {}".format( - filename, e.strerror)) - continue - download = FakeDownload(fileobj) - self._in_progress.append(download) - self.on_download_finished(download) + if os.path.isdir(filename): + for filenames in os.scandir(filename): + if not filenames.name.startswith('.')\ + and filenames.is_file(): + print( filenames.name ) + self._import_local(filenames.path) + else: + self._import_local(filename) else: fobj = io.BytesIO() fobj.name = 'adblock: ' + url.host() @@ -193,6 +192,22 @@ class HostBlocker: download.finished.connect( functools.partial(self.on_download_finished, download)) + def _import_local(self, filename): + """Adds the contents of a file to the blocklist. + + Args: + filename: path to a local file to import. + """ + try: + fileobj = open(filename, 'rb') + except OSError as e: + message.error("adblock: Error while reading {}: {}".format( + filename, e.strerror)) + return + download = FakeDownload(fileobj) + self._in_progress.append(download) + self.on_download_finished(download) + def _parse_line(self, line): """Parse a line from a host file.