diff --git a/plugins/Ebay.py b/plugins/Ebay.py index c5a364c2c..a0da0ca49 100644 --- a/plugins/Ebay.py +++ b/plugins/Ebay.py @@ -64,18 +64,18 @@ def configure(onStart, afterConnect, advanced): if yn('Do you want the Ebay snarfer enabled by default?') == 'n': onStart.append('Ebay toggle auction off') -class Ebay(callbacks.PrivmsgCommandAndRegexp, plugins.Toggleable): +class Ebay(callbacks.PrivmsgCommandAndRegexp, plugins.Configurable): """ Module for eBay stuff. Currently contains a URL snarfer and a command to get info about an auction. """ threaded = True regexps = ['ebaySnarfer'] - toggles = plugins.ToggleDictionary({'auction' : True}) - + configurables = plugins.ConfigurableDictionary( + [('snarfer', utils.safeEval, True, 'Controls the auction snarfer.')] + ) def __init__(self): callbacks.PrivmsgCommandAndRegexp.__init__(self) - plugins.Toggleable.__init__(self) _reopts = re.I | re.S _info = re.compile(r'eBay item (\d+) \([^)]+\) - ([^<]+)', @@ -119,7 +119,7 @@ class Ebay(callbacks.PrivmsgCommandAndRegexp, plugins.Toggleable): def ebaySnarfer(self, irc, msg, match): r"http://cgi\.ebay\.(?:com(?:.au)?|ca|co.uk)/(?:.*?/)?(?:ws/)?"\ r"eBayISAPI\.dll\?ViewItem(?:&item=\d+|&category=\d+)+" - if not self.toggles.get('auction', channel=msg.args[0]): + if not self.configurables.get('snarfer', channel=msg.args[0]): return url = match.group(0) #debug.printf(url) diff --git a/src/plugins.py b/src/plugins.py index 786ed64d0..bde561238 100644 --- a/src/plugins.py +++ b/src/plugins.py @@ -212,32 +212,65 @@ class ConfigurableDictionary(object): self.channels[None][name] = default def get(self, name, channel=None): - return self.channels[channel][name] + try: + return self.channels[channel][name] + except KeyError: + return self.channels[None][name] def set(self, name, value, channel=None): - self.channels[channel][name] = self.types[value] + d = self.channels.setdefault(channel, {}) + d[name] = self.types[name](value) + + def help(self, name): + return self.helps[name] + + def names(self): + L = self.helps.keys() + L.sort() + return L # XXX: Make persistent. class Configurable(object): """A mixin class to provide a "config" command that can be consistent across all plugins, in order to unify the configuration for each plugin. + + Plugins subclassing this should have a "configurables" attribute which is + a ConfigurableDictionary initialized with a list of 4-tuples of + (name, type, default, help). Name is the string name of the config + variable; type is a function taking a string and returning some value of + the type the variable is supposed to be; default is the default value the + variable should take on; help is a string that'll be returned to describe + the purpose of the config variable. """ - def __init__(self): - self.configurables = ConfigurableDictionary(self.configurables) - def config(self, irc, msg, args): - """[] [] + """[] [] [] - Sets the config variable to on . If - isn't given, returns the current value of on . - is only necessary if the message isn't sent in the channel - itself. + Sets the value of config variable to on . If + is given but is not, returns the help and current value + for . If neither nor is given, returns the valid + config variables for this plugin. is only necessary if the + message isn't sent in the channel itself. """ - channel = privmsgs.getChannel(msg, args) + try: + channel = privmsgs.getChannel(msg, args) + except callbacks.ArgumentError: + channel = None (name, value) = privmsgs.getArgs(args, needed=0, optional=2) if not name: - pass + irc.reply(msg, utils.commaAndify(self.configurables.names())) + return + if not value: + help = self.configurables.help(name) + value = self.configurables.get(name) + irc.reply(msg, '%s: %s (Current value: %r)' % (name, help, value)) + return + try: + self.configurables.set(name, value, channel) + irc.reply(msg, conf.replySuccess) + except Exception, e: + irc.error(msg, debug.exnToString(e)) + class ToggleDictionary(object):