move hooks into python

This commit is contained in:
Michael Davidsaver
2013-04-14 12:01:56 -04:00
parent 4d2c3c4fd3
commit 1e34cf9b76
2 changed files with 42 additions and 57 deletions

View File

@@ -1,5 +1,8 @@
from __future__ import print_function
import traceback
from collections import defaultdict
try:
import _dbapi
except ImportError:
@@ -13,12 +16,16 @@ __all__ = [
hooknames = _dbapi._hooks.keys()
_revnames = dict([(v,k) for k,v in _dbapi._hooks.iteritems()])
_hooktable = defaultdict(list)
def addHook(state, func):
"""addHook("stats", funcion)
Add callable to IOC start sequence.
Callables are run in the reverse of the order in
which they were added.
Callables are run in the order in
which they were added (except for 'AtIocExit').
>>> def show():
... print 'State Occurred'
@@ -28,13 +35,7 @@ def addHook(state, func):
for cleanup actions during IOC shutdown.
"""
sid = _dbapi._hooks[state]
try:
slist = _dbapi._hooktable[sid]
except KeyError:
slist = []
_dbapi._hooktable[sid] = slist
slist.append(func)
_hooktable[sid].append(func)
def debugHooks():
@@ -44,3 +45,16 @@ def debugHooks():
def _showstate(state=h):
print('Reached state',state)
addHook(h, _showstate)
def _runhook(sid):
name = _revnames[sid]
pop = -1 if name=='AtIocExit' else 0
fns = _hooktable.get(sid)
if fns is not None:
while len(fns)>0:
fn = fns.pop(pop)
try:
fn()
except:
print("Error running",name,"hook",fn)
traceback.print_exc()