Package wx :: Package lib :: Module pubsub :: Class PublisherClass
[frames | no frames]

Class PublisherClass


The publish/subscribe manager. It keeps track of which listeners are interested in which topics (see subscribe()), and sends a Message for a given topic to listeners that have subscribed to that topic, with optional user data (see sendMessage()).

The three important concepts for Publisher are:


Note:

This class is visible to importers of pubsub only as a Singleton. I.e., every time you execute 'Publisher()', it's actually the same instance of PublisherClass that is returned. So to use, just do'Publisher().method()'.

Method Summary
  __init__(self, singletonKey)
Construct a Publisher.
  __call__(self)
Allows for singleton
  __str__(self)
  getAssociatedTopics(self, listener)
Return a list of topics the given listener is registered with.
  getDeliveryCount(self)
How many listeners have received a message since beginning of run
  getMessageCount(self)
How many times sendMessage() was called since beginning of run
  isSubscribed(self, listener, topic)
Return true if listener has subscribed to topic specified.
  isValid(self, listener)
Return true only if listener will be able to subscribe to Publisher.
  sendMessage(self, topic, data, onTopicNeverCreated)
Send a message for given topic, with optional data, to subscribed listeners.
  subscribe(self, listener, topic)
Subscribe listener for given topic.
  unsubAll(self, topics, onNoSuchTopic)
Unsubscribe all listeners subscribed for topics.
  unsubscribe(self, listener, topics)
Unsubscribe listener.
  validate(self, listener)
Similar to isValid(), but raises a TypeError exception if not valid

Method Details

__init__(self, singletonKey)
(Constructor)

Construct a Publisher. This can only be done by the pubsub module. You just use pubsub.Publisher().

__call__(self)
(Call operator)

Allows for singleton

getAssociatedTopics(self, listener)

Return a list of topics the given listener is registered with. Returns [] if listener never subscribed.

Attention:

when using the return of this method to compare to expected list of topics, remember that topics that are not in the form of a tuple appear as a one-tuple in the return. E.g. if you have subscribed a listener to 'topic1' and ('topic2','subtopic2'), this method returns:

associatedTopics = [('topic1',), ('topic2','subtopic2')]

getDeliveryCount(self)

How many listeners have received a message since beginning of run

getMessageCount(self)

How many times sendMessage() was called since beginning of run

isSubscribed(self, listener, topic=None)

Return true if listener has subscribed to topic specified. If no topic specified, return true if subscribed to something. Use topic=getStrAllTopics() to determine if a listener will receive messages for all topics.

isValid(self, listener)

Return true only if listener will be able to subscribe to Publisher.

sendMessage(self, topic='', data=None, onTopicNeverCreated=None)

Send a message for given topic, with optional data, to subscribed listeners. If topic is not specified, only the listeners that are interested in all topics will receive message. The onTopicNeverCreated is an optional callback of your choice that will be called if the topic given was never created (i.e. it, or one of its subtopics, was never subscribed to by any listener). It will be called as onTopicNeverCreated(topic).

subscribe(self, listener, topic='')

Subscribe listener for given topic. If topic is not specified, listener will be subscribed for all topics (that listener will receive a Message for any topic for which a message is generated).

This method may be called multiple times for one listener, registering it with many topics. It can also be invoked many times for a particular topic, each time with a different listener. See the class doc for requirements on listener and topic.

Notes:

  • The listener is held by Publisher() only by weak reference. This means you must ensure you have at least one strong reference to listener, otherwise it will be DOA ("dead on arrival"). This is particularly easy to forget when wrapping a listener method in a proxy object (e.g. to bind some of its parameters), e.g.:

    class Foo: 
        def listener(self, event): pass
    class Wrapper:
        def __init__(self, fun): self.fun = fun
        def __call__(self, *args): self.fun(*args)
    foo = Foo()
    Publisher().subscribe( Wrapper(foo.listener) ) # whoops: DOA!
    wrapper = Wrapper(foo.listener)
    Publisher().subscribe(wrapper) # good!
    
  • Calling this method for the same listener, with two topics in the same branch of the topic hierarchy, will cause the listener to be notified twice when a message for the deepest topic is sent. E.g. subscribe(listener, 't1') and then subscribe(listener, ('t1','t2')) means that when calling sendMessage('t1'), listener gets one message, but when calling sendMessage(('t1','t2')), listener gets message twice.

unsubAll(self, topics=None, onNoSuchTopic=None)

Unsubscribe all listeners subscribed for topics. Topics can be a single topic (string or tuple) or a list of topics (ie list containing strings and/or tuples). If topics is not specified, all listeners for all topics will be unsubscribed, ie. the Publisher singleton will have no topics and no listeners left. If onNoSuchTopic is given, it will be called as onNoSuchTopic(topic) for each topic that is unknown.

unsubscribe(self, listener, topics=None)

Unsubscribe listener. If topics not specified, listener is completely unsubscribed. Otherwise, it is unsubscribed only for the topic (the usual tuple) or list of topics (ie a list of tuples) specified. Nothing happens if listener is not actually subscribed to any of the topics.

Note that if listener subscribed for two topics (a,b) and (a,c), then unsubscribing for topic (a) will do nothing. You must use getAssociatedTopics(listener) and give unsubscribe() the returned list (or a subset thereof).

validate(self, listener)

Similar to isValid(), but raises a TypeError exception if not valid


Generated by Epydoc 2.1.20050511.rpd on Mon Feb 16 12:55:31 2009 http://epydoc.sf.net