-
Notifications
You must be signed in to change notification settings - Fork 3
/
hook.py
executable file
·65 lines (54 loc) · 2.15 KB
/
hook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import imp
import sys
from misc import *
_GreenPool = None
_spawn_raw = None
# if 'gevent' in sys.modules.keys():
# import gevent, gevent.pool
# _GreenPool = gevent.pool.Pool
# _spawn_raw = gevent.spawn_raw
# gevent.Greenlet.wait = lambda self: self.get(True)
# elif 'eventlet' in sys.modules.keys():
import eventlet
_GreenPool = eventlet.GreenPool
_GreenPool.spawn_raw = _GreenPool.spawn_n
_GreenPool.join = _GreenPool.waitall
_spawn_raw = eventlet.spawn_n
# else:
# raise RuntimeError('Engine not loaded.')
_hooks = {}
_modules = {}
def _create_hook(name): # internal shit just to make sure the crap is initialised
if not _hooks.get(name, None):
_hooks[name] = []
def import_plugin(modname): # convenience function for plugins that depend on other plugins
if _modules.get(modname, None) is None:
f, p, d = imp.find_module(modname, ['plugins'])
_modules[modname] = imp.load_module('plugins.' + modname, f, p, d)
return _modules[modname]
def unref_plugin(modname): # removes references to a plugin
del _modules[modname]
def register(name, fn): # register a hook (called by plugin)
_create_hook(name)
if not fn in _hooks[name]:
_hooks[name].append(fn)
def unregister(name, fn): # unregister a hook (called by plugin)
_create_hook(name)
if fn in _hooks[name]:
_hooks[name].remove(fn)
def install(modname): # install a module
import_plugin(modname).install()
def uninstall(modname): # uninstall a module
import_plugin(modname).uninstall()
def call_wait(name, *args, **kwargs): # call all hooks registered under "name" and wait for results
_create_hook(name)
gp = _GreenPool()
greenlets = [ gp.spawn(i, *args, **kwargs) for i in _hooks[name] ]
gp.join()
results = [ i.wait() for i in greenlets ] # a list of return values. probably True or False to tell
# caller to abandon it's own processor (a la JavaScript)
return results
def call_nowait(name, *args, **kwargs): # call all hooks registered under "name" and return immediately
_create_hook(name)
for i in _hooks[name]:
_spawn_raw(i, *args, **kwargs)