Home | Trees | Index | Help |
|
---|
Package wx :: Package lib :: Module pubsub :: 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:
listener: a function, bound method or callable object that can be called with one parameter (not counting 'self' in the case of methods). The parameter will be a reference to a Message object. E.g., these listeners are ok:
class Foo: def __call__(self, a, b=1): pass # can be called with only one arg def meth(self, a): pass # takes only one arg def meth2(self, a=2, b=''): pass # can be called with one arg def func(a, b=''): pass Foo foo Publisher().subscribe(foo) # functor Publisher().subscribe(foo.meth) # bound method Publisher().subscribe(foo.meth2) # bound method Publisher().subscribe(func) # function
The three types of callables all have arguments that allow a call with only one argument. In every case, the parameter 'a' will contain the message.
topic: a single word, a tuple of words, or a string containing a set of words separated by dots, for example: 'sports.baseball'. A tuple or a dotted notation string denotes a hierarchy of topics from most general to least. For example, a listener of this topic:
('sports','baseball')
would receive messages for these topics:
('sports', 'baseball') # because same ('sports', 'baseball', 'highscores') # because more specific
but not these:
'sports' # because more general ('sports',) # because more general () or ('') # because only for those listening to 'all' topics ('news') # because different topic
message: this is an instance of Message, containing the topic for which the message was sent, and any data the sender specified.
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 | |
---|---|
Construct a Publisher. | |
Allows for singleton | |
__str__(self)
| |
Return a list of topics the given listener is registered with. | |
How many listeners have received a message since beginning of run | |
How many times sendMessage() was called since beginning of run | |
Return true if listener has subscribed to topic specified. | |
Return true only if listener will be able to subscribe to Publisher. | |
Send a message for given topic, with optional data, to subscribed listeners. | |
Subscribe listener for given topic. | |
Unsubscribe all listeners subscribed for topics. | |
Unsubscribe listener. | |
Similar to isValid(), but raises a TypeError exception if not valid |
Method Details |
---|
__init__(self,
singletonKey)
|
__call__(self)
|
getAssociatedTopics(self, listener)Return a list of topics the given listener is registered with. Returns [] if listener never subscribed.
|
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.
|
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 |
Home | Trees | Index | Help |
|
---|
Generated by Epydoc 2.1.20050511.rpd on Thu Mar 22 12:10:41 2007 | http://epydoc.sf.net |