summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--poezio/events.py89
-rw-r--r--poezio/plugin.py4
-rw-r--r--poezio/plugin_manager.py4
3 files changed, 55 insertions, 42 deletions
diff --git a/poezio/events.py b/poezio/events.py
index 3bfe5156..b34eef32 100644
--- a/poezio/events.py
+++ b/poezio/events.py
@@ -9,6 +9,7 @@ The list of available events is here:
http://poezio.eu/doc/en/plugins.html#_poezio_events
"""
+from collections import OrderedDict
from typing import Callable, Dict, List
@@ -21,49 +22,56 @@ class EventHandler:
"""
def __init__(self):
- self.events = {
- 'highlight': [],
- 'muc_say': [],
- 'muc_say_after': [],
- 'conversation_say': [],
- 'conversation_say_after': [],
- 'private_say': [],
- 'private_say_after': [],
- 'conversation_msg': [],
- 'private_msg': [],
- 'muc_msg': [],
- 'conversation_chatstate': [],
- 'muc_chatstate': [],
- 'private_chatstate': [],
- 'normal_presence': [],
- 'muc_presence': [],
- 'muc_join': [],
- 'joining_muc': [],
- 'changing_nick': [],
- 'muc_kick': [],
- 'muc_nickchange': [],
- 'muc_ban': [],
- 'send_normal_presence': [],
- 'ignored_private': [],
- 'tab_change': [],
- } # type: Dict[str, List[Callable]]
+ events = [
+ 'highlight',
+ 'muc_say',
+ 'muc_say_after',
+ 'conversation_say',
+ 'conversation_say_after',
+ 'private_say',
+ 'private_say_after',
+ 'conversation_msg',
+ 'private_msg',
+ 'muc_msg',
+ 'conversation_chatstate',
+ 'muc_chatstate',
+ 'private_chatstate',
+ 'normal_presence',
+ 'muc_presence',
+ 'muc_join',
+ 'joining_muc',
+ 'changing_nick',
+ 'muc_kick',
+ 'muc_nickchange',
+ 'muc_ban',
+ 'send_normal_presence',
+ 'ignored_private',
+ 'tab_change',
+ ]
+ self.events = {} # type: Dict[str, OrderedDict[int, List[Callable]]]
+ for event in events:
+ self.events[event] = OrderedDict()
def add_event_handler(self, name: str, callback: Callable,
- position=0) -> bool:
+ priority: int = 50) -> bool:
"""
Add a callback to a given event.
Note that if that event name doesn’t exist, it just returns False.
If it was successfully added, it returns True
- position: 0 means insert at the beginning, -1 means end
+ priority is a integer between 0 and 100. 0 is the highest priority and
+ will be called first. 100 is the lowest.
"""
+
if name not in self.events:
return False
callbacks = self.events[name]
- if position >= 0:
- callbacks.insert(position, callback)
- else:
- callbacks.append(callback)
+
+ # Clamp priority
+ priority = max(0, min(priority, 100))
+
+ entry = callbacks.setdefault(priority, [])
+ entry.append(callback)
return True
@@ -74,8 +82,9 @@ class EventHandler:
callbacks = self.events.get(name, None)
if callbacks is None:
return
- for callback in callbacks:
- callback(*args, **kwargs)
+ for priority in callbacks.values():
+ for callback in priority:
+ callback(*args, **kwargs)
def del_event_handler(self, name: str, callback: Callable):
"""
@@ -83,9 +92,13 @@ class EventHandler:
"""
if not name:
for callbacks in self.events.values():
- while callback in callbacks:
- callbacks.remove(callback)
+ for priority in callbacks.values():
+ for entry in priority[:]:
+ if entry == callback:
+ priority.remove(callback)
else:
callbacks = self.events[name]
- if callback in callbacks:
- callbacks.remove(callback)
+ for priority in callbacks.entries():
+ for entry in priority[:]:
+ if entry == callback:
+ priority.remove(callback)
diff --git a/poezio/plugin.py b/poezio/plugin.py
index 7e67d09c..0275e2f9 100644
--- a/poezio/plugin.py
+++ b/poezio/plugin.py
@@ -501,12 +501,12 @@ class BasePlugin(object, metaclass=SafetyMetaclass):
"""
return self.api.del_tab_command(tab_type, name)
- def add_event_handler(self, event_name, handler, position=0):
+ def add_event_handler(self, event_name, handler, *args, **kwargs):
"""
Add an event handler to the event event_name.
An optional position in the event handler list can be provided.
"""
- return self.api.add_event_handler(event_name, handler, position)
+ return self.api.add_event_handler(event_name, handler, *args, **kwargs)
def del_event_handler(self, event_name, handler):
"""
diff --git a/poezio/plugin_manager.py b/poezio/plugin_manager.py
index 89849747..8275e6f9 100644
--- a/poezio/plugin_manager.py
+++ b/poezio/plugin_manager.py
@@ -253,7 +253,7 @@ class PluginManager:
if key in self.core.key_func:
del self.core.commands[key]
- def add_event_handler(self, module_name, event_name, handler, position=0):
+ def add_event_handler(self, module_name, event_name, handler, *args, **kwargs):
"""
Add an event handler. If event_name isn’t in the event list, assume
it is a slixmpp event.
@@ -261,7 +261,7 @@ class PluginManager:
eh = self.event_handlers[module_name]
eh.append((event_name, handler))
if event_name in self.core.events.events:
- self.core.events.add_event_handler(event_name, handler, position)
+ self.core.events.add_event_handler(event_name, handler, *args, **kwargs)
else:
self.core.xmpp.add_event_handler(event_name, handler)