From 570ef6f10985a36609e97e3e0a568c6809cedc29 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 27 Sep 2018 01:52:35 +0200 Subject: [PATCH] Replaced EventAction with Procedure to allow more flexibility in event hooks and cronjobs --- platypush/cron/scheduler.py | 14 +++++--------- platypush/event/hook.py | 15 ++++++--------- tests/test_event_parse.py | 2 +- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/platypush/cron/scheduler.py b/platypush/cron/scheduler.py index 2bf46d96..5b67b7da 100644 --- a/platypush/cron/scheduler.py +++ b/platypush/cron/scheduler.py @@ -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,20 +15,16 @@ 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) - logger.info('Response from cronjob {}: {}'.format(self.name, response)) + response = self.actions.execute(_async=False, **context) + logger.info('Response from cronjob {}: {}'.format(self.name, response)) def should_run(self): diff --git a/platypush/event/hook.py b/platypush/event/hook.py index a634e5c1..ceddf09a 100644 --- a/platypush/event/hook.py +++ b/platypush/event/hook.py @@ -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: diff --git a/tests/test_event_parse.py b/tests/test_event_parse.py index 42c67f3e..e4bf2f62 100644 --- a/tests/test_event_parse.py +++ b/tests/test_event_parse.py @@ -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):