Add TimestampTemplate config type

No more crash when 'completion->timestamp-format' is set to '%'.

Fixes #1039.
This commit is contained in:
Daniel 2015-10-21 17:23:39 +02:00
parent 4fb374e764
commit ef8a681fcc
3 changed files with 42 additions and 1 deletions

View File

@ -381,7 +381,7 @@ def data(readonly=False):
"What to display in the download filename input."), "What to display in the download filename input."),
('timestamp-format', ('timestamp-format',
SettingValue(typ.String(none_ok=True), '%Y-%m-%d'), SettingValue(typ.TimestampTemplate(none_ok=True), '%Y-%m-%d'),
"How to format timestamps (e.g. for history)"), "How to format timestamps (e.g. for history)"),
('show', ('show',

View File

@ -27,6 +27,7 @@ import os.path
import itertools import itertools
import collections import collections
import warnings import warnings
import datetime
from PyQt5.QtCore import QUrl from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QColor, QFont from PyQt5.QtGui import QColor, QFont
@ -1630,3 +1631,25 @@ class URLSegmentList(FlagList):
"""A list of URL segments.""" """A list of URL segments."""
valid_values = ValidValues('host', 'path', 'query', 'anchor') valid_values = ValidValues('host', 'path', 'query', 'anchor')
class TimestampTemplate(BaseType):
"""A strftime-like template for timestamps.
See
https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior
for reference.
"""
def validate(self, value):
self._basic_validation(value)
if not value:
return
try:
# Dummy check to see if the template is valid
datetime.datetime.now().strftime(value)
except ValueError as error:
# thrown on invalid template string
raise configexc.ValidationError(
value, "Invalid format string: {}".format(error))

View File

@ -2042,6 +2042,24 @@ class TestUserAgent:
klass().complete() klass().complete()
class TestTimestampTemplate:
"""Test TimestampTemplate."""
@pytest.fixture
def klass(self):
return configtypes.TimestampTemplate
@pytest.mark.parametrize('val', ['', 'foobar', '%H:%M', 'foo %H bar %M'])
def test_validate_valid(self, klass, val):
klass(none_ok=True).validate(val)
@pytest.mark.parametrize('val', ['', '%'])
def test_validate_invalid(self, klass, val):
with pytest.raises(configexc.ValidationError):
klass().validate(val)
@pytest.mark.parametrize('first, second, equal', [ @pytest.mark.parametrize('first, second, equal', [
(re.compile('foo'), RegexEq('foo'), True), (re.compile('foo'), RegexEq('foo'), True),
(RegexEq('bar'), re.compile('bar'), True), (RegexEq('bar'), re.compile('bar'), True),