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