Example #1
0
def testDeadListener():
    # create a listener for listeners that have died
    class DeathListener(IgnoreNotificationsMixin):
        listenerStr = ''
        def notifyDeadListener(self, pubListener, topicObj):
            assert topicObj.getName() == 'sadTopic'
            #import pdb; pdb.set_trace()
            #print 'hi again'
            DeathListener.listenerStr = pubListener.name()
    dl = DeathListener()
    pub.addNotificationHandler( dl )
    pub.setNotificationFlags(deadListener=True)

    # define a topic, subscribe to it, and kill its listener:
    class TempListener:
        def __call__(self, **kwargs):
            pass
        def __del__(self):
            # print 'being deleted'
            pass
    #def tempListener(): pass
    tempListener = TempListener()
    expectLisrStr, _ = getListenerID(tempListener)
    pub.subscribe(tempListener, 'sadTopic')
    del tempListener

    # verify:
    gc.collect() # for pypy: the gc doesn't work the same as cpython's
    assert DeathListener.listenerStr.startswith(expectLisrStr), \
        '"%s" !~ "%s"' % (DeathListener.listenerStr, expectLisrStr)

    pub.addNotificationHandler(None)
    pub.clearNotificationHandlers()
Example #2
0
def useNotifyByPubsubMessage(pubModule=None,
                             topicMgr=None,
                             all=True,
                             **kwargs):
    '''Will cause all of pubsub's notifications of pubsub "actions" (such as
    new topic created, message sent, listener subscribed, etc) to be sent
    out as messages. Topic will be 'pubsub' subtopics, such as
    'pubsub.newTopic', 'pubsub.delTopic', 'pubsub.sendMessage', etc.

    The 'all' and kwargs args are the same as pubsub's setNotificationFlags(), 
    except that 'all' defaults to True.
    
    The pubModule and topicMgr are rarely needed:

    * The pubModule only needs to be specfied if pubsub is not installed
      on the system search path (ie from pubsub import ... would fail or
      import wrong pubsub -- such as if pubsub is within wxPython's
      wx.lib package). Then pbuModule is the pub module to use::

        from wx.lib.pubsub import pub
        from wx.lib.pubsub.utils import notification
        notification.useNotifyByPubsubMessage(pub)

    * The topicMgr only needs to be specified if you are not using the default
      topic manager created by pubsub.pub (or by provided pubModule).
    '''
    if pubModule is None:
        from pubsub import pub as pubModule
    if topicMgr is None:
        topicMgr = pubModule.getDefaultTopicMgr()
    notifHandler = NotifyByPubsubMessage(topicMgr)
    pubModule.addNotificationHandler(notifHandler)

    pubModule.setNotificationFlags(all=all, **kwargs)
Example #3
0
def useNotifyByPubsubMessage(pubModule=None, topicMgr=None, all=True, **kwargs):
    '''Will cause all of pubsub's notifications of pubsub "actions" (such as
    new topic created, message sent, listener subscribed, etc) to be sent
    out as messages. Topic will be 'pubsub' subtopics, such as
    'pubsub.newTopic', 'pubsub.delTopic', 'pubsub.sendMessage', etc.

    The 'all' and kwargs args are the same as pubsub's setNotificationFlags(), 
    except that 'all' defaults to True.
    
    The pubModule and topicMgr are rarely needed:

    * The pubModule only needs to be specfied if pubsub is not installed
      on the system search path (ie from pubsub import ... would fail or
      import wrong pubsub -- such as if pubsub is within wxPython's
      wx.lib package). Then pbuModule is the pub module to use::

        from wx.lib.pubsub import pub
        from wx.lib.pubsub.utils import notification
        notification.useNotifyByPubsubMessage(pub)

    * The topicMgr only needs to be specified if you are not using the default
      topic manager created by pubsub.pub (or by provided pubModule).
    '''
    if pubModule is None:
        from pubsub import pub as pubModule
    if topicMgr is None:
        topicMgr = pubModule.getDefaultTopicMgr()
    notifHandler = NotifyByPubsubMessage( topicMgr )
    pubModule.addNotificationHandler(notifHandler)

    pubModule.setNotificationFlags(all=all, **kwargs)
Example #4
0
def useNotifyByPubsubMessage(all=True, **kwargs):
    '''Will cause all pubsub notifications of pubsub "actions" (such as
    new topic created, message sent, listener died etc) to be sent out
    as messages of 'pubsub' subtopics, such as 'pubsub.newTopic',
    'pubsub.delTopic', 'pubsub.sendMessage', etc.

    The arguments are those of pubsub's setNotificationFlags(), except that
    'all' defaults to True instead of False. '''
    from pubsub import pub
    notifHandler = NotifyByPubsubMessage(createTopics=True)
    pub.addNotificationHandler(notifHandler)

    pub.setNotificationFlags(all=all, **kwargs)
