diff options
author | Florian Bruhin <me@the-compiler.org> | 2019-12-20 14:13:52 +0100 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2019-12-20 16:54:08 +0100 |
commit | 608a1ead410607c6fd7394dcf75d55495448b4ea (patch) | |
tree | 6d0a921a3868400bb1bc290ff993ab86ca5bb7c8 | |
parent | 7b87f6760f200add7e9eac9e8796b25e710ffbd6 (diff) | |
download | qutebrowser-608a1ead410607c6fd7394dcf75d55495448b4ea.tar.gz qutebrowser-608a1ead410607c6fd7394dcf75d55495448b4ea.zip |
Move widened_hostnames to configutils
This avoids various circular imports such as:
urlutils -> config -> configdata -> configtypes -> configexc -> jinja ->
urlutils
or:
configutils -> urlutils -> config -> configutils
-rw-r--r-- | qutebrowser/config/configutils.py | 13 | ||||
-rw-r--r-- | qutebrowser/utils/urlutils.py | 9 | ||||
-rw-r--r-- | tests/unit/config/test_configutils.py | 23 | ||||
-rw-r--r-- | tests/unit/utils/test_urlutils.py | 23 |
4 files changed, 34 insertions, 34 deletions
diff --git a/qutebrowser/config/configutils.py b/qutebrowser/config/configutils.py index 57a0893d2..1a7f612cb 100644 --- a/qutebrowser/config/configutils.py +++ b/qutebrowser/config/configutils.py @@ -28,13 +28,22 @@ import operator from PyQt5.QtCore import QUrl -from qutebrowser.utils import utils, urlmatch, urlutils, usertypes +from qutebrowser.utils import utils, urlmatch, usertypes from qutebrowser.config import configexc if typing.TYPE_CHECKING: from qutebrowser.config import configdata +def _widened_hostnames(hostname: str) -> typing.Iterable[str]: + """A generator for widening string hostnames. + + Ex: a.c.foo -> [a.c.foo, c.foo, foo]""" + while hostname: + yield hostname + hostname = hostname.partition(".")[-1] + + class ScopedValue: """A configuration value which is valid for a UrlPattern. @@ -216,7 +225,7 @@ class Values: return self._get_fallback(fallback) candidates = [] # type: typing.List[ScopedValue] - widened_hosts = urlutils.widened_hostnames(url.host()) + widened_hosts = _widened_hostnames(url.host()) # We must check the 'None' key as well, in case any patterns that # did not have a domain match. for host in itertools.chain(widened_hosts, [None]): diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index 51f0fd6cc..2908327dd 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -617,12 +617,3 @@ def proxy_from_url(url: QUrl) -> QNetworkProxy: if url.password(): proxy.setPassword(url.password()) return proxy - - -def widened_hostnames(hostname: str) -> typing.Iterable[str]: - """A generator for widening string hostnames. - - Ex: a.c.foo -> [a.c.foo, c.foo, foo]""" - while hostname: - yield hostname - hostname = hostname.partition(".")[-1] diff --git a/tests/unit/config/test_configutils.py b/tests/unit/config/test_configutils.py index 518391d29..f09df1c7f 100644 --- a/tests/unit/config/test_configutils.py +++ b/tests/unit/config/test_configutils.py @@ -250,3 +250,26 @@ def test_domain_lookup_sparse_benchmark(url, values, benchmark): values.add(False, urlmatch.UrlPattern(line)) benchmark(lambda: values.get_for_url(url)) + + +class TestWiden: + + @pytest.mark.parametrize('hostname, expected', [ + ('a.b.c', ['a.b.c', 'b.c', 'c']), + ('foobarbaz', ['foobarbaz']), + ('', []), + ('.c', ['.c', 'c']), + ('c.', ['c.']), + ('.c.', ['.c.', 'c.']), + (None, []), + ]) + def test_widen_hostnames(self, hostname, expected): + assert list(configutils._widened_hostnames(hostname)) == expected + + @pytest.mark.parametrize('hostname', [ + 'test.qutebrowser.org', + 'a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.z.y.z', + 'qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq.c', + ]) + def test_bench_widen_hostnames(self, hostname, benchmark): + benchmark(lambda: list(configutils._widened_hostnames(hostname))) diff --git a/tests/unit/utils/test_urlutils.py b/tests/unit/utils/test_urlutils.py index bf1ce47c8..b6d73319d 100644 --- a/tests/unit/utils/test_urlutils.py +++ b/tests/unit/utils/test_urlutils.py @@ -706,26 +706,3 @@ class TestProxyFromUrl: def test_invalid(self, url, exception): with pytest.raises(exception): urlutils.proxy_from_url(QUrl(url)) - - -class TestWiden: - - @pytest.mark.parametrize('hostname, expected', [ - ('a.b.c', ['a.b.c', 'b.c', 'c']), - ('foobarbaz', ['foobarbaz']), - ('', []), - ('.c', ['.c', 'c']), - ('c.', ['c.']), - ('.c.', ['.c.', 'c.']), - (None, []), - ]) - def test_widen_hostnames(self, hostname, expected): - assert list(urlutils.widened_hostnames(hostname)) == expected - - @pytest.mark.parametrize('hostname', [ - 'test.qutebrowser.org', - 'a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.z.y.z', - 'qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq.c', - ]) - def test_bench_widen_hostnames(self, hostname, benchmark): - benchmark(lambda: list(urlutils.widened_hostnames(hostname))) |