conftypes: Fix is_none in all types

This commit is contained in:
Florian Bruhin 2014-08-05 13:49:03 +02:00
parent a117cf0405
commit 7c54c20808

View File

@ -207,8 +207,11 @@ class String(BaseType):
self.forbidden = forbidden self.forbidden = forbidden
def validate(self, value): def validate(self, value):
if self.none_ok and not value: if not value:
return if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
if self.forbidden is not None and any(c in value if self.forbidden is not None and any(c in value
for c in self.forbidden): for c in self.forbidden):
raise ValidationError(value, "may not contain the chars " raise ValidationError(value, "may not contain the chars "
@ -228,13 +231,15 @@ class List(BaseType):
typestr = 'string-list' typestr = 'string-list'
def transform(self, value): def transform(self, value):
vals = value.split(',') return [v if v else None for v in value.split(',')]
if self.none_ok:
vals = [v if v else None for v in vals]
return vals
def validate(self, value): def validate(self, value):
pass vals = self.transform(value)
if None in vals:
if self.none_ok:
pass
else:
raise ValidationError(value, "items may not be empty!")
class Bool(BaseType): class Bool(BaseType):
@ -252,9 +257,16 @@ class Bool(BaseType):
'0': False, 'no': False, 'false': False, 'off': False} '0': False, 'no': False, 'false': False, 'off': False}
def transform(self, value): def transform(self, value):
if not value:
return None
return Bool._BOOLEAN_STATES[value.lower()] return Bool._BOOLEAN_STATES[value.lower()]
def validate(self, value): def validate(self, value):
if not value:
if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
if value.lower() not in Bool._BOOLEAN_STATES: if value.lower() not in Bool._BOOLEAN_STATES:
raise ValidationError(value, "must be a boolean!") raise ValidationError(value, "must be a boolean!")
@ -287,8 +299,11 @@ class Int(BaseType):
return int(value) return int(value)
def validate(self, value): def validate(self, value):
if self.none_ok and not value: if not value:
return if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
try: try:
intval = int(value) intval = int(value)
except ValueError: except ValueError:
@ -343,8 +358,11 @@ class Float(BaseType):
return float(value) return float(value)
def validate(self, value): def validate(self, value):
if self.none_ok and not value: if not value:
return if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
try: try:
floatval = float(value) floatval = float(value)
except ValueError: except ValueError:
@ -468,8 +486,11 @@ class PercOrInt(BaseType):
self.maxint = maxint self.maxint = maxint
def validate(self, value): def validate(self, value):
if not value and self.none_ok: if not value:
return if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
if value.endswith('%'): if value.endswith('%'):
try: try:
intval = int(value[:-1]) intval = int(value[:-1])
@ -502,7 +523,10 @@ class Command(BaseType):
def validate(self, value): def validate(self, value):
if not value: if not value:
return if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
if value.split()[0] not in cmdutils.cmd_dict: if value.split()[0] not in cmdutils.cmd_dict:
raise ValidationError(value, "must be a valid command!") raise ValidationError(value, "must be a valid command!")
@ -522,9 +546,16 @@ class ColorSystem(BaseType):
('hsl', "Interpolate in the HSL color system.")) ('hsl', "Interpolate in the HSL color system."))
def validate(self, value): def validate(self, value):
if not value:
if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
super().validate(value.lower()) super().validate(value.lower())
def transform(self, value): def transform(self, value):
if not value:
return None
mapping = { mapping = {
'rgb': QColor.Rgb, 'rgb': QColor.Rgb,
'hsv': QColor.Hsv, 'hsv': QColor.Hsv,
@ -540,6 +571,11 @@ class QtColor(BaseType):
typestr = 'qcolor' typestr = 'qcolor'
def validate(self, value): def validate(self, value):
if not value:
if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
if QColor.isValidColor(value): if QColor.isValidColor(value):
pass pass
else: else:
@ -556,6 +592,11 @@ class CssColor(BaseType):
typestr = 'css-color' typestr = 'css-color'
def validate(self, value): def validate(self, value):
if not value:
if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
if value.startswith('-'): if value.startswith('-'):
# custom function name, won't validate. # custom function name, won't validate.
pass pass
@ -578,6 +619,11 @@ class QssColor(CssColor):
_GRADIENTS = ('qlineargradient', 'qradialgradient', 'qconicalgradient') _GRADIENTS = ('qlineargradient', 'qradialgradient', 'qconicalgradient')
def validate(self, value): def validate(self, value):
if not value:
if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
if any([value.startswith(start) for start in self._GRADIENTS]): if any([value.startswith(start) for start in self._GRADIENTS]):
# We can't validate this further. # We can't validate this further.
pass pass
@ -594,6 +640,11 @@ class Font(BaseType):
typestr = 'font' typestr = 'font'
def validate(self, value): def validate(self, value):
if not value:
if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
if not QSS_FONT_REGEX.match(value): if not QSS_FONT_REGEX.match(value):
raise ValidationError(value, "must be a valid font") raise ValidationError(value, "must be a valid font")
@ -646,6 +697,11 @@ class Regex(BaseType):
self.flags = flags self.flags = flags
def validate(self, value): def validate(self, value):
if not value:
if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
try: try:
re.compile(value, self.flags) re.compile(value, self.flags)
except RegexError as e: except RegexError as e:
@ -684,9 +740,12 @@ class File(BaseType):
typestr = 'file' typestr = 'file'
def validate(self, value): def validate(self, value):
if not value:
if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
value = os.path.expanduser(value) value = os.path.expanduser(value)
if self.none_ok and not value:
return
if not os.path.isfile(value): if not os.path.isfile(value):
raise ValidationError(value, "must be a valid file!") raise ValidationError(value, "must be a valid file!")
if not os.path.isabs(value): if not os.path.isabs(value):
@ -703,8 +762,11 @@ class Directory(BaseType):
typestr = 'directory' typestr = 'directory'
def validate(self, value): def validate(self, value):
if self.none_ok and not value: if not value:
return if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
if not os.path.isdir(value): if not os.path.isdir(value):
raise ValidationError(value, "must be a valid directory!") raise ValidationError(value, "must be a valid directory!")
if not os.path.isabs(value): if not os.path.isabs(value):
@ -745,7 +807,8 @@ class WebKitBytes(BaseType):
self.maxsize = maxsize self.maxsize = maxsize
def validate(self, value): def validate(self, value):
if value == '': if not value:
# WebKitBytes is always None-able.
return return
try: try:
val = self.transform(value) val = self.transform(value)
@ -759,7 +822,7 @@ class WebKitBytes(BaseType):
raise ValidationError(value, "must be 0 minimum!") raise ValidationError(value, "must be 0 minimum!")
def transform(self, value): def transform(self, value):
if value == '': if not value:
return None return None
if any(value.lower().endswith(c) for c in self.SUFFIXES): if any(value.lower().endswith(c) for c in self.SUFFIXES):
suffix = value[-1].lower() suffix = value[-1].lower()
@ -794,7 +857,7 @@ class WebKitBytesList(List):
return [self.bytestype.transform(val) for val in vals] return [self.bytestype.transform(val) for val in vals]
def validate(self, value): def validate(self, value):
if value == '': if not value:
return return
vals = super().transform(value) vals = super().transform(value)
for val in vals: for val in vals:
@ -821,6 +884,11 @@ class ShellCommand(String):
self.placeholder = placeholder self.placeholder = placeholder
def validate(self, value): def validate(self, value):
if not value:
if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
super().validate(value) super().validate(value)
if self.placeholder and '{}' not in value: if self.placeholder and '{}' not in value:
raise ValidationError(value, "needs to contain a {}-placeholder.") raise ValidationError(value, "needs to contain a {}-placeholder.")
@ -861,6 +929,11 @@ class Proxy(BaseType):
} }
def validate(self, value): def validate(self, value):
if not value:
if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
if value in self.valid_values: if value in self.valid_values:
return return
url = QUrl(value) url = QUrl(value)
@ -910,7 +983,11 @@ class SearchEngineName(BaseType):
"""A search engine name.""" """A search engine name."""
def validate(self, value): def validate(self, value):
pass if not value:
if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
class SearchEngineUrl(BaseType): class SearchEngineUrl(BaseType):
@ -918,6 +995,11 @@ class SearchEngineUrl(BaseType):
"""A search engine URL.""" """A search engine URL."""
def validate(self, value): def validate(self, value):
if not value:
if self.none_ok:
return
else:
raise ValidationError(value, "may not be empty!")
if '{}' in value: if '{}' in value:
pass pass
else: else: