2010-02-02 04:42:34 +00:00
|
|
|
import inspect
|
2010-03-12 00:19:36 +00:00
|
|
|
import re
|
2009-04-19 11:18:27 +00:00
|
|
|
|
2010-03-01 02:32:41 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
def _hook_add(func, add, name=''):
|
|
|
|
if not hasattr(func, '_hook'):
|
|
|
|
func._hook = []
|
|
|
|
func._hook.append(add)
|
2009-03-16 04:30:46 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
if not hasattr(func, '_filename'):
|
|
|
|
func._filename = func.func_code.co_filename
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
if not hasattr(func, '_args'):
|
2010-02-02 04:42:34 +00:00
|
|
|
argspec = inspect.getargspec(func)
|
|
|
|
if name:
|
|
|
|
n_args = len(argspec.args)
|
|
|
|
if argspec.defaults:
|
|
|
|
n_args -= len(argspec.defaults)
|
|
|
|
if argspec.keywords:
|
|
|
|
n_args -= 1
|
|
|
|
if argspec.varargs:
|
|
|
|
n_args -= 1
|
|
|
|
if n_args != 1:
|
2010-03-01 02:32:41 +00:00
|
|
|
err = '%ss must take 1 non-keyword argument (%s)' % (name,
|
2014-01-14 21:12:37 +00:00
|
|
|
func.__name__)
|
2010-02-02 04:42:34 +00:00
|
|
|
raise ValueError(err)
|
|
|
|
|
|
|
|
args = []
|
|
|
|
if argspec.defaults:
|
|
|
|
end = bool(argspec.keywords) + bool(argspec.varargs)
|
|
|
|
args.extend(argspec.args[-len(argspec.defaults):
|
|
|
|
end if end else None])
|
|
|
|
if argspec.keywords:
|
2010-03-01 02:32:41 +00:00
|
|
|
args.append(0) # means kwargs present
|
2010-03-11 23:34:54 +00:00
|
|
|
func._args = args
|
2009-03-16 04:30:46 +00:00
|
|
|
|
2010-03-27 08:42:27 +00:00
|
|
|
if not hasattr(func, '_thread'): # does function run in its own thread?
|
2010-03-11 23:34:54 +00:00
|
|
|
func._thread = False
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2010-03-27 08:42:27 +00:00
|
|
|
|
2009-03-16 04:30:46 +00:00
|
|
|
def sieve(func):
|
2010-03-11 23:34:54 +00:00
|
|
|
if func.func_code.co_argcount != 5:
|
2009-04-18 00:57:18 +00:00
|
|
|
raise ValueError(
|
2014-01-14 21:12:37 +00:00
|
|
|
'sieves must take 5 arguments: (bot, input, func, type, args)')
|
2010-03-11 23:34:54 +00:00
|
|
|
_hook_add(func, ['sieve', (func,)])
|
2009-03-16 04:30:46 +00:00
|
|
|
return func
|
|
|
|
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2010-05-07 23:16:44 +00:00
|
|
|
def command(arg=None, **kwargs):
|
2009-03-16 04:30:46 +00:00
|
|
|
args = {}
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2009-03-16 04:30:46 +00:00
|
|
|
def command_wrapper(func):
|
|
|
|
args.setdefault('name', func.func_name)
|
2010-03-11 23:34:54 +00:00
|
|
|
_hook_add(func, ['command', (func, args)], 'command')
|
2009-03-16 04:30:46 +00:00
|
|
|
return func
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
if kwargs or not inspect.isfunction(arg):
|
|
|
|
if arg is not None:
|
|
|
|
args['name'] = arg
|
2009-03-16 04:30:46 +00:00
|
|
|
args.update(kwargs)
|
|
|
|
return command_wrapper
|
|
|
|
else:
|
2010-03-11 23:34:54 +00:00
|
|
|
return command_wrapper(arg)
|
2009-03-16 04:30:46 +00:00
|
|
|
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2009-04-17 21:54:11 +00:00
|
|
|
def event(arg=None, **kwargs):
|
|
|
|
args = kwargs
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2009-03-16 04:30:46 +00:00
|
|
|
def event_wrapper(func):
|
|
|
|
args['name'] = func.func_name
|
2010-03-11 23:34:54 +00:00
|
|
|
args.setdefault('events', ['*'])
|
|
|
|
_hook_add(func, ['event', (func, args)], 'event')
|
2009-03-16 04:30:46 +00:00
|
|
|
return func
|
2009-04-18 00:57:18 +00:00
|
|
|
|
2010-03-11 23:34:54 +00:00
|
|
|
if inspect.isfunction(arg):
|
2009-04-17 21:54:11 +00:00
|
|
|
return event_wrapper(arg, kwargs)
|
2009-03-16 04:30:46 +00:00
|
|
|
else:
|
2009-04-17 21:54:11 +00:00
|
|
|
if arg is not None:
|
|
|
|
args['events'] = arg.split()
|
2009-03-16 04:30:46 +00:00
|
|
|
return event_wrapper
|
2009-11-19 00:39:27 +00:00
|
|
|
|
|
|
|
|
2010-03-13 14:24:19 +00:00
|
|
|
def singlethread(func):
|
2010-03-11 23:34:54 +00:00
|
|
|
func._thread = True
|
2009-11-19 00:39:27 +00:00
|
|
|
return func
|
2010-03-12 00:19:36 +00:00
|
|
|
|
|
|
|
|
2013-01-27 22:40:31 +00:00
|
|
|
def api_key(key):
|
|
|
|
def annotate(func):
|
|
|
|
func._apikey = key
|
|
|
|
return func
|
|
|
|
return annotate
|
|
|
|
|
|
|
|
|
2010-03-12 00:19:36 +00:00
|
|
|
def regex(regex, flags=0, **kwargs):
|
|
|
|
args = kwargs
|
2010-03-27 08:42:27 +00:00
|
|
|
|
2010-03-12 00:19:36 +00:00
|
|
|
def regex_wrapper(func):
|
|
|
|
args['name'] = func.func_name
|
|
|
|
args['regex'] = regex
|
|
|
|
args['re'] = re.compile(regex, flags)
|
|
|
|
_hook_add(func, ['regex', (func, args)], 'regex')
|
|
|
|
return func
|
|
|
|
|
|
|
|
if inspect.isfunction(regex):
|
|
|
|
raise ValueError("regex decorators require a regex to match against")
|
|
|
|
else:
|
|
|
|
return regex_wrapper
|