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 warnings
import datetime
import functools
import operator
import yaml
from PyQt5.QtCore import QUrl, Qt
@ -862,8 +864,14 @@ class Regex(BaseType):
def __init__(self, flags=0, none_ok=False):
super().__init__(none_ok)
self.flags = flags
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):
"""Check if the given regex is valid.

View File

@ -1239,6 +1239,15 @@ class TestRegex:
with pytest.raises(configexc.ValidationError):
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: