Support for event filter on functional hook
This commit is contained in:
parent
0dae03551f
commit
d46385687f
1 changed files with 17 additions and 14 deletions
|
@ -165,24 +165,27 @@ class EventHook(object):
|
||||||
threading.Thread(target=_thread_func, name='Event-' + self.name, args=(result,)).start()
|
threading.Thread(target=_thread_func, name='Event-' + self.name, args=(result,)).start()
|
||||||
|
|
||||||
|
|
||||||
def hook(f, event_type=Event, **condition):
|
def hook(event_type=Event, **condition):
|
||||||
f.hook = True
|
def wrapper(f):
|
||||||
f.condition = EventCondition(type=event_type, **condition)
|
f.hook = True
|
||||||
|
f.condition = EventCondition(type=event_type, **condition)
|
||||||
|
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def _execute_hook(*args, **kwargs):
|
def wrapped(*args, **kwargs):
|
||||||
from platypush import Response
|
from platypush import Response
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ret = f(*args, **kwargs)
|
ret = f(*args, **kwargs)
|
||||||
if isinstance(ret, Response):
|
if isinstance(ret, Response):
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
return Response(output=ret)
|
return Response(output=ret)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return Response(errors=[str(e)])
|
return Response(errors=[str(e)])
|
||||||
|
|
||||||
return _execute_hook
|
return wrapped
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
# vim:sw=4:ts=4:et:
|
# vim:sw=4:ts=4:et:
|
||||||
|
|
Loading…
Reference in a new issue