Use exceptions for validation.
This commit is contained in:
parent
e61d7f724f
commit
c64d1029a7
@ -18,6 +18,11 @@
|
|||||||
"""Setting options used for qutebrowser."""
|
"""Setting options used for qutebrowser."""
|
||||||
|
|
||||||
|
|
||||||
|
class ValidationError(ValueError):
|
||||||
|
"""Exception raised when a value for a config type was invalid."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ValidValues:
|
class ValidValues:
|
||||||
|
|
||||||
"""Container for valid values for a given type.
|
"""Container for valid values for a given type.
|
||||||
@ -88,16 +93,17 @@ class BaseType:
|
|||||||
Args:
|
Args:
|
||||||
value: The value to validate.
|
value: The value to validate.
|
||||||
|
|
||||||
Return:
|
|
||||||
Ture if validation succeeded, False otherwise.
|
|
||||||
|
|
||||||
Raise:
|
Raise:
|
||||||
|
ValidationError if the value was invalid.
|
||||||
NotImplementedError if self.valid_values is not defined and this
|
NotImplementedError if self.valid_values is not defined and this
|
||||||
method should be overridden.
|
method should be overridden.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if self.valid_values is not None:
|
if self.valid_values is not None:
|
||||||
return value in self.valid_values
|
if value not in self.valid_values:
|
||||||
|
raise ValidationError
|
||||||
|
else:
|
||||||
|
return
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@ -127,7 +133,8 @@ class Bool(BaseType):
|
|||||||
return self._BOOLEAN_STATES[value.lower()]
|
return self._BOOLEAN_STATES[value.lower()]
|
||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
return value.lower() in self._BOOLEAN_STATES
|
if value.lower() not in self._BOOLEAN_STATES:
|
||||||
|
raise ValidationError
|
||||||
|
|
||||||
|
|
||||||
class Int(BaseType):
|
class Int(BaseType):
|
||||||
@ -143,9 +150,7 @@ class Int(BaseType):
|
|||||||
try:
|
try:
|
||||||
int(value)
|
int(value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return False
|
raise ValidationError
|
||||||
else:
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
class List(BaseType):
|
class List(BaseType):
|
||||||
@ -158,7 +163,7 @@ class List(BaseType):
|
|||||||
return value.split(',')
|
return value.split(',')
|
||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
return True
|
pass
|
||||||
|
|
||||||
|
|
||||||
class IntList(List):
|
class IntList(List):
|
||||||
@ -175,9 +180,7 @@ class IntList(List):
|
|||||||
try:
|
try:
|
||||||
self.transform(value)
|
self.transform(value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return False
|
raise ValidationError
|
||||||
else:
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
class PercOrInt(BaseType):
|
class PercOrInt(BaseType):
|
||||||
@ -189,16 +192,18 @@ class PercOrInt(BaseType):
|
|||||||
try:
|
try:
|
||||||
intval = int(value.rstrip('%'))
|
intval = int(value.rstrip('%'))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return False
|
raise ValidationError
|
||||||
else:
|
else:
|
||||||
return 0 <= intval <= 100
|
if not 0 <= intval <= 100:
|
||||||
|
raise ValidationError
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
intval = int(value)
|
intval = int(value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return False
|
raise ValidationError
|
||||||
else:
|
else:
|
||||||
return intval > 0
|
if intval < 0:
|
||||||
|
raise ValidationError
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseType):
|
class Command(BaseType):
|
||||||
@ -218,10 +223,8 @@ class Command(BaseType):
|
|||||||
#try:
|
#try:
|
||||||
# cp.parse(value)
|
# cp.parse(value)
|
||||||
#except NoSuchCommandError:
|
#except NoSuchCommandError:
|
||||||
# return False
|
# raise ValidationError
|
||||||
#else:
|
pass
|
||||||
# return True
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
class Color(BaseType):
|
class Color(BaseType):
|
||||||
@ -232,7 +235,7 @@ class Color(BaseType):
|
|||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
# FIXME validate colors
|
# FIXME validate colors
|
||||||
return True
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Font(BaseType):
|
class Font(BaseType):
|
||||||
@ -243,7 +246,7 @@ class Font(BaseType):
|
|||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
# FIXME validate fonts
|
# FIXME validate fonts
|
||||||
return True
|
pass
|
||||||
|
|
||||||
|
|
||||||
class SearchEngineName(BaseType):
|
class SearchEngineName(BaseType):
|
||||||
@ -251,7 +254,7 @@ class SearchEngineName(BaseType):
|
|||||||
"""A search engine name."""
|
"""A search engine name."""
|
||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
return True
|
pass
|
||||||
|
|
||||||
|
|
||||||
class SearchEngineUrl(BaseType):
|
class SearchEngineUrl(BaseType):
|
||||||
@ -268,7 +271,7 @@ class KeyBindingName(BaseType):
|
|||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
# FIXME can we validate anything here?
|
# FIXME can we validate anything here?
|
||||||
return True
|
pass
|
||||||
|
|
||||||
|
|
||||||
class AutoSearch(BaseType):
|
class AutoSearch(BaseType):
|
||||||
@ -281,9 +284,9 @@ class AutoSearch(BaseType):
|
|||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
if value.lower() in ["naive", "dns"]:
|
if value.lower() in ["naive", "dns"]:
|
||||||
return True
|
pass
|
||||||
else:
|
else:
|
||||||
return Bool.validate(self, value)
|
Bool.validate(self, value)
|
||||||
|
|
||||||
def transform(self, value):
|
def transform(self, value):
|
||||||
if value.lower() in ["naive", "dns"]:
|
if value.lower() in ["naive", "dns"]:
|
||||||
|
Loading…
Reference in New Issue
Block a user