From 3986549326c46649c5f194581825c00267043f52 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 31 May 2024 02:50:00 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20proper=20support=20for?= =?UTF-8?q?=20event=20package=20alias=20`platypush.events`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Even though `platypush.events` is just a symlink to `platypush.message.event`, imports from those two modules will be treated as different imports, thus hook conditions build on `platypush.events` imports will never match. --- platypush/event/hook.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/platypush/event/hook.py b/platypush/event/hook.py index 371557277a..52defbce81 100644 --- a/platypush/event/hook.py +++ b/platypush/event/hook.py @@ -1,5 +1,6 @@ import copy import json +import importlib import logging import threading from functools import wraps @@ -46,8 +47,7 @@ class EventCondition: kwargs -- Fields rules as a key-value (e.g. source_button=btn_id or recognized_phrase='Your phrase') """ - - self.type = type or Event.__class__ # type: ignore + self.type = self._get_event_type(type) self.args = {} self.parsed_args = {} self.priority = priority @@ -55,6 +55,23 @@ class EventCondition: for key, value in kwargs.items(): self.args[key] = value + @staticmethod + def _get_event_type(type: Optional[Type[Event]] = None) -> Type[Event]: + if not type: + return Event + + # The package alias `platypush.events` -> `platypush.message.event` is + # supported + if type.__module__.startswith('platypush.events'): + module = importlib.import_module( + 'platypush.message.event' + type.__module__[len('platypush.events') :] + ) + + type = getattr(module, type.__name__) + assert type, f'Invalid event type: {type}' + + return type + @classmethod def build(cls, rule): """