Ah, a better way to do default normalization.

This commit is contained in:
Jeremy Fincher 2004-04-08 11:13:03 +00:00
parent 9eee85dc63
commit 3538dee7c9
2 changed files with 9 additions and 4 deletions

View File

@ -424,9 +424,6 @@ length of time a driver should block waiting for input."""))
class ValidDriverModule(registry.OnlySomeStrings): class ValidDriverModule(registry.OnlySomeStrings):
validStrings = ('socketDrivers', 'twistedDrivers', 'asyncoreDrivers') validStrings = ('socketDrivers', 'twistedDrivers', 'asyncoreDrivers')
def normalize(self, s):
# We can't be case insensitive here. At least not without work.
return s
supybot.drivers.register('module', ValidDriverModule('socketDrivers', """ supybot.drivers.register('module', ValidDriverModule('socketDrivers', """
Determines what driver module the bot will use. socketDrivers, a simple Determines what driver module the bot will use. socketDrivers, a simple

View File

@ -297,13 +297,21 @@ class String(Value):
raise InvalidRegistryValue, '%r is not a string.' % s raise InvalidRegistryValue, '%r is not a string.' % s
class OnlySomeStrings(String): class OnlySomeStrings(String):
normalize = staticmethod(str.lower)
validStrings = () validStrings = ()
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
assert self.validStrings, 'There must be some valid strings. ' \ assert self.validStrings, 'There must be some valid strings. ' \
'This is a bug.' 'This is a bug.'
String.__init__(self, *args, **kwargs) String.__init__(self, *args, **kwargs)
def normalize(self, s):
lowered = s.lower()
L = list(map(str.lower, self.validStrings))
try:
i = L.index(lowered)
except ValueError:
return s # This is handled in setValue.
return self.validStrings[i]
def setValue(self, s): def setValue(self, s):
s = self.normalize(s) s = self.normalize(s)
if s in self.validStrings: if s in self.validStrings: