From 8b7c32fae06427f23b5fe9fac953d7639a687292 Mon Sep 17 00:00:00 2001 From: zhaoye Date: Thu, 11 Jul 2019 17:30:28 +0800 Subject: [PATCH] fix bug - checker register duplicate functions to event bus (#181) * fix bug * rm built-in object --- lyrebird/__init__.py | 4 +--- lyrebird/checker.py | 29 +++++++++++++++++++++++++++++ lyrebird/event.py | 10 +++++----- lyrebird/version.py | 2 +- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/lyrebird/__init__.py b/lyrebird/__init__.py index 92e908ea8..04711f28f 100644 --- a/lyrebird/__init__.py +++ b/lyrebird/__init__.py @@ -6,6 +6,7 @@ from blinker import Signal import os from .event import CustomEventReceiver +from .checker import event from lyrebird import application from lyrebird.log import get_logger @@ -13,9 +14,6 @@ APPLICATION_CONF_DIR = os.path.join(os.path.expanduser('~'), '.lyrebird') -event = CustomEventReceiver() - - def start_background_task(target, *args, **kwargs): """Start a background task in a new thread diff --git a/lyrebird/checker.py b/lyrebird/checker.py index fd83e4835..3e25d0fb2 100644 --- a/lyrebird/checker.py +++ b/lyrebird/checker.py @@ -12,6 +12,28 @@ logger = log.get_logger() +registered_func_array = [] + + +class CheckerEventHandler: + + def __call__(self, channel, *args, **kw): + def func(origin_func): + registered_func_array.append([channel, origin_func]) + return origin_func + return func + + def issue(self, title, message): + notice = { + "title": title, + "message": message + } + application.server['event'].publish('notice', notice) + + +event = CheckerEventHandler() + + class LyrebirdCheckerServer(ThreadServer): def __init__(self): super().__init__() @@ -133,11 +155,18 @@ def update(self, val): self._update = val def activate(self): + global registered_func_array self._module = script_module = self.load_class(self.path) event_proxy = getattr(script_module, 'event') if isinstance(event_proxy, CustomEventReceiver): self._event_receiver = event_proxy event_proxy.register(context.application.event_bus) + elif isinstance(event_proxy, CheckerEventHandler): + self._event_receiver = CustomEventReceiver() + for registered_func in registered_func_array: + self._event_receiver.listeners.append(dict(channel=registered_func[0], func=registered_func[1])) + self._event_receiver.register(context.application.event_bus) + registered_func_array = [] self.activated = True def deactivate(self): diff --git a/lyrebird/event.py b/lyrebird/event.py index 5a6c42ebd..28a3004d1 100644 --- a/lyrebird/event.py +++ b/lyrebird/event.py @@ -23,6 +23,7 @@ class Event: """ Event bus inner class """ + def __init__(self, event_id, channel, message): self.id = event_id self.channel = channel @@ -40,7 +41,6 @@ def __init__(self): self.any_channel = [] self.broadcast_executor = ThreadPoolExecutor(thread_name_prefix='event-broadcast-') - def broadcast_handler(self, callback_fn, event, args, kwargs): """ @@ -49,7 +49,7 @@ def broadcast_handler(self, callback_fn, event, args, kwargs): # Check func_sig = inspect.signature(callback_fn) func_parameters = list(func_sig.parameters.values()) - if len(func_parameters)<1 or func_parameters[0].default!=inspect._empty: + if len(func_parameters) < 1 or func_parameters[0].default != inspect._empty: logger.error(f'Event callback function [{callback_fn.__name__}] need a argument for receiving event object') return @@ -108,7 +108,7 @@ def publish(self, channel, message, state=False, *args, **kwargs): # Make sure event is dict if not isinstance(message, dict): # Plugins send a array list as message, then set this message to raw property - _msg = { 'raw': message } + _msg = {'raw': message} message = _msg message['channel'] = channel @@ -150,7 +150,6 @@ def subscribe(self, channel, callback_fn, *args, **kwargs): callback_fn_list = self.pubsub_channels.setdefault(channel, []) callback_fn_list.append([callback_fn, args, kwargs]) - def unsubscribe(self, channel, target_callback_fn, *args, **kwargs): """ Unsubscribe callback function from channel @@ -178,6 +177,7 @@ class CustomEventReceiver: def on_message(data): pass """ + def __init__(self): self.listeners = [] @@ -202,5 +202,5 @@ def issue(self, title, message): notice = { "title": title, "message": message - } + } application.server['event'].publish('notice', notice) diff --git a/lyrebird/version.py b/lyrebird/version.py index d4f86dc44..bf1b5dad0 100644 --- a/lyrebird/version.py +++ b/lyrebird/version.py @@ -1,3 +1,3 @@ -IVERSION = (1, 6, 14) +IVERSION = (1, 6, 15) VERSION = ".".join(str(i) for i in IVERSION) LYREBIRD = "Lyrebird " + VERSION