New syntax for wrap(). Now supports being used as a Python decorator with arguments.

For example, in Admin, "join = wrap(join, ['validChannel', additional('something')])" could become "@wrap(['validChannel', additional('something')])".
This commit is contained in:
Valentin Lorentz 2013-05-20 17:40:04 +02:00
parent b887a97be2
commit 66025cf7e4
1 changed files with 14 additions and 1 deletions

View File

@ -1038,7 +1038,7 @@ class Spec(object):
raise callbacks.ArgumentError
return state
def wrap(f, specList=[], name=None, **kw):
def _wrap(f, specList=[], name=None, **kw):
name = name or f.func_name
spec = Spec(specList, **kw)
def newf(self, irc, msg, args, **kwargs):
@ -1059,6 +1059,19 @@ def wrap(f, specList=[], name=None, **kw):
'function ;)')
raise
return utils.python.changeFunctionName(newf, name, f.__doc__)
def wrap(f, *args, **kwargs):
if callable(f):
# Old-style call OR decorator syntax with no converter.
# f is the command.
return _wrap(f, *args, **kwargs)
else:
# Call with the Python decorator syntax
assert isinstance(f, list) or isinstance(f, tuple)
specList = f
def decorator(f):
return _wrap(f, specList, *args, **kwargs)
return decorator
wrap.__doc__ = """Useful wrapper for plugin commands.
Valid converters are: %s.