1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
'''
First generation of notification handler could only have one registered
handler.
:copyright: Copyright 2006-2009 by Oliver Schoenborn, all rights reserved.
:license: BSD, see LICENSE.txt for details.
'''
class NotificationMgrOneHandler:
'''
Same as NotificationMgr, but can only accept one
notification at a time.
'''
def __init__(self, notificationHandler = None):
self.__notifyOnSend = False
self.__notifyOnSubscribe = False
self.__notifyOnUnsubscribe = False
self.__notifyOnNewTopic = False
self.__notifyOnDelTopic = False
self.__notifyOnDeadListener = False
self.__handler = notificationHandler
def addHandler(self, handler):
'''Set handler as handler. Can only have one handler. Returns
previous handler.'''
previous = self.__handler
self.__handler = handler
return previous
def notifySubscribe(self, *args, **kwargs):
if self.__notifyOnSubscribe and (self.__handler is not None):
self.__handler.notifySubscribe(*args, **kwargs)
def notifyUnsubscribe(self, *args, **kwargs):
if self.__notifyOnUnsubscribe and (self.__handler is not None):
self.__handler.notifyUnsubscribe(*args, **kwargs)
def notifySend(self, *args, **kwargs):
if self.__notifyOnSend and (self.__handler is not None):
self.__handler.notifySend(*args, **kwargs)
def notifyNewTopic(self, *args, **kwargs):
if self.__notifyOnNewTopic and (self.__handler is not None):
self.__handler.notifyNewTopic(*args, **kwargs)
def notifyDelTopic(self, *args, **kwargs):
if self.__notifyOnDelTopic and (self.__handler is not None):
self.__handler.notifyDelTopic(*args, **kwargs)
def notifyDeadListener(self, *args, **kwargs):
if self.__notifyOnDeadListener and (self.__handler is not None):
self.__handler.notifyDeadListener(*args, **kwargs)
def getFlagStates(self):
'''Return state of each notification flag, as a dict.'''
return dict(
subscribe = self.__notifyOnSubscribe,
unsubscribe = self.__notifyOnUnsubscribe,
deadListener = self.__notifyOnDeadListener,
sendMessage = self.__notifyOnSend,
newTopic = self.__notifyOnNewTopic,
delTopic = self.__notifyOnDelTopic,
)
def setFlagStates(self, subscribe=None, unsubscribe=None,
deadListener=None, sendMessage=None, newTopic=None,
delTopic=None, all=None):
'''Set the notification flag on/off for various aspects of pubsub:
- subscribe: whenever a listener subscribes to a topic;
- unsubscribe: whenever a listener unsubscribes from a topic;
- deadListener: whenever pubsub finds out that a subscribed
listener has been garbage-collected;
- sendMessage: whenever sendMessage() is called;
- newTopic: whenever a new topic is created;
- delTopic: whenever a topic is "deleted" by pubsub;
- all: set all of the above to the given value (True or False).
The kwargs that are None are left at their current value. The 'all'
is set first, then the others. E.g.
pubsub.setFlagStates(all=True, delTopic=False)
will toggle all notifications on, but will turn off the 'delTopic'
notification.
All registered notification handlers (see pub.addNotificationHandler())
will be notified when the above actions are taken.
'''
if all is not None:
# ignore all other arg settings, and set all of them to true:
numArgs = 7 # how many args in this method
self.setFlagStates( all=None, * ((numArgs-1)*[all]) )
if sendMessage is not None:
self.__notifyOnSend = sendMessage
if subscribe is not None:
self.__notifyOnSubscribe = subscribe
if unsubscribe is not None:
self.__notifyOnUnsubscribe = unsubscribe
if newTopic is not None:
self.__notifyOnNewTopic = newTopic
if delTopic is not None:
self.__notifyOnDelTopic = delTopic
if deadListener is not None:
self.__notifyOnDeadListener = deadListener
|