From 4f673181e8e91187d549f6497f036c5397d7238e Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Tue, 28 Oct 2003 23:32:56 +0000 Subject: [PATCH] Added Toggleable mixin. --- src/plugins.py | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/plugins.py b/src/plugins.py index 068787268..a0f6d02ea 100644 --- a/src/plugins.py +++ b/src/plugins.py @@ -188,13 +188,14 @@ class PeriodicFileDownloader(object): t.start() world.threadsSpawned += 1 + class ToggleDictionary(object): """I am ToggleDictionary! Hear me roar! """ def __init__(self, toggles): if not toggles: raise ValueError, 'At least one toggle must be provided.' - self.channels = {} + self.channels = ircutils.IrcDict() self.defaults = toggles def _getDict(self, channel): @@ -232,4 +233,46 @@ class ToggleDictionary(object): resp.sort() return '(%s)' % '; '.join(resp) + +class Toggleable(object): + """A mixin class to provide a 'toggle' command that can be consistent + across plugins. To use this class, simply define a 'toggles' attribute + in your class that is a ToggleDictionary mapping valid attributes to toggle + to their default values. + """ + def __init__(self): + s = """[] [] + + Toggles the value of in . If is given, + explicitly sets the value of to . is only + necessary if the message isn't sent in the channel itself. Valid + names are %s""" % (self._toggleNames()) + self.toggle.__doc__ = s + + def _toggleNames(self): + return utils.commaAndify(map(repr, self.toggles.defaults.keys())) + + def toggle(self, irc, msg, args): + try: + channel = getChannel(msg, args) + except callbacks.Error: + channel = None + (name, value) = getArgs(args, optional=1) + if not value: + value = None + elif value.lower() in ('enable', 'on', 'true'): + value = True + elif value.lower() in ('disable', 'off', 'false'): + value = False + else: + irc.error(msg, '%r isn\'t a valid value.' % value) + return + try: + self.toggles.toggle(name, value=value, channel=channel) + s = '%s %s' % (conf.replySuccess, self.toggles.toString(channel)) + irc.reply(msg, s) + except KeyError: + irc.error(msg, '%r isn\'t a valid name to toggle. ' + 'Valid names are %s' % (name, self._toggleNames())) + # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: