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