Example #5
0
def useNotifyByPubsubMessage(all=True, **kwargs):
    '''Will cause all pubsub notifications of pubsub "actions" (such as
    new topic created, message sent, listener died etc) to be sent out
    as messages of 'pubsub' subtopics, such as 'pubsub.newTopic',
    'pubsub.delTopic', 'pubsub.sendMessage', etc.

    The arguments are those of pubsub's setNotificationFlags(), except that
    'all' defaults to True instead of False. '''
    from pubsub import pub
    notifHandler = NotifyByPubsubMessage(createTopics=True)
    pub.addNotificationHandler(notifHandler)

    pub.setNotificationFlags(all=all, **kwargs)
Example #6
0
def useNotifyByWriteFile(fileObj=None, prefix=None, all=True, **kwargs):
    '''Will cause all pubsub notifications of pubsub "actions" (such as
    new topic created, message sent, listener died etc) to be written to
    specified file (or stdout if none given). The fileObj need only
    provide a 'write(string)' method.
    
    The first two arguments are the same as those of NotifyByWriteFile
    constructor. The remaining arguments are those of pubsub's
    setNotificationFlags(), except that 'all' defaults to True instead of
    False. '''
    from pubsub import pub
    notifHandler = NotifyByWriteFile(fileObj, prefix)
    pub.addNotificationHandler(notifHandler)
    pub.setNotificationFlags(all=all, **kwargs)
Example #7
0
def useNotifyByWriteFile(fileObj=None, prefix=None, all=True, **kwargs):
    '''Will cause all pubsub notifications of pubsub "actions" (such as
    new topic created, message sent, listener died etc) to be written to
    specified file (or stdout if none given). The fileObj need only
    provide a 'write(string)' method.
    
    The first two arguments are the same as those of NotifyByWriteFile
    constructor. The remaining arguments are those of pubsub's
    setNotificationFlags(), except that 'all' defaults to True instead of
    False. '''
    from pubsub import pub
    notifHandler = NotifyByWriteFile(fileObj, prefix)
    pub.addNotificationHandler(notifHandler)
    pub.setNotificationFlags(all=all, **kwargs)
Example #8
0
def useNotifyByWriteFile(fileObj=None, prefix=None, 
    pubModule=None, all=True, **kwargs):
    '''Will cause all pubsub notifications of pubsub "actions" (such as
    new topic created, message sent, listener died etc) to be written to
    specified file (or stdout if none given). The fileObj need only
    provide a 'write(string)' method.
    
    The first two arguments are the same as those of NotifyByWriteFile
    constructor. The 'all' and kwargs arguments are those of pubsub's
    setNotificationFlags(), except that 'all' defaults to True.  See
    useNotifyByPubsubMessage() for an explanation of pubModule (typically
    only if pubsub inside wxPython's wx.lib)'''
    if pubModule is None:
        from pubsub import pub as pubModule
    notifHandler = NotifyByWriteFile(fileObj, prefix)
    pubModule.addNotificationHandler(notifHandler)
    pubModule.setNotificationFlags(all=all, **kwargs)
Example #9
0
def useNotifyByWriteFile(fileObj=None,
                         prefix=None,
                         pubModule=None,
                         all=True,
                         **kwargs):
    '''Will cause all pubsub notifications of pubsub "actions" (such as
    new topic created, message sent, listener died etc) to be written to
    specified file (or stdout if none given). The fileObj need only
    provide a 'write(string)' method.
    
    The first two arguments are the same as those of NotifyByWriteFile
    constructor. The 'all' and kwargs arguments are those of pubsub's
    setNotificationFlags(), except that 'all' defaults to True.  See
    useNotifyByPubsubMessage() for an explanation of pubModule (typically
    only if pubsub inside wxPython's wx.lib)'''
    if pubModule is None:
        from pubsub import pub as pubModule
    notifHandler = NotifyByWriteFile(fileObj, prefix)
    pubModule.addNotificationHandler(notifHandler)
    pubModule.setNotificationFlags(all=all, **kwargs)
Example #10
0
"""

:copyright: Copyright since 2006 by Oliver Schoenborn, all rights reserved.
:license: BSD, see LICENSE.txt for details.

"""

import sys

from pubsub import pub
from pubsub.utils.notification import useNotifyByWriteFile, IgnoreNotificationsMixin


# create one special notification handler that ignores all except
# one type of notification
class MyPubsubNotifHandler(IgnoreNotificationsMixin):
    def notifySubscribe(self, pubListener, topicObj, newSub):
        newSubMsg = ''
        if not newSub:
            newSubMsg = ' was already'
        msg = 'MyPubsubNotifHandler: listener %s%s subscribed to %s'
        print(msg % (pubListener.name(), newSubMsg, topicObj.getName()))


