[Twisted-Python] twisted.plugin question

Is the Twisted plugin system described here http://twistedmatrix.com/projects/core/documentation/howto/plugin.html still supported / functional? I tried implementing the example on that page and couldn't make it work. Below is my simplified code from that example, in two files. BTW, the code on that page in the section Extending an Existing Program is missing a 'from zope.interface import implements'. Also, the assumption that code is in a file called imatsim.py, and the curious 'from matsim import imatsim' could be explained. I guess the later is just a harmless anachronism? Regards, Terry # ------------------- # Begin plugin.py: import sys from zope.interface import Interface from twisted.plugin import getPlugins sys.path[:0] = ['./twisted/plugins'] class IMaterial(Interface): def yieldStress(temperature): """ """ def displayMaterial(m): print 'A material with yield stress %s at 500 K' % (m.yieldStress(500),) def displayAllKnownMaterials(): for material in getPlugins(IMaterial): displayMaterial(material) if __name__ == '__main__': displayAllKnownMaterials() # End plugin.py: # ------------------- # Begin ./twisted/plugins/materials.py: from twisted.plugin import IPlugin from plugin import IMaterial from zope.interface import implements class SimpleMaterial(object): implements(IPlugin, IMaterial) def __init__(self, yieldStressFactor): self._yieldStressFactor = yieldStressFactor def yieldStress(self, temperature): return self._yieldStressFactory * temperature steelPlate = SimpleMaterial(2) brassPlate = SimpleMaterial(1) # End ./twisted/plugins/materials.py: When I run plugin.py, I get the error: $ python plugin.py Traceback (most recent call last): File "plugin.py", line 18, in ? displayAllKnownMaterials() File "plugin.py", line 14, in displayAllKnownMaterials for material in getPlugins(IMaterial): File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/twisted/plugin.py", line 210, in getPlugins allDropins = getCache(package) File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site-packages/twisted/plugin.py", line 176, in getCache newCacheData = pickle.dumps(cache, 2) cPickle.PicklingError: Can't pickle <InterfaceClass plugin.IMaterial>: attribute lookup plugin.IMaterial failed

On Thu, 15 Jun 2006 17:09:26 +0200, Terry Jones <terry@jon.es> wrote:
Is the Twisted plugin system described here
http://twistedmatrix.com/projects/core/documentation/howto/plugin.html
still supported / functional?
Yes.
I tried implementing the example on that page and couldn't make it work. Below is my simplified code from that example, in two files.
One problem with the code attached is that it creates a plugin class which implements plugin.IMaterial but then goes and searches for plugins which provide __main__.IMaterial. Since these aren't the same interface, even without the pickle error no results will be found. As for the pickle error itself, I'm not sure what's causing it. I can't reproduce it myself. The exception suggests that either "plugin" cannot be imported, or that it has no attribute IMaterial. Since the module which defines the object being pickled imports plugin and uses its IMaterial attribute, this seems unlikely, but that is what the exception suggests to me.
BTW, the code on that page in the section Extending an Existing Program is missing a 'from zope.interface import implements'. Also, the assumption that code is in a file called imatsim.py, and the curious 'from matsim import imatsim' could be explained. I guess the later is just a harmless anachronism?
I've created a ticket in the tracker for expanding the howto to explain these things. Thanks for pointing out that they were not well explained. Jean-Paul

On Thu, 15 Jun 2006 17:09:26 +0200, Terry Jones <terry@jon.es> wrote:
sys.path[:0] = ['./twisted/plugins']
This line is unnecessary, and probably wrong. If you look in twisted/plugins/__init__.py, you will notice that it puts any additional entries on sys.path into its __path__; the fact that "." is already on your sys.path is enough for it to recognize this plugin.
def displayAllKnownMaterials(): for material in getPlugins(IMaterial): displayMaterial(material)
This is going to look for plugins in twisted.plugins, which is probably not what you want. Did you look under the "alternate plugin packages" section of that document?
from twisted.plugin import IPlugin from plugin import IMaterial
There is some vagueness in naming here. Depending on your working directory (etc) twisted.plugin might be picked up as plugin. You probably want to change the name of your example module to something unique.
When I run plugin.py, I get the error:
My analysis of this is the same as JP's; however, generally the file you run with 'python' should look something like this: from myapp import main main() Python has the unfortunate habit of creating a special module called "__main__" and putting the code from the file it is passed onto the commandline into that module, rather than into the location where it would go if you imported it; having two copies of your "plugin" module in memory at once (the imported one, and the __main__ one) is probably causing at least some of your problems.
participants (3)
-
glyph@divmod.com
-
Jean-Paul Calderone
-
Terry Jones