Updated, added getPlugin, getSomething, validChannel, etc.

This commit is contained in:
Jeremy Fincher 2004-09-21 18:36:40 +00:00
parent 58cb831448
commit 8f97cccb1a

View File

@ -37,6 +37,8 @@ __revision__ = "$Id$"
import supybot.fix as fix import supybot.fix as fix
import getopt
import time import time
import types import types
import threading import threading
@ -186,6 +188,13 @@ def channelDb(irc, msg, args, **kwargs):
else: else:
return channel(irc, msg, args, **kwargs) return channel(irc, msg, args, **kwargs)
def validChannel(irc, msg, args):
s = args.pop(0)
if ircutils.isChannel(s):
return s
else:
irc.errorInvalid('channel', s, Raise=True)
def getHostmask(irc, msg, args): def getHostmask(irc, msg, args):
if ircutils.isUserHostmask(args[0]): if ircutils.isUserHostmask(args[0]):
return args.pop(0) return args.pop(0)
@ -260,23 +269,49 @@ def channel(irc, msg, args, cap=None):
def getLowered(irc, msg, args): def getLowered(irc, msg, args):
return ircutils.toLower(args.pop(0)) return ircutils.toLower(args.pop(0))
def getSomething(irc, msg, args):
s = args.pop(0)
if not s:
# XXX Better reply? How?
irc.error('You must not give the empty string as an argument.',
Raise=True)
return s
def getPlugin(irc, msg, args, requirePresent=False):
s = args.pop(0)
cb = irc.getCallback(s)
if requirePresent and cb is None:
irc.errorInvalid('plugin', s, Raise=True)
return cb
argWrappers = ircutils.IrcDict({ argWrappers = ircutils.IrcDict({
'id': getId, 'id': getId,
'int': getInt, 'int': getInt,
'nick': getNick, 'nick': getNick,
'channel': channel, 'channel': channel,
'plugin': getPlugin,
'lowered': getLowered, 'lowered': getLowered,
'something': getSomething,
'channelDb': channelDb, 'channelDb': channelDb,
'hostmask': getHostmask, 'hostmask': getHostmask,
'user': getUser, 'user': getUser,
'otherUser': getOtherUser, 'otherUser': getOtherUser,
'regexpMatcher': getMatcher, 'regexpMatcher': getMatcher,
'validChannel': validChannel,
'regexpReplacer': getReplacer, 'regexpReplacer': getReplacer,
}) })
_wrappers = wrappers # Used below so we can use a keyword argument "wrappers". _wrappers = wrappers # Used below so we can use a keyword argument "wrappers".
def wrap(f, required=[], optional=[], wrappers=None, noExtra=False): def wrap(f, required=[], optional=[],
wrappers=None, getopts=None, noExtra=False):
if getopts is not None:
getoptL = []
for (key, value) in getopts.iteritems():
if value != '': # value can be None, remember.
key += '='
getoptL.append(key)
def getArgWrapper(x): def getArgWrapper(x):
if isinstance(x, tuple): if isinstance(x, tuple):
assert x assert x
@ -295,24 +330,43 @@ def wrap(f, required=[], optional=[], wrappers=None, noExtra=False):
starArgs = [] starArgs = []
req = (required or [])[:] req = (required or [])[:]
opt = (optional or [])[:] opt = (optional or [])[:]
def callConverter(name): def getConversion(name):
(converter, convertArgs) = getArgWrapper(name) (converter, convertArgs) = getArgWrapper(name)
v = converter(irc, msg, args, *convertArgs) v = converter(irc, msg, args, *convertArgs)
return v
def callConverter(name):
v = getConversion(name)
starArgs.append(v) starArgs.append(v)
# First, we getopt stuff.
if getopts is not None:
L = []
(optlist, args) = getopt.getopt(args, '', getoptL)
for (opt, arg) in optlist:
opt = opt[2:] # Strip --
assert opt in getopts
if arg is not None:
assert getopts[opt] != ''
L.append((opt, getConversion(getopts[opt])))
else:
assert getopts[opt] == ''
L.append((opt, True))
starArgs.append(L)
# Second, we get out everything but the last argument.
try: try:
# First, we get out everything but the last argument.
while len(req) + len(opt) > 1: while len(req) + len(opt) > 1:
if req: if req:
callConverter(req.pop(0)) callConverter(req.pop(0))
else: else:
assert opt assert opt
callConverter(opt.pop(0)) callConverter(opt.pop(0))
# Second, if there is a remaining required or optional argument # Third, if there is a remaining required or optional argument
# (there's a possibility that there were no required or optional # (there's a possibility that there were no required or optional
# arguments) then we join the remaining args and work convert that. # arguments) then we join the remaining args and work convert that.
if req or opt: if req or opt:
rest = ' '.join(args) rest = ' '.join(args)
args = [rest]
if required: if required:
converterName = req.pop(0) converterName = req.pop(0)
else: else: