This section describes how to upgrade an application that uses the version 1 (v1) API to use version 3 (v3) of pubsub API.
If your application uses only the Publisher.sendMessage and Publisher.subscribe functions, your job will be very simple (if it isn’t, post on the pubsub Support forum).
start a python interpreter session (same version as used by your app)
copy your app’s pubsub import statement; for instance
>>> from wx.lib.pubsub import pubthen :
>>> print pub.VERSION_STR 1.0.xxxxxxxx >>>
If the printout starts with a number greater than 1 (unlike the above example), your application is already using v3 of pubsub, there is nothing else to do. You may want to try print pub.getMsgProtocol(): if this prints kwargs you are already using the most advanced pubsub API. If it prints arg1 then consider migrating to kwargs protocol as described in Convert Arg1 to Kwargs. DONE.
If the printout starts with “1” (as in the above example), then jump to step 3. Otherwise, the print statement must have led to a traceback message (if not, see Support), so
- download and install the standalone pubsub
- do a search/replace for wx.lib.pubsub with pubsub in your application code
- add a from pubsub import setupv1 in your application’s startup script, before the first pubsub import
- continue from 3.2 below
Do the following:
- add a from wx.lib.pubsub import setupv1 in your application’s startup script, before the first pubsub import
- confirm that all works well: it should because the setupv1 import causes a module to be loaded that has almost exact same code as wx.lib.pubsub
- do a search/replace of Publisher. for pub.
- confirm that all works well
- change setupv1 to setuparg1
- check that all works well: if problem, look at the Differences section
For instance, if yourApp.py is your application’s startup script, and contains
from wx.lib.pubsub import Publisher
Publisher.sendMessage('hello')
then after step 3.3 the above lines would be
from wx.lib.pubsub import setuparg1
from wx.lib.pubsub import pub
pub.sendMessage('hello')
Notes:
See the test file tests/trans1to3/test_trans_step_1.py in the source distribution for an example application after this step has been executed.
Each subsection describes a difference between the v1 and v3 arg1 API. Differences with v3 kwargs API are not covered as going straight fromm v1 to v3 kwargs is not supported.
The main interface to pubsub in v1 was via pubsub.Publisher, which was a singleton instance of a PublisherClass class. Pubsub supported the following ways of accessing pubsub functionality:
Version 1.x:
from wx.lib.pubsub import Publisher
Publisher.function(...) # OR:
Publisher().function(...)
Version >= 3.0:
from wx.lib.pubsub import pub # OR:
from pubsub import Publisher # alias for "pub"
pub.function() # only!
Ie,
Version 1.x:
sendMessage(topic = ALL_TOPICS, data = None, onTopicNeverCreated=None)
Version >= 3.0:
sendMessage(topicName, data = None)
Changes:
Version 1.x:
subscribe(listener, topic = ALL_TOPICS)
Version >= 3.0:
subscribe(listener, topicName)
Change: In v1, subscribe(listener) can be used to subscribe a callable to the “ALL_TOPICS” topic. In v3, you must explicitly give pub.ALL_TOPICS for the topicName argument, ie a topic name is always required. This follows the “explicit is better” philosophy of Python.
Version 1.x:
unsubscribe(listener, topics=None)
Version >= 3.0:
unsubscribe(listener, topicName)
Change: In v1, unsubscribe(listener) can be used to unsubscribe a callable from all topics that it is subscribed to. This is redundant since this functionality is available via unsubAll function, so this capability has been removed and a topic name is always required. Furthermore, in v1 the topics argument could be a list of topic names, a convenience to unsubscribe a listener from several topics. Again, this is available via the unsubAll function so it has been removed. Change any calls of the form unsubscribe(listener) or unsubscribe(listener, list of topics) to use unsubAll
Version 1.x:
isSubscribed(listener, topic=None)
Version >= 3.0:
isSubscribed(listener, topicName)
Change: In v1, leaving topic=None cause isSubscribed(listener) to check whether listener was subscribed to anything. This test is no longer available via isSubscribed post v1 due to the way listeners are registered. However, getAssociatedTopics(listener) != [] provides the same answer.
Version 1.x:
unsubAll(topics = None, onNoSuchTopic = None)
Version >= 3.0:
unsubAll(topicName = None, listenerFilter = None, topicFilter = None)
Change: as with sendMessage, the callback is no longer accepted. The equivalent functionality could be obtained similarly, see the sendMessage discussion, specifically, about pubsub notification handling.
Version 1.x:
returns list of topic names (names in tuple format)
Version >= 3.0:
returns list of pub.Topic objects
Both are no longer available as equivalent metrics can be obtained via the use of a notification handler’s notifySend() method, and filtering the calls: