~ubuntu-branches/ubuntu/saucy/pida/saucy

« back to all changes in this revision

Viewing changes to pida/utils/culebra/events.py

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2006-08-01 13:08:56 UTC
  • mfrom: (0.1.2 etch) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060801130856-v92ktopgdxc8rv7q
Tags: 0.3.1-2ubuntu1
* Re-sync with Debian
* Remove bashisms from debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 $
 
4
 
 
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:
 
11
#
 
12
#The above copyright notice and this permission notice shall be included in
 
13
#all copies or substantial portions of the Software.
 
14
#
 
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
 
21
#SOFTWARE.
 
22
 
 
23
__all__ = "EventsDispatcher",
 
24
 
 
25
class EventSource:
 
26
    def __init__ (self, callbacks):
 
27
        self.callbacks = callbacks
 
28
    
 
29
    def __call__ (self, *args, **kwargs):
 
30
        for callback in self.callbacks:
 
31
            callback (*args, **kwargs)
 
32
 
 
33
class EventsDispatcher(object):
 
34
    """
 
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:
 
39
    
 
40
    >>> dispatcher = EventDispatcher()
 
41
    >>> evt_src = dispatcher.create_event ("on-ring-event")
 
42
    >>> 
 
43
    >>> def callback1 ():
 
44
    >>>     print "riiiing!"
 
45
    >>> 
 
46
    >>> dispatcher.register ("on-ring-event", callback1)
 
47
    >>> 
 
48
    >>> evt_src ()
 
49
    riiing
 
50
    >>> 
 
51
    """
 
52
    def __init__ (self):
 
53
        self._events = {}
 
54
        
 
55
    def create_event (self, event_name):
 
56
        self._events[event_name] = []
 
57
        return EventSource (self._events[event_name])
 
58
    
 
59
    def create_events (self, event_names, event_sources = None):
 
60
        """
 
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
 
63
        event sources.
 
64
        """
 
65
        if event_sources is None:
 
66
            event_sources = {}
 
67
            
 
68
        for evt_name in event_names:
 
69
            event_sources[evt_name] = self.create_event (evt_name)
 
70
        return event_sources
 
71
    
 
72
    def event_exists (self, event_name):
 
73
        return event_name in self._events
 
74
    
 
75
    def register (self, event_name, callback):
 
76
        assert self.event_exists (event_name)
 
77
        self._events[event_name].append (callback)
 
78
    
 
79
    def unregister (self, event_name, callback):
 
80
        self._events[event_name].remove (callback)
 
81
 
 
82