##// END OF EJS Templates
doctest example for NotificationCenter
Barry Wark -
Show More
@@ -1,110 +1,122 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/blueprints/notification_blueprint.txt for an overview of the
5 See docs/blueprints/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
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 def __init__(self):
33 def __init__(self):
22 super(NotificationCenter, self).__init__()
34 super(NotificationCenter, self).__init__()
23 self._init_observers()
35 self._init_observers()
24
36
25
37
26 def _init_observers(self):
38 def _init_observers(self):
27 """Initialize observer storage"""
39 """Initialize observer storage"""
28
40
29 self.registeredTypes = set() #set of types that are observed
41 self.registeredTypes = set() #set of types that are observed
30 self.registeredSenders = set() #set of senders that are observed
42 self.registeredSenders = set() #set of senders that are observed
31 self.observers = {} #map (type,sender) => callback (callable)
43 self.observers = {} #map (type,sender) => callback (callable)
32
44
33
45
34 def post_notification(self, theType, sender, **kwargs):
46 def post_notification(self, theType, sender, **kwargs):
35 """Post notification (type,sender,**kwargs) to all registered
47 """Post notification (type,sender,**kwargs) to all registered
36 observers.
48 observers.
37
49
38 Implementation
50 Implementation
39 --------------
51 --------------
40 * If no registered observers, performance is O(1).
52 * If no registered observers, performance is O(1).
41 * Notificaiton order is undefined.
53 * Notificaiton order is undefined.
42 * Notifications are posted synchronously.
54 * Notifications are posted synchronously.
43 """
55 """
44
56
45 if(theType==None or sender==None):
57 if(theType==None or sender==None):
46 raise Exception("NotificationCenter.post_notification requires \
58 raise Exception("NotificationCenter.post_notification requires \
47 type and sender.")
59 type and sender.")
48
60
49 # If there are no registered observers for the type/sender pair
61 # If there are no registered observers for the type/sender pair
50 if((theType not in self.registeredTypes and None not in self.registeredTypes) or
62 if((theType not in self.registeredTypes and None not in self.registeredTypes) or
51 (sender not in self.registeredSenders and None not in self.registeredSenders)):
63 (sender not in self.registeredSenders and None not in self.registeredSenders)):
52 print theType,sender,self.registeredTypes,self.registeredSenders
64 print theType,sender,self.registeredTypes,self.registeredSenders
53 return
65 return
54
66
55 for o in self._observers_for_notification(theType, sender):
67 for o in self._observers_for_notification(theType, sender):
56 o(theType, sender, args=kwargs)
68 o(theType, sender, args=kwargs)
57
69
58
70
59 def _observers_for_notification(self, theType, sender):
71 def _observers_for_notification(self, theType, sender):
60 """Find all registered observers that should recieve notification"""
72 """Find all registered observers that should recieve notification"""
61
73
62 keys = (
74 keys = (
63 (theType,sender),
75 (theType,sender),
64 (theType, None),
76 (theType, None),
65 (None, sender),
77 (None, sender),
66 (None,None)
78 (None,None)
67 )
79 )
68
80
69
81
70 obs = set()
82 obs = set()
71 for k in keys:
83 for k in keys:
72 obs.update(self.observers.get(k, set()))
84 obs.update(self.observers.get(k, set()))
73
85
74 return obs
86 return obs
75
87
76
88
77 def add_observer(self, callback, theType, sender):
89 def add_observer(self, callback, theType, sender):
78 """Add an observer callback to this notification center.
90 """Add an observer callback to this notification center.
79
91
80 The given callback will be called upon posting of notifications of
92 The given callback will be called upon posting of notifications of
81 the given type/sender and will receive any additional kwargs passed
93 the given type/sender and will receive any additional kwargs passed
82 to post_notification.
94 to post_notification.
83
95
84 Parameters
96 Parameters
85 ----------
97 ----------
86 observerCallback : callable
98 observerCallback : callable
87 Callable. Must take at least two arguments::
99 Callable. Must take at least two arguments::
88 observerCallback(type, sender, args={})
100 observerCallback(type, sender, args={})
89
101
90 theType : hashable
102 theType : hashable
91 The notification type. If None, all notifications from sender
103 The notification type. If None, all notifications from sender
92 will be posted.
104 will be posted.
93
105
94 sender : hashable
106 sender : hashable
95 The notification sender. If None, all notifications of theType
107 The notification sender. If None, all notifications of theType
96 will be posted.
108 will be posted.
97 """
109 """
98 assert(callback != None)
110 assert(callback != None)
99 self.registeredTypes.add(theType)
111 self.registeredTypes.add(theType)
100 self.registeredSenders.add(sender)
112 self.registeredSenders.add(sender)
101 self.observers.setdefault((theType,sender), set()).add(callback)
113 self.observers.setdefault((theType,sender), set()).add(callback)
102
114
103 def remove_all_observers(self):
115 def remove_all_observers(self):
104 """Removes all observers from this notification center"""
116 """Removes all observers from this notification center"""
105
117
106 self._init_observers()
118 self._init_observers()
107
119
108
120
109
121
110 sharedCenter = NotificationCenter() No newline at end of file
122 sharedCenter = NotificationCenter()
General Comments 0
You need to be logged in to leave comments. Login now