configtypes: parse regex flags properly

This commit is contained in:
Florian Bruhin 2017-06-15 14:53:31 +02:00
parent a7c3bb0d55
commit 0857a45b0a
2 changed files with 18 additions and 1 deletions

View File

@ -27,6 +27,8 @@ import itertools
import collections import collections
import warnings import warnings
import datetime import datetime
import functools
import operator
import yaml import yaml
from PyQt5.QtCore import QUrl, Qt from PyQt5.QtCore import QUrl, Qt
@ -862,8 +864,14 @@ class Regex(BaseType):
def __init__(self, flags=0, none_ok=False): def __init__(self, flags=0, none_ok=False):
super().__init__(none_ok) super().__init__(none_ok)
self.flags = flags
self._regex_type = type(re.compile('')) self._regex_type = type(re.compile(''))
# Parse flags from configdata.yml
if flags == 0:
self.flags = flags
else:
self.flags = functools.reduce(
operator.or_,
(getattr(re, flag.strip()) for flag in flags.split(' | ')))
def _compile_regex(self, pattern): def _compile_regex(self, pattern):
"""Check if the given regex is valid. """Check if the given regex is valid.

View File

@ -1239,6 +1239,15 @@ class TestRegex:
with pytest.raises(configexc.ValidationError): with pytest.raises(configexc.ValidationError):
regex.from_py('foo') regex.from_py('foo')
@pytest.mark.parametrize('flags, expected', [
(0, 0),
('IGNORECASE', re.IGNORECASE),
('IGNORECASE | VERBOSE', re.IGNORECASE | re.VERBOSE),
])
def test_flag_parsing(self, klass, flags, expected):
typ = klass(flags=flags)
assert typ.flags == expected
class TestDict: class TestDict: