Factored invariant checking into setValue as appropriate.

This commit is contained in:
Jeremy Fincher 2004-01-20 15:08:08 +00:00
parent b8d73d98c6
commit 68c4ca6895

View File

@ -78,10 +78,14 @@ class Value(object):
def set(self, s): def set(self, s):
"""Override this with a function to convert a string to whatever type """Override this with a function to convert a string to whatever type
you want, and set it to .value.""" you want, and call self.setValue to set the value."""
raise NotImplementedError raise NotImplementedError
def setValue(self, v): def setValue(self, v):
"""Check conditions on the actual value type here. I.e., if you're a
IntegerLessThanOneHundred (all your values must be integers less than
100) convert to an integer in set() and check that the integer is less
than 100 in this method."""
self.value = v self.value = v
def reset(self): def reset(self):
@ -99,36 +103,41 @@ class Boolean(Value):
def set(self, s): def set(self, s):
s = s.lower() s = s.lower()
if s in ('true', 'on', 'enabled'): if s in ('true', 'on', 'enabled'):
self.value = True value = True
elif s in ('false', 'off', 'disabled'): elif s in ('false', 'off', 'disabled'):
self.value = False value = False
elif s == 'toggle': elif s == 'toggle':
self.value = not self.value value = not self.value
else: else:
raise InvalidRegistryValue, 'Value must be True or False.' raise InvalidRegistryValue, 'Value must be True or False.'
self.setValue(value)
def setValue(self, v):
self.value = bool(v)
class Integer(Value): class Integer(Value):
def set(self, s): def set(self, s):
try: try:
self.value = int(s) self.setValue(int(s))
except ValueError: except ValueError:
raise InvalidRegistryValue, 'Value must be an integer.' raise InvalidRegistryValue, 'Value must be an integer.'
class PositiveInteger(Value): class PositiveInteger(Value):
def set(self, s): def set(self, s):
try: try:
original = self.value self.setValue(int(s))
self.value = int(s)
if self.value < 0:
raise ValueError
except ValueError: except ValueError:
self.value = original
raise InvalidRegistryValue, 'Value must be a positive integer.' raise InvalidRegistryValue, 'Value must be a positive integer.'
def setValue(self, v):
if v <= 0:
raise ValueError, 'Value must be a positive integer.'
self.value = v
class Float(Value): class Float(Value):
def set(self, s): def set(self, s):
try: try:
self.value = float(s) self.setValue(float(s))
except ValueError: except ValueError:
raise InvalidRegistryValue, 'Value must be a float.' raise InvalidRegistryValue, 'Value must be a float.'
@ -156,23 +165,26 @@ class NormalizedString(String):
class StringSurroundedBySpaces(String): class StringSurroundedBySpaces(String):
def set(self, s): def set(self, s):
String.set(self, s) String.set(self, s)
if self.value.lstrip() == self.value: self.setValue(self.value)
self.value = ' ' + self.value
if self.value.rstrip() == self.value: def setValue(self, v):
self.value += ' ' if v.lstrip() == v:
v= ' ' + self.value
if v.rstrip() == v:
v += ' '
self.value = v
class CommaSeparatedListOfStrings(String): class CommaSeparatedListOfStrings(String):
def set(self, s): def set(self, s):
String.set(self, s) String.set(self, s)
self.value = map(str.strip, self.value.split(',')) self.setValue(map(str.strip, self.value.split(',')))
def __str__(self): def __str__(self):
return ','.join(self.value) return ','.join(self.value)
class CommaSeparatedSetOfStrings(CommaSeparatedListOfStrings): class CommaSeparatedSetOfStrings(CommaSeparatedListOfStrings):
def set(self, s): def setValue(self, v):
CommaSeparatedListOfStrings.set(self, s) self.value = sets.Set(v)
self.value = sets.Set(self.value)
class Group(object): class Group(object):
def __init__(self): def __init__(self):