Added Configurable class and changed Ebay to use it (as an example).

This commit is contained in:
Jeremy Fincher 2003-11-07 20:11:37 +00:00
parent 901a225619
commit 69adce4a57
2 changed files with 50 additions and 17 deletions

View File

@ -64,18 +64,18 @@ def configure(onStart, afterConnect, advanced):
if yn('Do you want the Ebay snarfer enabled by default?') == 'n': if yn('Do you want the Ebay snarfer enabled by default?') == 'n':
onStart.append('Ebay toggle auction off') 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 Module for eBay stuff. Currently contains a URL snarfer and a command to
get info about an auction. get info about an auction.
""" """
threaded = True threaded = True
regexps = ['ebaySnarfer'] regexps = ['ebaySnarfer']
toggles = plugins.ToggleDictionary({'auction' : True}) configurables = plugins.ConfigurableDictionary(
[('snarfer', utils.safeEval, True, 'Controls the auction snarfer.')]
)
def __init__(self): def __init__(self):
callbacks.PrivmsgCommandAndRegexp.__init__(self) callbacks.PrivmsgCommandAndRegexp.__init__(self)
plugins.Toggleable.__init__(self)
_reopts = re.I | re.S _reopts = re.I | re.S
_info = re.compile(r'<title>eBay item (\d+) \([^)]+\) - ([^<]+)</title>', _info = re.compile(r'<title>eBay item (\d+) \([^)]+\) - ([^<]+)</title>',
@ -119,7 +119,7 @@ class Ebay(callbacks.PrivmsgCommandAndRegexp, plugins.Toggleable):
def ebaySnarfer(self, irc, msg, match): def ebaySnarfer(self, irc, msg, match):
r"http://cgi\.ebay\.(?:com(?:.au)?|ca|co.uk)/(?:.*?/)?(?:ws/)?"\ r"http://cgi\.ebay\.(?:com(?:.au)?|ca|co.uk)/(?:.*?/)?(?:ws/)?"\
r"eBayISAPI\.dll\?ViewItem(?:&item=\d+|&category=\d+)+" 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 return
url = match.group(0) url = match.group(0)
#debug.printf(url) #debug.printf(url)

View File

@ -212,32 +212,65 @@ class ConfigurableDictionary(object):
self.channels[None][name] = default self.channels[None][name] = default
def get(self, name, channel=None): def get(self, name, channel=None):
try:
return self.channels[channel][name] return self.channels[channel][name]
except KeyError:
return self.channels[None][name]
def set(self, name, value, channel=None): 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. # XXX: Make persistent.
class Configurable(object): class Configurable(object):
"""A mixin class to provide a "config" command that can be consistent """A mixin class to provide a "config" command that can be consistent
across all plugins, in order to unify the configuration for each plugin. across all plugins, in order to unify the configuration for each plugin.
"""
def __init__(self):
self.configurables = ConfigurableDictionary(self.configurables)
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 config(self, irc, msg, args): def config(self, irc, msg, args):
"""[<channel>] <name> [<value>] """[<channel>] [<name>] [<value>]
Sets the config variable <name> to <value> on <channel>. If <value> Sets the value of config variable <name> to <value> on <channel>. If
isn't given, returns the current value of <name> on <channel>. <name> is given but <value> is not, returns the help and current value
<channel> is only necessary if the message isn't sent in the channel for <name>. If neither <name> nor <value> is given, returns the valid
itself. config variables for this plugin. <channel> is only necessary if the
message isn't sent in the channel itself.
""" """
try:
channel = privmsgs.getChannel(msg, args) channel = privmsgs.getChannel(msg, args)
except callbacks.ArgumentError:
channel = None
(name, value) = privmsgs.getArgs(args, needed=0, optional=2) (name, value) = privmsgs.getArgs(args, needed=0, optional=2)
if not name: 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): class ToggleDictionary(object):