[Zope3-Users] Letting a custom factory instance act like a class with implements()
Martin Aspeli
optilude+lists at gmail.com
Sat Jan 10 21:38:25 EST 2009
Hi,
I have the following three classes. DelegatingIndexer is a kind of
generic implementation of the IIndexer interface (not shown, it
basically just has a __call__ method) that delegates to a given callable
after doing some prep work. DelegatingIndexerFactory is an adapter
factory for the delegating indexer. indexer is a decorator that uses
these two to allow you to do the following:
>>> @indexer(IMyType)
... def foo(object, **kw):
... return "some value"
class DelegatingIndexer(object):
implements(IIndexer)
def __init__(self, context, callable):
self.context = context
self.callable = callable
def __call__(self, portal, **kwargs):
kwargs = kwargs.copy()
kwargs.setdefault('portal', portal)
return self.callable(self.context, **kwargs)
class DelegatingIndexerFactory(object):
def __init__(self, callable):
self.callable = callable
self.__implemented__ = Implements(IIndexer)
def __call__(self, object):
return DelegatingIndexer(object, self.callable)
class indexer(zope.component.adapter):
def __init__(self, *interfaces):
adapter.__init__(self, *interfaces)
def __call__(self, callable):
factory = DelegatingIndexerFactory(callable)
return adapter.__call__(self, factory)
This works, but I don't understand why I need to set
self.__implemented__ explicitly as an instance variable on the
DelegatingIndexerFactory. Without this, I can't just do
provideAdapter(foo), because it says that the factory (an instance of
type DelegatingIndexerFactory) doesn't implement a single interface (in
fact, implementedBy() returns an empty generator).
I've tried various directives in zope.interface, but I can't find a
nicer way than just setting __implemented__ as above. Am I missing
something?
Cheers,
Martin
--
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book
More information about the Zope3-users
mailing list