[Twisted-Python] twisted plugins
Gabriel Rossetti
mailing_lists at evotex.ch
Tue Mar 4 03:29:00 EST 2008
Jean-Paul Calderone wrote:
> 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
>
Thank you Jean-Paul, I understand better now. I had been trying to
create instances in the main. There's one thing I don't get though with
those 2 lines, if I put them in the main, before the for loop, I get en
error that states that it can't be instantiated, but it works in the
class, how come?
Gabriel
More information about the Twisted-Python
mailing list