Add a UrlPattern config type

For some settings it makes sense to have a list of URL patterns in the config,
rather than having a per-domain boolean setting.
This commit is contained in:
Florian Bruhin 2018-09-01 15:51:14 +02:00
parent 5c8d4ede06
commit 7fb2224640
3 changed files with 39 additions and 2 deletions

View File

@ -3449,5 +3449,8 @@ See the setting's valid values for more information on allowed values.
See https://sqlite.org/lang_datefunc.html for reference.
|UniqueCharString|A string which may not contain duplicate chars.
|Url|A URL as a string.
|UrlPattern|A match pattern for a URL.
See https://developer.chrome.com/apps/match_patterns for the allowed syntax.
|VerticalPosition|The position of the download bar.
|==============

View File

@ -61,7 +61,7 @@ from PyQt5.QtWidgets import QTabWidget, QTabBar
from qutebrowser.commands import cmdutils
from qutebrowser.config import configexc
from qutebrowser.utils import standarddir, utils, qtutils, urlutils
from qutebrowser.utils import standarddir, utils, qtutils, urlutils, urlmatch
from qutebrowser.keyinput import keyutils
@ -1661,3 +1661,22 @@ class Key(BaseType):
return keyutils.KeySequence.parse(value)
except keyutils.KeyParseError as e:
raise configexc.ValidationError(value, str(e))
class UrlPattern(BaseType):
"""A match pattern for a URL.
See https://developer.chrome.com/apps/match_patterns for the allowed
syntax.
"""
def to_py(self, value):
self._basic_py_validation(value, str)
if not value:
return None
try:
return urlmatch.UrlPattern(value)
except urlmatch.ParseError as e:
raise configexc.ValidationError(value, str(e))

View File

@ -35,7 +35,7 @@ from PyQt5.QtGui import QColor, QFont
from PyQt5.QtNetwork import QNetworkProxy
from qutebrowser.config import configtypes, configexc
from qutebrowser.utils import debug, utils, qtutils
from qutebrowser.utils import debug, utils, qtutils, urlmatch
from qutebrowser.browser.network import pac
from qutebrowser.keyinput import keyutils
from tests.helpers import utils as testutils
@ -2099,6 +2099,21 @@ class TestKey:
klass().to_py(val)
class TestUrlPattern:
@pytest.fixture
def klass(self):
return configtypes.UrlPattern
def test_to_py_valid(self, klass):
pattern = 'http://*.example.com/'
assert klass().to_py(pattern) == urlmatch.UrlPattern(pattern)
def test_to_py_invalid(self, klass):
with pytest.raises(configexc.ValidationError):
klass().to_py('http://')
@pytest.mark.parametrize('first, second, equal', [
(re.compile('foo'), RegexEq('foo'), True),
(RegexEq('bar'), re.compile('bar'), True),