mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-11 20:52:42 +01:00
Factored out the args component of wrap into its own function, so it can be used in more situations.
This commit is contained in:
parent
2e07e23fd5
commit
7558a437e1
118
src/commands.py
118
src/commands.py
@ -301,17 +301,16 @@ argWrappers = ircutils.IrcDict({
|
|||||||
'regexpReplacer': getReplacer,
|
'regexpReplacer': getReplacer,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
def args(irc,msg,args, required=[], optional=[], getopts=None, noExtra=False):
|
||||||
_wrappers = wrappers # Used below so we can use a keyword argument "wrappers".
|
starArgs = []
|
||||||
def wrap(f, required=[], optional=[],
|
req = required[:]
|
||||||
wrappers=None, getopts=None, noExtra=False):
|
opt = optional[:]
|
||||||
if getopts is not None:
|
if getopts is not None:
|
||||||
getoptL = []
|
getoptL = []
|
||||||
for (key, value) in getopts.iteritems():
|
for (key, value) in getopts.iteritems():
|
||||||
if value != '': # value can be None, remember.
|
if value != '': # value can be None, remember.
|
||||||
key += '='
|
key += '='
|
||||||
getoptL.append(key)
|
getoptL.append(key)
|
||||||
|
|
||||||
def getArgWrapper(x):
|
def getArgWrapper(x):
|
||||||
if isinstance(x, tuple):
|
if isinstance(x, tuple):
|
||||||
assert x
|
assert x
|
||||||
@ -325,61 +324,68 @@ def wrap(f, required=[], optional=[],
|
|||||||
return argWrappers[name], args
|
return argWrappers[name], args
|
||||||
else:
|
else:
|
||||||
return lambda irc, msg, args: args.pop(0), args
|
return lambda irc, msg, args: args.pop(0), args
|
||||||
|
def getConversion(name):
|
||||||
|
(converter, convertArgs) = getArgWrapper(name)
|
||||||
|
v = converter(irc, msg, args, *convertArgs)
|
||||||
|
return v
|
||||||
|
def callConverter(name):
|
||||||
|
v = getConversion(name)
|
||||||
|
starArgs.append(v)
|
||||||
|
|
||||||
def newf(self, irc, msg, args, **kwargs):
|
# First, we getopt stuff.
|
||||||
starArgs = []
|
if getopts is not None:
|
||||||
req = (required or [])[:]
|
L = []
|
||||||
opt = (optional or [])[:]
|
(optlist, args) = getopt.getopt(args, '', getoptL)
|
||||||
def getConversion(name):
|
for (opt, arg) in optlist:
|
||||||
(converter, convertArgs) = getArgWrapper(name)
|
opt = opt[2:] # Strip --
|
||||||
v = converter(irc, msg, args, *convertArgs)
|
assert opt in getopts
|
||||||
return v
|
if arg is not None:
|
||||||
def callConverter(name):
|
assert getopts[opt] != ''
|
||||||
v = getConversion(name)
|
L.append((opt, getConversion(getopts[opt])))
|
||||||
starArgs.append(v)
|
else:
|
||||||
|
assert getopts[opt] == ''
|
||||||
|
L.append((opt, True))
|
||||||
|
starArgs.append(L)
|
||||||
|
|
||||||
# First, we getopt stuff.
|
# Second, we get out everything but the last argument.
|
||||||
if getopts is not None:
|
try:
|
||||||
L = []
|
while len(req) + len(opt) > 1:
|
||||||
(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:
|
|
||||||
while len(req) + len(opt) > 1:
|
|
||||||
if req:
|
|
||||||
callConverter(req.pop(0))
|
|
||||||
else:
|
|
||||||
assert opt
|
|
||||||
callConverter(opt.pop(0))
|
|
||||||
# Third, if there is a remaining required or optional argument
|
|
||||||
# (there's a possibility that there were no required or optional
|
|
||||||
# arguments) then we join the remaining args and work convert that.
|
|
||||||
if req or opt:
|
|
||||||
rest = ' '.join(args)
|
|
||||||
args = [rest]
|
|
||||||
if required:
|
|
||||||
converterName = req.pop(0)
|
|
||||||
else:
|
|
||||||
converterName = opt.pop(0)
|
|
||||||
callConverter(converterName)
|
|
||||||
except IndexError:
|
|
||||||
if req:
|
if req:
|
||||||
raise callbacks.ArgumentError
|
callConverter(req.pop(0))
|
||||||
while opt:
|
else:
|
||||||
del opt[-1]
|
assert opt
|
||||||
starArgs.append('')
|
callConverter(opt.pop(0))
|
||||||
if noExtra and args:
|
# Third, if there is a remaining required or optional argument
|
||||||
|
# (there's a possibility that there were no required or optional
|
||||||
|
# arguments) then we join the remaining args and work convert that.
|
||||||
|
if req or opt:
|
||||||
|
rest = ' '.join(args)
|
||||||
|
args = [rest]
|
||||||
|
if required:
|
||||||
|
converterName = req.pop(0)
|
||||||
|
else:
|
||||||
|
converterName = opt.pop(0)
|
||||||
|
callConverter(converterName)
|
||||||
|
except IndexError:
|
||||||
|
if req:
|
||||||
raise callbacks.ArgumentError
|
raise callbacks.ArgumentError
|
||||||
|
while opt:
|
||||||
|
del opt[-1]
|
||||||
|
starArgs.append('')
|
||||||
|
if noExtra and args:
|
||||||
|
raise callbacks.ArgumentError
|
||||||
|
return starArgs
|
||||||
|
|
||||||
|
# These are used below, but we need to rename them so their names aren't
|
||||||
|
# shadowed by our locals.
|
||||||
|
_args = args
|
||||||
|
_wrappers = wrappers
|
||||||
|
def wrap(f, required=[], optional=[],
|
||||||
|
wrappers=None, getopts=None, noExtra=False):
|
||||||
|
def newf(self, irc, msg, args, **kwargs):
|
||||||
|
starArgs = _args(irc, msg, args,
|
||||||
|
getopts=getopts, noExtra=noExtra,
|
||||||
|
required=required, optional=optional)
|
||||||
f(self, irc, msg, args, *starArgs, **kwargs)
|
f(self, irc, msg, args, *starArgs, **kwargs)
|
||||||
|
|
||||||
if wrappers is not None:
|
if wrappers is not None:
|
||||||
|
Loading…
Reference in New Issue
Block a user