[Twisted-Python] twisted.plugin question

Terry Jones terry at jon.es
Thu Jun 15 09:09:26 MDT 2006


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




More information about the Twisted-Python mailing list