Changes to ToggleDictionary.

This commit is contained in:
Jeremy Fincher 2003-10-28 23:14:49 +00:00
parent 2757f239b1
commit e49e5d02f4
2 changed files with 28 additions and 23 deletions

View File

@ -192,33 +192,39 @@ class ToggleDictionary(object):
"""I am ToggleDictionary! Hear me roar! """I am ToggleDictionary! Hear me roar!
""" """
def __init__(self, toggles): def __init__(self, toggles):
if toggles.keys() == []: if not toggles:
raise ValueError raise ValueError, 'At least one toggle must be provided.'
self.toggles = {} self.channels = {}
self.toggles['Default'] = toggles self.defaults = toggles
def get(self, key, channel='Default'): def _getDict(self, channel):
if channel != 'Default': if channel is None:
try: return self.defaults
return self.toggles[channel][key]
except KeyError:
return self.toggles['Default'][key]
else: else:
return self.toggles[channel][key] if channel not in self.channels:
self.channels[channel] = self.defaults.copy()
return self.channels[channel]
def toggle(self, key, value=None, channel='Default'): def get(self, key, channel=None):
if value is not None and value != True and value != False: return self._getDict(channel)[key]
raise ValueError
if not self.toggles.has_key(channel): def toggle(self, key, value=None, channel=None):
self.toggles[channel] = self.toggles['Default'].copy() d = self._getDict(channel)
if value is None: if value is None:
self.toggles[channel][key] = not self.toggles[channel][key] d[key] = not d[key] # Raises KeyError, we want this.
else: else:
self.toggles[channel][key] = value # I considered this, to save the if statement:
# d[key] = (d[key] ^ d[key]) or value
# But didn't, so people can provide non-boolean keys.
if key in d:
d[key] = value
else:
raise KeyError, key
def toString(self, channel='Default'): def toString(self, channel=None):
resp = [] resp = []
for k,v in self.toggles[channel].iteritems(): d = self._getDict(channel)
for (k, v) in d.iteritems():
if v: if v:
resp.append('%s: On' % k) resp.append('%s: On' % k)
else: else:

View File

@ -44,7 +44,7 @@ class ToggleDictionaryTestCase(unittest.TestCase):
self.assertEqual(t.get('foo', '#baz'), True) self.assertEqual(t.get('foo', '#baz'), True)
t.toggle('foo', channel='#baz') t.toggle('foo', channel='#baz')
self.assertEqual(t.get('foo', '#baz'), False) self.assertEqual(t.get('foo', '#baz'), False)
self.assertRaises(ValueError, t.toggle, 'foo', value='lak') #self.assertRaises(TypeError, t.toggle, 'foo', value='lak')
def test__init__(self): def test__init__(self):
self.assertRaises(TypeError, plugins.ToggleDictionary.__init__) self.assertRaises(TypeError, plugins.ToggleDictionary.__init__)
@ -53,8 +53,7 @@ class ToggleDictionaryTestCase(unittest.TestCase):
def testToggle(self): def testToggle(self):
t = plugins.ToggleDictionary({'foo': True}) t = plugins.ToggleDictionary({'foo': True})
self.assertRaises(KeyError, t.toggle, 'bar') self.assertRaises(KeyError, t.toggle, 'bar')
t.toggle('bar', value=False) self.assertRaises(KeyError, t.toggle, 'bar', value=False)
self.assertEqual(t.get('bar'), False)
def testToString(self): def testToString(self):
t = plugins.ToggleDictionary({'foo': True, 'bar': False}) t = plugins.ToggleDictionary({'foo': True, 'bar': False})