diff --git a/plugins/Topic.py b/plugins/Topic.py index 7a85242f7..f5cb11ab8 100644 --- a/plugins/Topic.py +++ b/plugins/Topic.py @@ -44,12 +44,12 @@ import random import supybot.conf as conf import supybot.utils as utils import supybot.ircdb as ircdb +from supybot.commands import * import supybot.ircmsgs as ircmsgs import supybot.plugins as plugins import supybot.ircutils as ircutils import supybot.registry as registry import supybot.callbacks as callbacks -from supybot.commands import wrap, addWrapper, inChannel, getChannel class TopicFormat(registry.String): "Value must include $topic, otherwise the actual topic would be left out." @@ -82,8 +82,8 @@ conf.registerChannelValue(conf.supybot.plugins.Topic.undo, 'max', def canChangeTopic(irc, msg, args, state): assert not state.channel - getChannel(irc, msg, args, state) - inChannel(irc, msg, args, state) + callConverter('channel', irc, msg, args, state) + callConverter('inChannel', irc, msg, args, state) if state.channel not in irc.state.channels: irc.error('I\'m not currently in %s.' % state.channel, Raise=True) c = irc.state.channels[state.channel] @@ -128,9 +128,9 @@ def getTopicNumber(irc, msg, args, state): n += len(topics) state.args.append(n) -addWrapper('topic', getTopic) -addWrapper('topicNumber', getTopicNumber) -addWrapper('canChangeTopic', canChangeTopic) +addConverter('topic', getTopic) +addConverter('topicNumber', getTopicNumber) +addConverter('canChangeTopic', canChangeTopic) def splitTopic(topic, separator): return filter(None, topic.split(separator)) diff --git a/src/commands.py b/src/commands.py index f044c91f7..77ecac6a8 100644 --- a/src/commands.py +++ b/src/commands.py @@ -188,9 +188,8 @@ def getNonInt(irc, msg, args, state, type='non-integer value'): def getFloat(irc, msg, args, state): try: - x = float(args[0]) + state.args.append(float(args[0])) del args[0] - return x except ValueError: irc.errorInvalid('floating point number', args[0]) @@ -498,9 +497,15 @@ wrappers = ircutils.IrcDict({ 'checkChannelCapability': checkChannelCapability, }) -def addWrapper(name, wrapper): +def addConverter(name, wrapper): wrappers[name] = wrapper +def getConverter(name): + return wrappers[name] + +def callConverter(name, irc, msg, args, state, *L): + getConverter(name)(irc, msg, args, state, *L) + class State(object): def __init__(self, name=None, logger=None): if logger is None: @@ -512,6 +517,23 @@ class State(object): self.getopts = [] self.channel = None +class context(object): + def __init__(self, spec): + self.args = () + if isinstance(spec, tuple): + assert spec, 'tuple spec must not be empty.' + self.args = spec[1:] + self.converter = getConverter(spec[0]) + elif spec is None: + self.converter = getConverter('anything') + else: + assert isinstance(spec, basestring) + self.args = () + self.converter = getConverter(spec) + + def __call__(self, irc, msg, args, state): + self.converter(irc, msg, args, state, *self.args) + # getopts: None means "no conversion", '' means "takes no argument" def args(irc,msg,args, types=[], state=None, getopts=None, allowExtra=False, requireExtra=False, combineRest=True): @@ -659,4 +681,6 @@ def wrap(f, *argsArgs, **argsKwargs): return newf +__all__ = ['wrap', 'args', + 'getConverter', 'addConverter', 'callConverter'] # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: