🐛 Fixed proper support for event package alias platypush.events.

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.
This commit is contained in:
Fabio Manganiello 2024-05-31 02:50:00 +02:00
parent c18768e61f
commit 67478e7ca1

View file

@ -1,5 +1,6 @@
import copy import copy
import json import json
import importlib
import logging import logging
import threading import threading
from functools import wraps from functools import wraps
@ -46,8 +47,7 @@ class EventCondition:
kwargs -- Fields rules as a key-value (e.g. source_button=btn_id kwargs -- Fields rules as a key-value (e.g. source_button=btn_id
or recognized_phrase='Your phrase') or recognized_phrase='Your phrase')
""" """
self.type = self._get_event_type(type)
self.type = type or Event.__class__ # type: ignore
self.args = {} self.args = {}
self.parsed_args = {} self.parsed_args = {}
self.priority = priority self.priority = priority
@ -55,6 +55,23 @@ class EventCondition:
for key, value in kwargs.items(): for key, value in kwargs.items():
self.args[key] = value 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 @classmethod
def build(cls, rule): def build(cls, rule):
""" """