Skip to content

Commit

Permalink
fix bug - checker register duplicate functions to event bus (#181)
Browse files Browse the repository at this point in the history
* fix bug

* rm built-in object
  • Loading branch information
zhaoye authored Jul 11, 2019
1 parent bf58777 commit 8b7c32f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
4 changes: 1 addition & 3 deletions lyrebird/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
from blinker import Signal
import os
from .event import CustomEventReceiver
from .checker import event
from lyrebird import application
from lyrebird.log import get_logger


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
Expand Down
29 changes: 29 additions & 0 deletions lyrebird/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 5 additions & 5 deletions lyrebird/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Event:
"""
Event bus inner class
"""

def __init__(self, event_id, channel, message):
self.id = event_id
self.channel = channel
Expand All @@ -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):
"""
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -178,6 +177,7 @@ class CustomEventReceiver:
def on_message(data):
pass
"""

def __init__(self):
self.listeners = []

Expand All @@ -202,5 +202,5 @@ def issue(self, title, message):
notice = {
"title": title,
"message": message
}
}
application.server['event'].publish('notice', notice)
2 changes: 1 addition & 1 deletion lyrebird/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
IVERSION = (1, 6, 14)
IVERSION = (1, 6, 15)
VERSION = ".".join(str(i) for i in IVERSION)
LYREBIRD = "Lyrebird " + VERSION

0 comments on commit 8b7c32f

Please sign in to comment.