[Grok-dev] Dynamic Adapter Registration

Lennart Regebro regebro at gmail.com
Fri Feb 6 13:06:17 EST 2009


On Fri, Feb 6, 2009 at 11:30, Jeroen Michiel <jmichiel at yahoo.com> wrote:
> I'm having a problem dynamically registering adapters.
> I'll describe what I want to do, and why (I think) I need this:
> I want to be able to have 'configurable' adapters: adapters that all share
> the same adapted Interface IA and all provide the same Interface IB, but do
> the adaptation in a different, configurable way. To get this working I need
> named adapters to distinguish between them.
> Since adapters just have to be callables, what I thought to do was have 1
> class that has a __call__ function which accepts 1 parameter of an object
> providing IA, then when it is needed, create an object of that class, add it
> to the ZODB somechere, and register that object as a named adapter for IA to
> IB. These registrations need to be persistent, of course!

Well, this is doable, but why do you need them to be persistent? The
adapters are in python code, and can be registered there, in  the
normal Grok way, with names. You don't need another registration, one
is enough.

> I can't use the normal grok.Adapter or grok.adapter way to register them (I
> think???), since I'm not using it on a class or function, but on an object,
> so I have to be able to do it in 'run-time', but how?

I guess what you are trying to say is that you want to enable certain
functionality on objects in the ZODB, on an object per object basis?

One common way to do this is to have one interface each for the
adapters, and then mark the object with that interface. So that the
object foo/bar/frotz implements IImplementsSomething, and you have an
adapter that adapts from IImplementsSomething to ISomething.

-- 
Lennart Regebro: Zope and Plone consulting.
http://www.colliberty.com/
+33 661 58 14 64


More information about the Grok-dev mailing list