summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormathieui <mathieui@mathieui.net>2011-11-07 13:06:57 +0100
committermathieui <mathieui@mathieui.net>2011-11-07 13:06:57 +0100
commit452f62ecf41f416c7a3af33f8e73e306cdafd1c4 (patch)
treea270e0066349bea8eedf2838a31455d60eb53c56 /src
parentb7027e53474c208d7b772794fd34b30092bc4df1 (diff)
downloadpoezio-452f62ecf41f416c7a3af33f8e73e306cdafd1c4.tar.gz
poezio-452f62ecf41f416c7a3af33f8e73e306cdafd1c4.tar.bz2
poezio-452f62ecf41f416c7a3af33f8e73e306cdafd1c4.tar.xz
poezio-452f62ecf41f416c7a3af33f8e73e306cdafd1c4.zip
Actually add the events.py file
Diffstat (limited to 'src')
-rw-r--r--src/events.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/events.py b/src/events.py
new file mode 100644
index 00000000..2363eca8
--- /dev/null
+++ b/src/events.py
@@ -0,0 +1,70 @@
+#
+# This file is part of Poezio.
+#
+# Poezio is free software: you can redistribute it and/or modify
+# it under the terms of the zlib license. See the COPYING file.
+
+"""
+Defines the EventHandler class
+"""
+
+import logging
+log = logging.getLogger(__name__)
+
+class EventHandler(object):
+ """
+ A class keeping a list of possible events that are triggered
+ by poezio. You (a plugin for example) can add an event handler
+ associated with an event name, and whenever that event is triggered,
+ the callback is called
+ """
+ def __init__(self):
+ self.events = {
+ # when you are highlighted in a muc tab
+ 'highlight': [],
+ 'muc_say': [],
+ 'conversation_say': [],
+ 'private_say': [],
+ }
+
+ def add_event_handler(self, name, callback, first=True, last=False, position=None):
+ """
+ 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
+ """
+ if name not in self.events:
+ return False
+
+ if first:
+ self.events[name].insert(0, callback)
+ elif last:
+ self.events[name].append(callback)
+ elif position != None and isinstance(position, int):
+ self.events[name].insert(position, callback)
+ else:
+ self.events[name].insert(0, callback)
+
+ return True
+
+ def trigger(self, name, *args, **kwargs):
+ """
+ Call all the callbacks associated to the given event name
+ """
+ callbacks = self.events[name]
+ for callback in callbacks:
+ callback(*args, **kwargs)
+
+ def del_event_handler(self, name, callback):
+ """
+ Remove the callback from the list of callbacks of the given event
+ """
+ if not name:
+ for event in self.events:
+ while callback in self.events[event]:
+ self.events[event].remove(callback)
+ return True
+ else:
+ if callback in self.events[name]:
+ self.events[name].remove(callback)
+