From 66025cf7e412790bc8943f169324f923129e92c2 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Mon, 20 May 2013 17:40:04 +0200 Subject: [PATCH] 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')])". --- src/commands.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/commands.py b/src/commands.py index f1f49ff03..b410f84d5 100644 --- a/src/commands.py +++ b/src/commands.py @@ -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.