Replaced EventAction with Procedure to allow more flexibility in event hooks and cronjobs

This commit is contained in:
Fabio Manganiello 2018-09-27 01:52:35 +02:00
parent f3b1bdb397
commit 570ef6f109
3 changed files with 12 additions and 19 deletions

View file

@ -5,7 +5,7 @@ import time
from threading import Thread
from platypush.event.hook import EventAction
from platypush.procedure import Procedure
logger = logging.getLogger(__name__)
@ -15,19 +15,15 @@ class Cronjob(Thread):
super().__init__()
self.cron_expression = cron_expression
self.name = name
self.actions = []
for action in actions:
self.actions.append(EventAction.build(action))
self.actions = Procedure.build(name=name+'__Cron', _async=False,
requests=actions)
def run(self):
logger.info('Running cronjob {}'.format(self.name))
response = None
context = {}
for action in self.actions:
response = action.execute(_async=False, **context)
response = self.actions.execute(_async=False, **context)
logger.info('Response from cronjob {}: {}'.format(self.name, response))

View file

@ -6,6 +6,7 @@ import re
from platypush.config import Config
from platypush.message.event import Event, EventMatchResult
from platypush.message.request import Request
from platypush.procedure import Procedure
from platypush.utils import get_event_class_by_type
logger = logging.getLogger(__name__)
@ -99,8 +100,8 @@ class EventHook(object):
one or multiple actions to be executed """
def __init__(self, name, priority=None, condition=None, actions=[]):
""" Construtor. Takes a name, a EventCondition object and a list of
EventAction objects as input. It may also have a priority attached
""" Construtor. Takes a name, a EventCondition object and an event action
procedure as input. It may also have a priority attached
as a positive number. If multiple hooks match against an event,
only the ones that have either the maximum match score or the
maximum pre-configured priority will be run. """
@ -132,6 +133,7 @@ class EventHook(object):
else:
actions = [hook['then']]
actions = Procedure.build(name=name+'__Hook', requests=actions, _async=False)
return cls(name=name, condition=condition, actions=actions, priority=priority)
@ -151,13 +153,8 @@ class EventHook(object):
if result.is_match:
logger.info('Running hook {} triggered by an event'.format(self.name))
for action in self.actions:
a = EventAction.build(action)
if token:
a.token = token
a.execute(event=event, **result.parsed_args)
# TODO here event should actually be event.args
self.actions.execute(event=event, **result.parsed_args)
# vim:sw=4:ts=4:et:

View file

@ -2,7 +2,7 @@ from .context import platypush
import unittest
from platypush.event.hook import EventHook, EventCondition, EventAction
from platypush.event.hook import EventCondition
from platypush.message.event.ping import PingEvent
class TestEventParse(unittest.TestCase):