mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-27 21:29:24 +01:00
Abstracted out bool verification in utils.toBool.
This commit is contained in:
parent
05c06f8f4b
commit
0874dfd5f3
@ -327,16 +327,14 @@ class Value(Group):
|
||||
class Boolean(Value):
|
||||
"""Value must be either True or False (or On or Off)."""
|
||||
def set(self, s):
|
||||
s = s.strip().lower()
|
||||
if s in ('true', 'on', 'enable', 'enabled'):
|
||||
value = True
|
||||
elif s in ('false', 'off', 'disable', 'disabled'):
|
||||
value = False
|
||||
elif s == 'toggle':
|
||||
value = not self.value
|
||||
else:
|
||||
self.error()
|
||||
self.setValue(value)
|
||||
try:
|
||||
v = utils.toBool(s)
|
||||
except ValueError:
|
||||
if s.strip().lower() == 'toggle':
|
||||
v = not self.value
|
||||
else:
|
||||
self.error()
|
||||
self.setValue(v)
|
||||
|
||||
def setValue(self, v):
|
||||
super(Boolean, self).setValue(bool(v))
|
||||
|
18
src/utils.py
18
src/utils.py
@ -807,21 +807,15 @@ def callTracer(fd=None, basename=True):
|
||||
return tracer
|
||||
|
||||
|
||||
# These are used by Owner and Misc for their callBefore/callAfter attributes.
|
||||
class Everything(object):
|
||||
def __contains__(self, x):
|
||||
def toBool(s):
|
||||
s = s.strip().lower()
|
||||
if s in ('true', 'on', 'enable', 'enabled'):
|
||||
return True
|
||||
|
||||
def __iter__(self):
|
||||
return iter([])
|
||||
|
||||
class Nothing(object):
|
||||
def __contains__(self, x):
|
||||
elif s in ('false', 'off', 'disable', 'disabled'):
|
||||
return False
|
||||
else:
|
||||
raise ValueError, 'Invalid string for toBool: %r' % s
|
||||
|
||||
def __iter__(self):
|
||||
return iter([])
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
doctest.testmod(sys.modules['__main__'])
|
||||
|
Loading…
Reference in New Issue
Block a user