Added a few more types.

This commit is contained in:
Jeremy Fincher 2004-09-30 07:43:24 +00:00
parent 8bcb9f2f21
commit a2e43fe367

View File

@ -162,6 +162,21 @@ def getInt(irc, msg, args, state, type='integer', p=None):
except ValueError: except ValueError:
irc.errorInvalid(type, args[0]) irc.errorInvalid(type, args[0])
def getNonInt(irc, msg, args, state, type='non-integer value'):
try:
i = _int(args[0])
irc.errorInvalid(type, args[0])
except ValueError:
state.args.append(args.pop(0))
def getFloat(irc, msg, args, state):
try:
x = float(args[0])
del args[0]
return x
except ValueError:
irc.errorInvalid('floating point number', args[0])
def getPositiveInt(irc, msg, args, state, *L): def getPositiveInt(irc, msg, args, state, *L):
getInt(irc, msg, args, state, getInt(irc, msg, args, state,
p=lambda i: i<=0, type='positive integer', *L) p=lambda i: i<=0, type='positive integer', *L)
@ -288,6 +303,12 @@ def getChannel(irc, msg, args, state):
state.channel = channel state.channel = channel
state.args.append(channel) state.args.append(channel)
def getChannelOrNone(irc, msg, args, state):
try:
getChannel(irc, msg, args, state)
except callbacks.ArgumentError:
state.args.append(None)
def checkChannelCapability(irc, msg, args, state, cap): def checkChannelCapability(irc, msg, args, state, cap):
assert state.channel, \ assert state.channel, \
'You must use a channel arg before you use checkChannelCapability.' 'You must use a channel arg before you use checkChannelCapability.'
@ -338,6 +359,8 @@ def anything(irc, msg, args, state):
wrappers = ircutils.IrcDict({ wrappers = ircutils.IrcDict({
'id': getId, 'id': getId,
'int': getInt, 'int': getInt,
'float': getFloat,
'nonInt': getNonInt,
'positiveInt': getPositiveInt, 'positiveInt': getPositiveInt,
'nonNegativeInt': getNonNegativeInt, 'nonNegativeInt': getNonNegativeInt,
'haveOp': getHaveOp, 'haveOp': getHaveOp,
@ -408,6 +431,7 @@ def args(irc,msg,args, types=[], state=None,
optional = True optional = True
enforce = False enforce = False
name = name[:-1] name = name[:-1]
default = ''
wrapper = wrappers[name] wrapper = wrappers[name]
if optional and specArgs: if optional and specArgs:
# First arg is default. # First arg is default.
@ -420,14 +444,14 @@ def args(irc,msg,args, types=[], state=None,
except (callbacks.Error, ValueError, callbacks.ArgumentError), e: except (callbacks.Error, ValueError, callbacks.ArgumentError), e:
state.log.debug('%r when calling wrapper.', utils.exnToString(e)) state.log.debug('%r when calling wrapper.', utils.exnToString(e))
if not enforce: if not enforce:
state.args.append('') state.args.append(default)
else: else:
state.log.debug('Re-raising %s because of enforce.', e) state.log.debug('Re-raising %s because of enforce.', e)
raise raise
except IndexError, e: except IndexError, e:
state.log.debug('%r when calling wrapper.', utils.exnToString(e)) state.log.debug('%r when calling wrapper.', utils.exnToString(e))
if optional: if optional:
state.args.append('') state.args.append(default)
else: else:
state.log.debug('Raising ArgumentError because of ' state.log.debug('Raising ArgumentError because of '
'non-optional args.') 'non-optional args.')