1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2005 Tiago Cogumbreiro <cogumbreiro@users.sf.net>
3
# $Id: components.py 570 2005-09-09 19:28:26Z cogumbreiro $
5
#Permission is hereby granted, free of charge, to any person obtaining a copy
6
#of this software and associated documentation files (the "Software"), to deal
7
#in the Software without restriction, including without limitation the rights
8
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
#copies of the Software, and to permit persons to whom the Software is
10
#furnished to do so, subject to the following conditions:
12
#The above copyright notice and this permission notice shall be included in
13
#all copies or substantial portions of the Software.
15
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
__all__ = "EventsDispatcher",
26
def __init__ (self, callbacks):
27
self.callbacks = callbacks
29
def __call__ (self, *args, **kwargs):
30
for callback in self.callbacks:
31
callback (*args, **kwargs)
33
class EventsDispatcher(object):
35
An event dispatcher is the central events object. To use it you must first
36
create an event with the ``create_event`` method, this will return an
37
event source which is basically the function you'll use to trigger the
38
event. After that you register the callbacks. Its usage follows:
40
>>> dispatcher = EventDispatcher()
41
>>> evt_src = dispatcher.create_event ("on-ring-event")
46
>>> dispatcher.register ("on-ring-event", callback1)
55
def create_event (self, event_name):
56
self._events[event_name] = []
57
return EventSource (self._events[event_name])
59
def create_events (self, event_names, event_sources = None):
61
This is a utility method that creates or fills a dict-like object
62
and returns it. The keys are the event names and the values are the
65
if event_sources is None:
68
for evt_name in event_names:
69
event_sources[evt_name] = self.create_event (evt_name)
72
def event_exists (self, event_name):
73
return event_name in self._events
75
def register (self, event_name, callback):
76
assert self.event_exists (event_name)
77
self._events[event_name].append (callback)
79
def unregister (self, event_name, callback):
80
self._events[event_name].remove (callback)