Show More
@@ -1,110 +1,122 b'' | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | |
|
3 | 3 | """The IPython Core Notification Center. |
|
4 | 4 | |
|
5 | 5 | See docs/blueprints/notification_blueprint.txt for an overview of the |
|
6 | 6 | notification module. |
|
7 | 7 | """ |
|
8 | 8 | |
|
9 | 9 | __docformat__ = "restructuredtext en" |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Copyright (C) 2008 The IPython Development Team |
|
13 | 13 | # |
|
14 | 14 | # Distributed under the terms of the BSD License. The full license is in |
|
15 | 15 | # the file COPYING, distributed as part of this software. |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | |
|
18 | 18 | |
|
19 | 19 | class NotificationCenter(object): |
|
20 |
"""Synchronous notification center |
|
|
20 | """Synchronous notification center | |
|
21 | ||
|
22 | Example | |
|
23 | ------- | |
|
24 | >>> import IPython.kernel.core.notification as notification | |
|
25 | >>> def callback(theType, theSender, args={}): | |
|
26 | ... print theType,theSender,args | |
|
27 | ... | |
|
28 | >>> notification.sharedCenter.add_observer(callback, 'NOTIFICATION_TYPE', None) | |
|
29 | >>> notification.sharedCenter.post_notification('NOTIFICATION_TYPE', object()) | |
|
30 | NOTIFICATION_TYPE <object object at 0x284b0> {} | |
|
31 | ||
|
32 | """ | |
|
21 | 33 | def __init__(self): |
|
22 | 34 | super(NotificationCenter, self).__init__() |
|
23 | 35 | self._init_observers() |
|
24 | 36 | |
|
25 | 37 | |
|
26 | 38 | def _init_observers(self): |
|
27 | 39 | """Initialize observer storage""" |
|
28 | 40 | |
|
29 | 41 | self.registeredTypes = set() #set of types that are observed |
|
30 | 42 | self.registeredSenders = set() #set of senders that are observed |
|
31 | 43 | self.observers = {} #map (type,sender) => callback (callable) |
|
32 | 44 | |
|
33 | 45 | |
|
34 | 46 | def post_notification(self, theType, sender, **kwargs): |
|
35 | 47 | """Post notification (type,sender,**kwargs) to all registered |
|
36 | 48 | observers. |
|
37 | 49 | |
|
38 | 50 | Implementation |
|
39 | 51 | -------------- |
|
40 | 52 | * If no registered observers, performance is O(1). |
|
41 | 53 | * Notificaiton order is undefined. |
|
42 | 54 | * Notifications are posted synchronously. |
|
43 | 55 | """ |
|
44 | 56 | |
|
45 | 57 | if(theType==None or sender==None): |
|
46 | 58 | raise Exception("NotificationCenter.post_notification requires \ |
|
47 | 59 | type and sender.") |
|
48 | 60 | |
|
49 | 61 | # If there are no registered observers for the type/sender pair |
|
50 | 62 | if((theType not in self.registeredTypes and None not in self.registeredTypes) or |
|
51 | 63 | (sender not in self.registeredSenders and None not in self.registeredSenders)): |
|
52 | 64 | print theType,sender,self.registeredTypes,self.registeredSenders |
|
53 | 65 | return |
|
54 | 66 | |
|
55 | 67 | for o in self._observers_for_notification(theType, sender): |
|
56 | 68 | o(theType, sender, args=kwargs) |
|
57 | 69 | |
|
58 | 70 | |
|
59 | 71 | def _observers_for_notification(self, theType, sender): |
|
60 | 72 | """Find all registered observers that should recieve notification""" |
|
61 | 73 | |
|
62 | 74 | keys = ( |
|
63 | 75 | (theType,sender), |
|
64 | 76 | (theType, None), |
|
65 | 77 | (None, sender), |
|
66 | 78 | (None,None) |
|
67 | 79 | ) |
|
68 | 80 | |
|
69 | 81 | |
|
70 | 82 | obs = set() |
|
71 | 83 | for k in keys: |
|
72 | 84 | obs.update(self.observers.get(k, set())) |
|
73 | 85 | |
|
74 | 86 | return obs |
|
75 | 87 | |
|
76 | 88 | |
|
77 | 89 | def add_observer(self, callback, theType, sender): |
|
78 | 90 | """Add an observer callback to this notification center. |
|
79 | 91 | |
|
80 | 92 | The given callback will be called upon posting of notifications of |
|
81 | 93 | the given type/sender and will receive any additional kwargs passed |
|
82 | 94 | to post_notification. |
|
83 | 95 | |
|
84 | 96 | Parameters |
|
85 | 97 | ---------- |
|
86 | 98 | observerCallback : callable |
|
87 | 99 | Callable. Must take at least two arguments:: |
|
88 | 100 | observerCallback(type, sender, args={}) |
|
89 | 101 | |
|
90 | 102 | theType : hashable |
|
91 | 103 | The notification type. If None, all notifications from sender |
|
92 | 104 | will be posted. |
|
93 | 105 | |
|
94 | 106 | sender : hashable |
|
95 | 107 | The notification sender. If None, all notifications of theType |
|
96 | 108 | will be posted. |
|
97 | 109 | """ |
|
98 | 110 | assert(callback != None) |
|
99 | 111 | self.registeredTypes.add(theType) |
|
100 | 112 | self.registeredSenders.add(sender) |
|
101 | 113 | self.observers.setdefault((theType,sender), set()).add(callback) |
|
102 | 114 | |
|
103 | 115 | def remove_all_observers(self): |
|
104 | 116 | """Removes all observers from this notification center""" |
|
105 | 117 | |
|
106 | 118 | self._init_observers() |
|
107 | 119 | |
|
108 | 120 | |
|
109 | 121 | |
|
110 | 122 | sharedCenter = NotificationCenter() No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now