pub.addNotificationHandler(MyPubsubNotifHandler())

# print(all notifications to stdout)

useNotifyByWriteFile(sys.stdout, prefix='NotifyByWriteFile:')
Example #11
0
def testNotifications():
    class Handler(INotificationHandler):
        def __init__(self):
            self.resetCounts()
        def resetCounts(self):
            self.counts = dict(send=0, sub=0, unsub=0, delt=0, newt=0, dead=0, all=0)
        def notifySubscribe(self, pubListener, topicObj, newSub):
            self.counts['sub'] += 1
        def notifyUnsubscribe(self, pubListener, topicObj):
            self.counts['unsub'] += 1
        def notifyDeadListener(self, pubListener, topicObj):
            self.counts['dead'] += 1
        def notifySend(self, stage, topicObj, pubListener=None):
            if stage == 'pre': self.counts['send'] += 1
        def notifyNewTopic(self, topicObj, description, required, argsDocs):
            self.counts['newt'] += 1
        def notifyDelTopic(self, topicName):
            self.counts['delt'] += 1

    notifiee = Handler()
    pub.addNotificationHandler(notifiee)
    pub.setNotificationFlags(all=True)

    def verify(**ref):
        gc.collect() # for pypy: the gc doesn't work the same as cpython's
        for key, val in notifiee.counts.items():
            if key in ref:
                assert val == ref[key], "\n%s\n%s" % (notifiee.counts, ref)
            else:
                assert val == 0, "%s = %s, expected 0" % (key, val)
        notifiee.resetCounts()

    verify()
    def testListener(): pass
    def testListener2(): pass
    def testListener3(): pass
    class TestListener:
        def __call__(self): pass
        def __del__(self): pass
    testListener = TestListener()

    topicMgr = pub.getDefaultTopicMgr()
    topicMgr.getOrCreateTopic('newTopic')
    verify(newt=1)

    pub.subscribe(testListener, 'newTopic')
    pub.subscribe(testListener2, 'newTopic')
    pub.subscribe(testListener3, 'newTopic')
    verify(sub=3)

    pub.sendMessage('newTopic')
    verify(send=1)

    verify(dead=0)
    del testListener
    del testListener3
    verify(dead=2)

    pub.unsubscribe(testListener2,'newTopic')
    verify(unsub=1)

    topicMgr.delTopic('newTopic')
    verify(delt=1)
Example #12
0
def testNotifications():
    class Handler(INotificationHandler):
        def __init__(self):
            self.resetCounts()

        def resetCounts(self):
            self.counts = dict(send=0,
                               sub=0,
                               unsub=0,
                               delt=0,
                               newt=0,
                               dead=0,
                               all=0)

        def notifySubscribe(self, pubListener, topicObj, newSub):
            self.counts['sub'] += 1

        def notifyUnsubscribe(self, pubListener, topicObj):
            self.counts['unsub'] += 1

        def notifyDeadListener(self, pubListener, topicObj):
            self.counts['dead'] += 1

        def notifySend(self, stage, topicObj, pubListener=None):
            if stage == 'pre': self.counts['send'] += 1

        def notifyNewTopic(self, topicObj, description, required, argsDocs):
            self.counts['newt'] += 1

        def notifyDelTopic(self, topicName):
            self.counts['delt'] += 1

    notifiee = Handler()
    pub.addNotificationHandler(notifiee)
    pub.setNotificationFlags(all=True)

    def verify(**ref):
        gc.collect()  # for pypy: the gc doesn't work the same as cpython's
        for key, val in notifiee.counts.items():
            if key in ref:
                assert val == ref[key], "\n%s\n%s" % (notifiee.counts, ref)
            else:
                assert val == 0, "%s = %s, expected 0" % (key, val)
        notifiee.resetCounts()

    verify()

    def testListener():
        pass

    def testListener2():
        pass

    def testListener3():
        pass

    class TestListener:
        def __call__(self):
            pass

        def __del__(self):
            pass

    testListener = TestListener()

    topicMgr = pub.getDefaultTopicMgr()
    topicMgr.getOrCreateTopic('newTopic')
    verify(newt=1)

    pub.subscribe(testListener, 'newTopic')
    pub.subscribe(testListener2, 'newTopic')
    pub.subscribe(testListener3, 'newTopic')
    verify(sub=3)

    pub.sendMessage('newTopic')
    verify(send=1)

    verify(dead=0)
    del testListener
    del testListener3
    verify(dead=2)

    pub.unsubscribe(testListener2, 'newTopic')
    verify(unsub=1)

    topicMgr.delTopic('newTopic')
    verify(delt=1)