We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Disable the automatic "Run :adblock-update" info message on startup
Every time I start qutebrowser I got a message:

> Run :adblock-update to get adblock lists

I've set `host-blocking-enabled = false` in the config, because I have my own hosts file in /etc + I have privoxy.

I couldn't find a way to disable this message. I searched in the config, in the man page, in the help, here through the issues... Am I missing something? :confused: 

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `qutebrowser/browser/adblock.py`
Content:
```
1 # vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2 
3 # Copyright 2014-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
4 #
5 # This file is part of qutebrowser.
6 #
7 # qutebrowser is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # qutebrowser is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with qutebrowser.  If not, see <http://www.gnu.org/licenses/>.
19 
20 """Functions related to ad blocking."""
21 
22 import io
23 import os.path
24 import functools
25 import posixpath
26 import zipfile
27 import fnmatch
28 
29 from qutebrowser.browser import downloads
30 from qutebrowser.config import config
31 from qutebrowser.utils import objreg, standarddir, log, message
32 from qutebrowser.commands import cmdutils
33 
34 
35 def guess_zip_filename(zf):
36     """Guess which file to use inside a zip file.
37 
38     Args:
39         zf: A ZipFile instance.
40     """
41     files = zf.namelist()
42     if len(files) == 1:
43         return files[0]
44     else:
45         for e in files:
46             if posixpath.splitext(e)[0].lower() == 'hosts':
47                 return e
48     raise FileNotFoundError("No hosts file found in zip")
49 
50 
51 def get_fileobj(byte_io):
52     """Get a usable file object to read the hosts file from."""
53     byte_io.seek(0)  # rewind downloaded file
54     if zipfile.is_zipfile(byte_io):
55         byte_io.seek(0)  # rewind what zipfile.is_zipfile did
56         zf = zipfile.ZipFile(byte_io)
57         filename = guess_zip_filename(zf)
58         byte_io = zf.open(filename, mode='r')
59     else:
60         byte_io.seek(0)  # rewind what zipfile.is_zipfile did
61     return io.TextIOWrapper(byte_io, encoding='utf-8')
62 
63 
64 def is_whitelisted_host(host):
65     """Check if the given host is on the adblock whitelist.
66 
67     Args:
68         host: The host of the request as string.
69     """
70     whitelist = config.get('content', 'host-blocking-whitelist')
71     if whitelist is None:
72         return False
73 
74     for pattern in whitelist:
75         if fnmatch.fnmatch(host, pattern.lower()):
76             return True
77     return False
78 
79 
80 class FakeDownload:
81 
82     """A download stub to use on_download_finished with local files."""
83 
84     def __init__(self, fileobj):
85         self.basename = os.path.basename(fileobj.name)
86         self.fileobj = fileobj
87         self.successful = True
88 
89 
90 class HostBlocker:
91 
92     """Manage blocked hosts based from /etc/hosts-like files.
93 
94     Attributes:
95         _blocked_hosts: A set of blocked hosts.
96         _config_blocked_hosts: A set of blocked hosts from ~/.config.
97         _in_progress: The DownloadItems which are currently downloading.
98         _done_count: How many files have been read successfully.
99         _local_hosts_file: The path to the blocked-hosts file.
100         _config_hosts_file: The path to a blocked-hosts in ~/.config
101 
102     Class attributes:
103         WHITELISTED: Hosts which never should be blocked.
104     """
105 
106     WHITELISTED = ('localhost', 'localhost.localdomain', 'broadcasthost',
107                    'local')
108 
109     def __init__(self):
110         self._blocked_hosts = set()
111         self._config_blocked_hosts = set()
112         self._in_progress = []
113         self._done_count = 0
114 
115         data_dir = standarddir.data()
116         self._local_hosts_file = os.path.join(data_dir, 'blocked-hosts')
117         self.on_config_changed()
118 
119         config_dir = standarddir.config()
120         self._config_hosts_file = os.path.join(config_dir, 'blocked-hosts')
121 
122         objreg.get('config').changed.connect(self.on_config_changed)
123 
124     def is_blocked(self, url):
125         """Check if the given URL (as QUrl) is blocked."""
126         if not config.get('content', 'host-blocking-enabled'):
127             return False
128         host = url.host()
129         return ((host in self._blocked_hosts or
130                  host in self._config_blocked_hosts) and
131                 not is_whitelisted_host(host))
132 
133     def _read_hosts_file(self, filename, target):
134         """Read hosts from the given filename.
135 
136         Args:
137             filename: The file to read.
138             target: The set to store the hosts in.
139 
140         Return:
141             True if a read was attempted, False otherwise
142         """
143         if not os.path.exists(filename):
144             return False
145 
146         try:
147             with open(filename, 'r', encoding='utf-8') as f:
148                 for line in f:
149                     target.add(line.strip())
150         except OSError:
151             log.misc.exception("Failed to read host blocklist!")
152 
153         return True
154 
155     def read_hosts(self):
156         """Read hosts from the existing blocked-hosts file."""
157         self._blocked_hosts = set()
158 
159         self._read_hosts_file(self._config_hosts_file,
160                               self._config_blocked_hosts)
161 
162         found = self._read_hosts_file(self._local_hosts_file,
163                                       self._blocked_hosts)
164 
165         if not found:
166             args = objreg.get('args')
167             if (config.get('content', 'host-block-lists') is not None and
168                     args.basedir is None):
169                 message.info("Run :adblock-update to get adblock lists.")
170 
171     @cmdutils.register(instance='host-blocker')
172     def adblock_update(self):
173         """Update the adblock block lists.
174 
175         This updates `~/.local/share/qutebrowser/blocked-hosts` with downloaded
176         host lists and re-reads `~/.config/qutebrowser/blocked-hosts`.
177         """
178         self._read_hosts_file(self._config_hosts_file,
179                               self._config_blocked_hosts)
180         self._blocked_hosts = set()
181         self._done_count = 0
182         urls = config.get('content', 'host-block-lists')
183         download_manager = objreg.get('qtnetwork-download-manager',
184                                       scope='window', window='last-focused')
185         if urls is None:
186             return
187         for url in urls:
188             if url.scheme() == 'file':
189                 try:
190                     fileobj = open(url.path(), 'rb')
191                 except OSError as e:
192                     message.error("adblock: Error while reading {}: {}".format(
193                         url.path(), e.strerror))
194                     continue
195                 download = FakeDownload(fileobj)
196                 self._in_progress.append(download)
197                 self.on_download_finished(download)
198             else:
199                 fobj = io.BytesIO()
200                 fobj.name = 'adblock: ' + url.host()
201                 target = downloads.FileObjDownloadTarget(fobj)
202                 download = download_manager.get(url, target=target,
203                                                 auto_remove=True)
204                 self._in_progress.append(download)
205                 download.finished.connect(
206                     functools.partial(self.on_download_finished, download))
207 
208     def _merge_file(self, byte_io):
209         """Read and merge host files.
210 
211         Args:
212             byte_io: The BytesIO object of the completed download.
213 
214         Return:
215             A set of the merged hosts.
216         """
217         error_count = 0
218         line_count = 0
219         try:
220             f = get_fileobj(byte_io)
221         except (OSError, UnicodeDecodeError, zipfile.BadZipFile,
222                 zipfile.LargeZipFile, LookupError) as e:
223             message.error("adblock: Error while reading {}: {} - {}".format(
224                 byte_io.name, e.__class__.__name__, e))
225             return
226         for line in f:
227             line_count += 1
228             # Remove comments
229             try:
230                 hash_idx = line.index('#')
231                 line = line[:hash_idx]
232             except ValueError:
233                 pass
234             line = line.strip()
235             # Skip empty lines
236             if not line:
237                 continue
238             parts = line.split()
239             if len(parts) == 1:
240                 # "one host per line" format
241                 host = parts[0]
242             elif len(parts) == 2:
243                 # /etc/hosts format
244                 host = parts[1]
245             else:
246                 error_count += 1
247                 continue
248             if host not in self.WHITELISTED:
249                 self._blocked_hosts.add(host)
250         log.misc.debug("{}: read {} lines".format(byte_io.name, line_count))
251         if error_count > 0:
252             message.error("adblock: {} read errors for {}".format(
253                 error_count, byte_io.name))
254 
255     def on_lists_downloaded(self):
256         """Install block lists after files have been downloaded."""
257         with open(self._local_hosts_file, 'w', encoding='utf-8') as f:
258             for host in sorted(self._blocked_hosts):
259                 f.write(host + '\n')
260             message.info("adblock: Read {} hosts from {} sources.".format(
261                 len(self._blocked_hosts), self._done_count))
262 
263     @config.change_filter('content', 'host-block-lists')
264     def on_config_changed(self):
265         """Update files when the config changed."""
266         urls = config.get('content', 'host-block-lists')
267         if urls is None:
268             try:
269                 os.remove(self._local_hosts_file)
270             except FileNotFoundError:
271                 pass
272             except OSError as e:
273                 log.misc.exception("Failed to delete hosts file: {}".format(e))
274 
275     def on_download_finished(self, download):
276         """Check if all downloads are finished and if so, trigger reading.
277 
278         Arguments:
279             download: The finished DownloadItem.
280         """
281         self._in_progress.remove(download)
282         if download.successful:
283             self._done_count += 1
284             try:
285                 self._merge_file(download.fileobj)
286             finally:
287                 download.fileobj.close()
288         if not self._in_progress:
289             try:
290                 self.on_lists_downloaded()
291             except OSError:
292                 log.misc.exception("Failed to write host block list!")
293
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/qutebrowser/browser/adblock.py b/qutebrowser/browser/adblock.py
--- a/qutebrowser/browser/adblock.py
+++ b/qutebrowser/browser/adblock.py
@@ -165,7 +165,8 @@
         if not found:
             args = objreg.get('args')
             if (config.get('content', 'host-block-lists') is not None and
-                    args.basedir is None):
+                    args.basedir is None and
+                    config.get('content', 'host-blocking-enabled')):
                 message.info("Run :adblock-update to get adblock lists.")
 
     @cmdutils.register(instance='host-blocker')

{"golden_diff": "diff --git a/qutebrowser/browser/adblock.py b/qutebrowser/browser/adblock.py\n--- a/qutebrowser/browser/adblock.py\n+++ b/qutebrowser/browser/adblock.py\n@@ -165,7 +165,8 @@\n         if not found:\n             args = objreg.get('args')\n             if (config.get('content', 'host-block-lists') is not None and\n-                    args.basedir is None):\n+                    args.basedir is None and\n+                    config.get('content', 'host-blocking-enabled')):\n                 message.info(\"Run :adblock-update to get adblock lists.\")\n \n     @cmdutils.register(instance='host-blocker')\n", "issue": "Disable the automatic \"Run :adblock-update\" info message on startup\nEvery time I start qutebrowser I got a message:\r\n\r\n> Run :adblock-update to get adblock lists\r\n\r\nI've set `host-blocking-enabled = false` in the config, because I have my own hosts file in /etc + I have privoxy.\r\n\r\nI couldn't find a way to disable this message. I searched in the config, in the man page, in the help, here through the issues... Am I missing something? :confused: \n", "before_files": [{"content": "# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:\n\n# Copyright 2014-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>\n#\n# This file is part of qutebrowser.\n#\n# qutebrowser is free software: you can redistribute it and/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation, either version 3 of the License, or\n# (at your option) any later version.\n#\n# qutebrowser is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with qutebrowser.  If not, see <http://www.gnu.org/licenses/>.\n\n\"\"\"Functions related to ad blocking.\"\"\"\n\nimport io\nimport os.path\nimport functools\nimport posixpath\nimport zipfile\nimport fnmatch\n\nfrom qutebrowser.browser import downloads\nfrom qutebrowser.config import config\nfrom qutebrowser.utils import objreg, standarddir, log, message\nfrom qutebrowser.commands import cmdutils\n\n\ndef guess_zip_filename(zf):\n    \"\"\"Guess which file to use inside a zip file.\n\n    Args:\n        zf: A ZipFile instance.\n    \"\"\"\n    files = zf.namelist()\n    if len(files) == 1:\n        return files[0]\n    else:\n        for e in files:\n            if posixpath.splitext(e)[0].lower() == 'hosts':\n                return e\n    raise FileNotFoundError(\"No hosts file found in zip\")\n\n\ndef get_fileobj(byte_io):\n    \"\"\"Get a usable file object to read the hosts file from.\"\"\"\n    byte_io.seek(0)  # rewind downloaded file\n    if zipfile.is_zipfile(byte_io):\n        byte_io.seek(0)  # rewind what zipfile.is_zipfile did\n        zf = zipfile.ZipFile(byte_io)\n        filename = guess_zip_filename(zf)\n        byte_io = zf.open(filename, mode='r')\n    else:\n        byte_io.seek(0)  # rewind what zipfile.is_zipfile did\n    return io.TextIOWrapper(byte_io, encoding='utf-8')\n\n\ndef is_whitelisted_host(host):\n    \"\"\"Check if the given host is on the adblock whitelist.\n\n    Args:\n        host: The host of the request as string.\n    \"\"\"\n    whitelist = config.get('content', 'host-blocking-whitelist')\n    if whitelist is None:\n        return False\n\n    for pattern in whitelist:\n        if fnmatch.fnmatch(host, pattern.lower()):\n            return True\n    return False\n\n\nclass FakeDownload:\n\n    \"\"\"A download stub to use on_download_finished with local files.\"\"\"\n\n    def __init__(self, fileobj):\n        self.basename = os.path.basename(fileobj.name)\n        self.fileobj = fileobj\n        self.successful = True\n\n\nclass HostBlocker:\n\n    \"\"\"Manage blocked hosts based from /etc/hosts-like files.\n\n    Attributes:\n        _blocked_hosts: A set of blocked hosts.\n        _config_blocked_hosts: A set of blocked hosts from ~/.config.\n        _in_progress: The DownloadItems which are currently downloading.\n        _done_count: How many files have been read successfully.\n        _local_hosts_file: The path to the blocked-hosts file.\n        _config_hosts_file: The path to a blocked-hosts in ~/.config\n\n    Class attributes:\n        WHITELISTED: Hosts which never should be blocked.\n    \"\"\"\n\n    WHITELISTED = ('localhost', 'localhost.localdomain', 'broadcasthost',\n                   'local')\n\n    def __init__(self):\n        self._blocked_hosts = set()\n        self._config_blocked_hosts = set()\n        self._in_progress = []\n        self._done_count = 0\n\n        data_dir = standarddir.data()\n        self._local_hosts_file = os.path.join(data_dir, 'blocked-hosts')\n        self.on_config_changed()\n\n        config_dir = standarddir.config()\n        self._config_hosts_file = os.path.join(config_dir, 'blocked-hosts')\n\n        objreg.get('config').changed.connect(self.on_config_changed)\n\n    def is_blocked(self, url):\n        \"\"\"Check if the given URL (as QUrl) is blocked.\"\"\"\n        if not config.get('content', 'host-blocking-enabled'):\n            return False\n        host = url.host()\n        return ((host in self._blocked_hosts or\n                 host in self._config_blocked_hosts) and\n                not is_whitelisted_host(host))\n\n    def _read_hosts_file(self, filename, target):\n        \"\"\"Read hosts from the given filename.\n\n        Args:\n            filename: The file to read.\n            target: The set to store the hosts in.\n\n        Return:\n            True if a read was attempted, False otherwise\n        \"\"\"\n        if not os.path.exists(filename):\n            return False\n\n        try:\n            with open(filename, 'r', encoding='utf-8') as f:\n                for line in f:\n                    target.add(line.strip())\n        except OSError:\n            log.misc.exception(\"Failed to read host blocklist!\")\n\n        return True\n\n    def read_hosts(self):\n        \"\"\"Read hosts from the existing blocked-hosts file.\"\"\"\n        self._blocked_hosts = set()\n\n        self._read_hosts_file(self._config_hosts_file,\n                              self._config_blocked_hosts)\n\n        found = self._read_hosts_file(self._local_hosts_file,\n                                      self._blocked_hosts)\n\n        if not found:\n            args = objreg.get('args')\n            if (config.get('content', 'host-block-lists') is not None and\n                    args.basedir is None):\n                message.info(\"Run :adblock-update to get adblock lists.\")\n\n    @cmdutils.register(instance='host-blocker')\n    def adblock_update(self):\n        \"\"\"Update the adblock block lists.\n\n        This updates `~/.local/share/qutebrowser/blocked-hosts` with downloaded\n        host lists and re-reads `~/.config/qutebrowser/blocked-hosts`.\n        \"\"\"\n        self._read_hosts_file(self._config_hosts_file,\n                              self._config_blocked_hosts)\n        self._blocked_hosts = set()\n        self._done_count = 0\n        urls = config.get('content', 'host-block-lists')\n        download_manager = objreg.get('qtnetwork-download-manager',\n                                      scope='window', window='last-focused')\n        if urls is None:\n            return\n        for url in urls:\n            if url.scheme() == 'file':\n                try:\n                    fileobj = open(url.path(), 'rb')\n                except OSError as e:\n                    message.error(\"adblock: Error while reading {}: {}\".format(\n                        url.path(), e.strerror))\n                    continue\n                download = FakeDownload(fileobj)\n                self._in_progress.append(download)\n                self.on_download_finished(download)\n            else:\n                fobj = io.BytesIO()\n                fobj.name = 'adblock: ' + url.host()\n                target = downloads.FileObjDownloadTarget(fobj)\n                download = download_manager.get(url, target=target,\n                                                auto_remove=True)\n                self._in_progress.append(download)\n                download.finished.connect(\n                    functools.partial(self.on_download_finished, download))\n\n    def _merge_file(self, byte_io):\n        \"\"\"Read and merge host files.\n\n        Args:\n            byte_io: The BytesIO object of the completed download.\n\n        Return:\n            A set of the merged hosts.\n        \"\"\"\n        error_count = 0\n        line_count = 0\n        try:\n            f = get_fileobj(byte_io)\n        except (OSError, UnicodeDecodeError, zipfile.BadZipFile,\n                zipfile.LargeZipFile, LookupError) as e:\n            message.error(\"adblock: Error while reading {}: {} - {}\".format(\n                byte_io.name, e.__class__.__name__, e))\n            return\n        for line in f:\n            line_count += 1\n            # Remove comments\n            try:\n                hash_idx = line.index('#')\n                line = line[:hash_idx]\n            except ValueError:\n                pass\n            line = line.strip()\n            # Skip empty lines\n            if not line:\n                continue\n            parts = line.split()\n            if len(parts) == 1:\n                # \"one host per line\" format\n                host = parts[0]\n            elif len(parts) == 2:\n                # /etc/hosts format\n                host = parts[1]\n            else:\n                error_count += 1\n                continue\n            if host not in self.WHITELISTED:\n                self._blocked_hosts.add(host)\n        log.misc.debug(\"{}: read {} lines\".format(byte_io.name, line_count))\n        if error_count > 0:\n            message.error(\"adblock: {} read errors for {}\".format(\n                error_count, byte_io.name))\n\n    def on_lists_downloaded(self):\n        \"\"\"Install block lists after files have been downloaded.\"\"\"\n        with open(self._local_hosts_file, 'w', encoding='utf-8') as f:\n            for host in sorted(self._blocked_hosts):\n                f.write(host + '\\n')\n            message.info(\"adblock: Read {} hosts from {} sources.\".format(\n                len(self._blocked_hosts), self._done_count))\n\n    @config.change_filter('content', 'host-block-lists')\n    def on_config_changed(self):\n        \"\"\"Update files when the config changed.\"\"\"\n        urls = config.get('content', 'host-block-lists')\n        if urls is None:\n            try:\n                os.remove(self._local_hosts_file)\n            except FileNotFoundError:\n                pass\n            except OSError as e:\n                log.misc.exception(\"Failed to delete hosts file: {}\".format(e))\n\n    def on_download_finished(self, download):\n        \"\"\"Check if all downloads are finished and if so, trigger reading.\n\n        Arguments:\n            download: The finished DownloadItem.\n        \"\"\"\n        self._in_progress.remove(download)\n        if download.successful:\n            self._done_count += 1\n            try:\n                self._merge_file(download.fileobj)\n            finally:\n                download.fileobj.close()\n        if not self._in_progress:\n            try:\n                self.on_lists_downloaded()\n            except OSError:\n                log.misc.exception(\"Failed to write host block list!\")\n", "path": "qutebrowser/browser/adblock.py"}], "after_files": [{"content": "# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:\n\n# Copyright 2014-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>\n#\n# This file is part of qutebrowser.\n#\n# qutebrowser is free software: you can redistribute it and/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation, either version 3 of the License, or\n# (at your option) any later version.\n#\n# qutebrowser is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n#\n# You should have received a copy of the GNU General Public License\n# along with qutebrowser.  If not, see <http://www.gnu.org/licenses/>.\n\n\"\"\"Functions related to ad blocking.\"\"\"\n\nimport io\nimport os.path\nimport functools\nimport posixpath\nimport zipfile\nimport fnmatch\n\nfrom qutebrowser.browser import downloads\nfrom qutebrowser.config import config\nfrom qutebrowser.utils import objreg, standarddir, log, message\nfrom qutebrowser.commands import cmdutils\n\n\ndef guess_zip_filename(zf):\n    \"\"\"Guess which file to use inside a zip file.\n\n    Args:\n        zf: A ZipFile instance.\n    \"\"\"\n    files = zf.namelist()\n    if len(files) == 1:\n        return files[0]\n    else:\n        for e in files:\n            if posixpath.splitext(e)[0].lower() == 'hosts':\n                return e\n    raise FileNotFoundError(\"No hosts file found in zip\")\n\n\ndef get_fileobj(byte_io):\n    \"\"\"Get a usable file object to read the hosts file from.\"\"\"\n    byte_io.seek(0)  # rewind downloaded file\n    if zipfile.is_zipfile(byte_io):\n        byte_io.seek(0)  # rewind what zipfile.is_zipfile did\n        zf = zipfile.ZipFile(byte_io)\n        filename = guess_zip_filename(zf)\n        byte_io = zf.open(filename, mode='r')\n    else:\n        byte_io.seek(0)  # rewind what zipfile.is_zipfile did\n    return io.TextIOWrapper(byte_io, encoding='utf-8')\n\n\ndef is_whitelisted_host(host):\n    \"\"\"Check if the given host is on the adblock whitelist.\n\n    Args:\n        host: The host of the request as string.\n    \"\"\"\n    whitelist = config.get('content', 'host-blocking-whitelist')\n    if whitelist is None:\n        return False\n\n    for pattern in whitelist:\n        if fnmatch.fnmatch(host, pattern.lower()):\n            return True\n    return False\n\n\nclass FakeDownload:\n\n    \"\"\"A download stub to use on_download_finished with local files.\"\"\"\n\n    def __init__(self, fileobj):\n        self.basename = os.path.basename(fileobj.name)\n        self.fileobj = fileobj\n        self.successful = True\n\n\nclass HostBlocker:\n\n    \"\"\"Manage blocked hosts based from /etc/hosts-like files.\n\n    Attributes:\n        _blocked_hosts: A set of blocked hosts.\n        _config_blocked_hosts: A set of blocked hosts from ~/.config.\n        _in_progress: The DownloadItems which are currently downloading.\n        _done_count: How many files have been read successfully.\n        _local_hosts_file: The path to the blocked-hosts file.\n        _config_hosts_file: The path to a blocked-hosts in ~/.config\n\n    Class attributes:\n        WHITELISTED: Hosts which never should be blocked.\n    \"\"\"\n\n    WHITELISTED = ('localhost', 'localhost.localdomain', 'broadcasthost',\n                   'local')\n\n    def __init__(self):\n        self._blocked_hosts = set()\n        self._config_blocked_hosts = set()\n        self._in_progress = []\n        self._done_count = 0\n\n        data_dir = standarddir.data()\n        self._local_hosts_file = os.path.join(data_dir, 'blocked-hosts')\n        self.on_config_changed()\n\n        config_dir = standarddir.config()\n        self._config_hosts_file = os.path.join(config_dir, 'blocked-hosts')\n\n        objreg.get('config').changed.connect(self.on_config_changed)\n\n    def is_blocked(self, url):\n        \"\"\"Check if the given URL (as QUrl) is blocked.\"\"\"\n        if not config.get('content', 'host-blocking-enabled'):\n            return False\n        host = url.host()\n        return ((host in self._blocked_hosts or\n                 host in self._config_blocked_hosts) and\n                not is_whitelisted_host(host))\n\n    def _read_hosts_file(self, filename, target):\n        \"\"\"Read hosts from the given filename.\n\n        Args:\n            filename: The file to read.\n            target: The set to store the hosts in.\n\n        Return:\n            True if a read was attempted, False otherwise\n        \"\"\"\n        if not os.path.exists(filename):\n            return False\n\n        try:\n            with open(filename, 'r', encoding='utf-8') as f:\n                for line in f:\n                    target.add(line.strip())\n        except OSError:\n            log.misc.exception(\"Failed to read host blocklist!\")\n\n        return True\n\n    def read_hosts(self):\n        \"\"\"Read hosts from the existing blocked-hosts file.\"\"\"\n        self._blocked_hosts = set()\n\n        self._read_hosts_file(self._config_hosts_file,\n                              self._config_blocked_hosts)\n\n        found = self._read_hosts_file(self._local_hosts_file,\n                                      self._blocked_hosts)\n\n        if not found:\n            args = objreg.get('args')\n            if (config.get('content', 'host-block-lists') is not None and\n                    args.basedir is None and\n                    config.get('content', 'host-blocking-enabled')):\n                message.info(\"Run :adblock-update to get adblock lists.\")\n\n    @cmdutils.register(instance='host-blocker')\n    def adblock_update(self):\n        \"\"\"Update the adblock block lists.\n\n        This updates `~/.local/share/qutebrowser/blocked-hosts` with downloaded\n        host lists and re-reads `~/.config/qutebrowser/blocked-hosts`.\n        \"\"\"\n        self._read_hosts_file(self._config_hosts_file,\n                              self._config_blocked_hosts)\n        self._blocked_hosts = set()\n        self._done_count = 0\n        urls = config.get('content', 'host-block-lists')\n        download_manager = objreg.get('qtnetwork-download-manager',\n                                      scope='window', window='last-focused')\n        if urls is None:\n            return\n        for url in urls:\n            if url.scheme() == 'file':\n                try:\n                    fileobj = open(url.path(), 'rb')\n                except OSError as e:\n                    message.error(\"adblock: Error while reading {}: {}\".format(\n                        url.path(), e.strerror))\n                    continue\n                download = FakeDownload(fileobj)\n                self._in_progress.append(download)\n                self.on_download_finished(download)\n            else:\n                fobj = io.BytesIO()\n                fobj.name = 'adblock: ' + url.host()\n                target = downloads.FileObjDownloadTarget(fobj)\n                download = download_manager.get(url, target=target,\n                                                auto_remove=True)\n                self._in_progress.append(download)\n                download.finished.connect(\n                    functools.partial(self.on_download_finished, download))\n\n    def _merge_file(self, byte_io):\n        \"\"\"Read and merge host files.\n\n        Args:\n            byte_io: The BytesIO object of the completed download.\n\n        Return:\n            A set of the merged hosts.\n        \"\"\"\n        error_count = 0\n        line_count = 0\n        try:\n            f = get_fileobj(byte_io)\n        except (OSError, UnicodeDecodeError, zipfile.BadZipFile,\n                zipfile.LargeZipFile, LookupError) as e:\n            message.error(\"adblock: Error while reading {}: {} - {}\".format(\n                byte_io.name, e.__class__.__name__, e))\n            return\n        for line in f:\n            line_count += 1\n            # Remove comments\n            try:\n                hash_idx = line.index('#')\n                line = line[:hash_idx]\n            except ValueError:\n                pass\n            line = line.strip()\n            # Skip empty lines\n            if not line:\n                continue\n            parts = line.split()\n            if len(parts) == 1:\n                # \"one host per line\" format\n                host = parts[0]\n            elif len(parts) == 2:\n                # /etc/hosts format\n                host = parts[1]\n            else:\n                error_count += 1\n                continue\n            if host not in self.WHITELISTED:\n                self._blocked_hosts.add(host)\n        log.misc.debug(\"{}: read {} lines\".format(byte_io.name, line_count))\n        if error_count > 0:\n            message.error(\"adblock: {} read errors for {}\".format(\n                error_count, byte_io.name))\n\n    def on_lists_downloaded(self):\n        \"\"\"Install block lists after files have been downloaded.\"\"\"\n        with open(self._local_hosts_file, 'w', encoding='utf-8') as f:\n            for host in sorted(self._blocked_hosts):\n                f.write(host + '\\n')\n            message.info(\"adblock: Read {} hosts from {} sources.\".format(\n                len(self._blocked_hosts), self._done_count))\n\n    @config.change_filter('content', 'host-block-lists')\n    def on_config_changed(self):\n        \"\"\"Update files when the config changed.\"\"\"\n        urls = config.get('content', 'host-block-lists')\n        if urls is None:\n            try:\n                os.remove(self._local_hosts_file)\n            except FileNotFoundError:\n                pass\n            except OSError as e:\n                log.misc.exception(\"Failed to delete hosts file: {}\".format(e))\n\n    def on_download_finished(self, download):\n        \"\"\"Check if all downloads are finished and if so, trigger reading.\n\n        Arguments:\n            download: The finished DownloadItem.\n        \"\"\"\n        self._in_progress.remove(download)\n        if download.successful:\n            self._done_count += 1\n            try:\n                self._merge_file(download.fileobj)\n            finally:\n                download.fileobj.close()\n        if not self._in_progress:\n            try:\n                self.on_lists_downloaded()\n            except OSError:\n                log.misc.exception(\"Failed to write host block list!\")\n", "path": "qutebrowser/browser/adblock.py"}]}