From 85972db5b182f4eb035b44e75f50e01ab53dd521 Mon Sep 17 00:00:00 2001 From: "Vgr E. Barry" Date: Tue, 25 Oct 2016 21:25:33 -0400 Subject: [PATCH] Update Event to allow arbitrary keyword arguments Comments are on the same line as the possibly-failing line; if it fails, the traceback will include that line (so it will be obvious what the error is without looking at the source). --- src/events.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/events.py b/src/events.py index 7da0c8f..89bc930 100644 --- a/src/events.py +++ b/src/events.py @@ -15,18 +15,22 @@ def remove_listener(event, callback, priority = 5): EVENT_CALLBACKS[event].remove((priority, callback)) class Event: - def __init__(self, name, data, **kwargs): + def __init__(*args, **kwargs): + # We do this so that e.g. 'data' can be given as a param + self, name, data = args # If this raises, there are too few/many args + self.stop_processing = False self.prevent_default = False self.name = name self.data = data self.params = SimpleNamespace(**kwargs) - def dispatch(self, *args, **kwargs): + def dispatch(*args, **kwargs): + self = args[0] # If this fails, you forgot to do Event(stuff) first self.stop_processing = False self.prevent_default = False for item in list(EVENT_CALLBACKS[self.name]): - item[1](self, *args, **kwargs) + item[1](*args, **kwargs) if self.stop_processing: break