[Twisted-Python] twisted plugins

Jean-Paul Calderone exarkun at divmod.com
Mon Mar 3 06:52:42 MST 2008


On Mon, 03 Mar 2008 14:44:12 +0100, Gabriel Rossetti <mailing_lists at evotex.ch> wrote:
>Hello,
>
>I tried creating some test plugins using the plugin howto on the twisted 
>website. I did as shown, I added the plugins dir to the PYTHONPATH and 
>tested it. The problem is I get nothing back when calling 
>getPlugins(IMyInterface).  Here is some code :
>
>----------------------------------------------------------------------------------------------
>
>    from zope.interface import Interface
>
>    class IHelloWorld(Interface):
>        """
>        A simple test plugin that displays "Hello world"
>        """
>              def display():
>            """
>            Displays on stdout "Hello world" in a language chosen by the
>    programmer
>            """
>
>----------------------------------------------------------------------------------------------
>
>    from twisted.plugin import IPlugin
>    from zope.interface import implements
>    from interfaces import ihelloworld
>
>    class EnglishHelloWorld(object):
>              implements(IPlugin, ihelloworld.IHelloWorld)
>
>        def display(self):
>            """
>            Displays on stdout "Hello world" in English
>            """
>            print "Hello world!"
>
>----------------------------------------------------------------------------------------------
>
>    from twisted.plugin import getPlugins
>    from twisted.plugin import IPlugin
>    from interfaces import ihelloworld
>
>    def displayHelloWorld():
>              for h in getPlugins(ihelloworld.IHelloWorld):
>            h().display()
>              if(__name__ == "__main__"):
>        displayHelloWorld()
>
>----------------------------------------------------------------------------------------------
>
>I tried running in debug mode to see what happens, but I don't get it, in 
>the getPlugins() code, it does this :
>
>    allDropins = getCache(package)
>
>and in this dict I see my plugins, but their inner "plugin" member is an 
>empty list. I looked at the code of some of the examples, they used 
>"classProvides()" and not "implements()", so I tried that :
>
>----------------------------------------------------------------------------------------------
>
>    from twisted.plugin import IPlugin
>    from zope.interface import implements
>    from zope.interface import classProvides
>    from interfaces import ihelloworld
>
>    class EnglishHelloWorld(object):
>              classProvides(IPlugin, ihelloworld.IHelloWorld)
>
>        def display(self):
>            """
>            Displays on stdout "Hello world" in English
>            """
>            print "Hello world!"
>
>----------------------------------------------------------------------------------------------
>
>which works, is the doc out of sync with the current code or did I do 
>something wrong? Another question, it's not really very practical to have to 
>iterate all the plugins and I guess test to see which one is which, is there 
>a better way of doing it?

Using classProvides can be correct, although it's not quite in your example
code because EnglishHelloWorld must be instantiated in order to use it, and
that isn't reflected in your IHelloWorld interface.  You could make display
a static or class method.  Or you continue using implements and instantiate
EnglishHelloWorld, binding a name to the result (as the documentation shows
with steelPlate/brassPlate).

In the example, when the plugin system finds objects, it finds steelPlate
and brassPlate, not SimpleMaterial.

Jean-Paul




More information about the Twisted-Python mailing list