Abstracted out bool verification in utils.toBool.

This commit is contained in:
Jeremy Fincher 2004-09-28 07:22:26 +00:00
parent 05c06f8f4b
commit 0874dfd5f3
2 changed files with 14 additions and 22 deletions

View File

@ -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))

View File

@ -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__'])