##// END OF EJS Templates
changed notification.py instance variables to underscore_case
Barry Wark -
Show More
@@ -1,122 +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/blueprints/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())
30 NOTIFICATION_TYPE <object object at 0x284b0> {}
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.registeredTypes = set() #set of types that are observed
41 self.registered_types = set() #set of types that are observed
42 self.registeredSenders = 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.registeredTypes and None not in self.registeredTypes) or
62 if((theType not in self.registered_types and
63 (sender not in self.registeredSenders and None not in self.registeredSenders)):
63 None not in self.registered_types) or
64 print theType,sender,self.registeredTypes,self.registeredSenders
64 (sender not in self.registered_senders and
65 None not in self.registered_senders)):
65 return
66 return
66
67
67 for o in self._observers_for_notification(theType, sender):
68 for o in self._observers_for_notification(theType, sender):
68 o(theType, sender, args=kwargs)
69 o(theType, sender, args=kwargs)
69
70
70
71
71 def _observers_for_notification(self, theType, sender):
72 def _observers_for_notification(self, theType, sender):
72 """Find all registered observers that should recieve notification"""
73 """Find all registered observers that should recieve notification"""
73
74
74 keys = (
75 keys = (
75 (theType,sender),
76 (theType,sender),
76 (theType, None),
77 (theType, None),
77 (None, sender),
78 (None, sender),
78 (None,None)
79 (None,None)
79 )
80 )
80
81
81
82
82 obs = set()
83 obs = set()
83 for k in keys:
84 for k in keys:
84 obs.update(self.observers.get(k, set()))
85 obs.update(self.observers.get(k, set()))
85
86
86 return obs
87 return obs
87
88
88
89
89 def add_observer(self, callback, theType, sender):
90 def add_observer(self, callback, theType, sender):
90 """Add an observer callback to this notification center.
91 """Add an observer callback to this notification center.
91
92
92 The given callback will be called upon posting of notifications of
93 The given callback will be called upon posting of notifications of
93 the given type/sender and will receive any additional kwargs passed
94 the given type/sender and will receive any additional kwargs passed
94 to post_notification.
95 to post_notification.
95
96
96 Parameters
97 Parameters
97 ----------
98 ----------
98 observerCallback : callable
99 observerCallback : callable
99 Callable. Must take at least two arguments::
100 Callable. Must take at least two arguments::
100 observerCallback(type, sender, args={})
101 observerCallback(type, sender, args={})
101
102
102 theType : hashable
103 theType : hashable
103 The notification type. If None, all notifications from sender
104 The notification type. If None, all notifications from sender
104 will be posted.
105 will be posted.
105
106
106 sender : hashable
107 sender : hashable
107 The notification sender. If None, all notifications of theType
108 The notification sender. If None, all notifications of theType
108 will be posted.
109 will be posted.
109 """
110 """
110 assert(callback != None)
111 assert(callback != None)
111 self.registeredTypes.add(theType)
112 self.registered_types.add(theType)
112 self.registeredSenders.add(sender)
113 self.registered_senders.add(sender)
113 self.observers.setdefault((theType,sender), set()).add(callback)
114 self.observers.setdefault((theType,sender), set()).add(callback)
114
115
115 def remove_all_observers(self):
116 def remove_all_observers(self):
116 """Removes all observers from this notification center"""
117 """Removes all observers from this notification center"""
117
118
118 self._init_observers()
119 self._init_observers()
119
120
120
121
121
122
122 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