configtypes: Refactor regex validation.
This commit is contained in:
parent
8e52e5f2fc
commit
e4a216e7cf
@ -24,7 +24,6 @@ import shlex
|
|||||||
import base64
|
import base64
|
||||||
import codecs
|
import codecs
|
||||||
import os.path
|
import os.path
|
||||||
import sre_constants
|
|
||||||
import itertools
|
import itertools
|
||||||
import collections
|
import collections
|
||||||
|
|
||||||
@ -45,6 +44,14 @@ BOOLEAN_STATES = {'1': True, 'yes': True, 'true': True, 'on': True,
|
|||||||
'0': False, 'no': False, 'false': False, 'off': False}
|
'0': False, 'no': False, 'false': False, 'off': False}
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_regex(pattern, flags):
|
||||||
|
try:
|
||||||
|
re.compile(pattern, flags)
|
||||||
|
except re.error as e:
|
||||||
|
raise configexc.ValidationError(pattern, "must be a valid regex - " +
|
||||||
|
str(e))
|
||||||
|
|
||||||
|
|
||||||
class ValidValues:
|
class ValidValues:
|
||||||
|
|
||||||
"""Container for valid values for a given type.
|
"""Container for valid values for a given type.
|
||||||
@ -751,11 +758,7 @@ class Regex(BaseType):
|
|||||||
self._basic_validation(value)
|
self._basic_validation(value)
|
||||||
if not value:
|
if not value:
|
||||||
return
|
return
|
||||||
try:
|
_validate_regex(value, self.flags)
|
||||||
re.compile(value, self.flags)
|
|
||||||
except sre_constants.error as e:
|
|
||||||
raise configexc.ValidationError(value, "must be a valid regex - " +
|
|
||||||
str(e))
|
|
||||||
|
|
||||||
def transform(self, value):
|
def transform(self, value):
|
||||||
if not value:
|
if not value:
|
||||||
@ -783,13 +786,15 @@ class RegexList(List):
|
|||||||
self._basic_validation(value)
|
self._basic_validation(value)
|
||||||
if not value:
|
if not value:
|
||||||
return
|
return
|
||||||
try:
|
vals = super().transform(value)
|
||||||
vals = self.transform(value)
|
|
||||||
except sre_constants.error as e:
|
for val in vals:
|
||||||
raise configexc.ValidationError(value, "must be a list valid "
|
if val is None:
|
||||||
"regexes - " + str(e))
|
if not self.none_ok:
|
||||||
if not self.none_ok and None in vals:
|
raise configexc.ValidationError(
|
||||||
raise configexc.ValidationError(value, "items may not be empty!")
|
value, "items may not be empty!")
|
||||||
|
else:
|
||||||
|
_validate_regex(val, self.flags)
|
||||||
|
|
||||||
|
|
||||||
class File(BaseType):
|
class File(BaseType):
|
||||||
|
Loading…
Reference in New Issue
Block a user