[Interface-dev] Class -> interface adapters, and design question

Itamar Shtull-Trauring itamar at itamarst.org
Thu Apr 22 18:44:18 EDT 2004


On Mon, 2004-04-19 at 06:32, Jim Fulton wrote:

>    adapter.aRegistry.register([implmentedBy(Klass)], [IFoo], '', AdapterClass)
> 
> The first argument is a seqience of specifications.  You get the specification
> for a class using implementedBy.

Thanks. I actually implemented a "create a marker interface on the fly"
thing, I'm pretty what you're suggesting doesn't do what I want. I want
to say "here's how to adapt all instances of class Foo to IBar".

In my AdapterRegistry subclass:

    def registerAdapter(self, adapterFactory, origInterface, destInterface):
        if not issubclass(origInterface, Interface):
            class IMarker(Interface):
                pass
            declarations.classImplements(origInterface, IMarker)
            self.classInterfaces[origInterface] = IMarker
            origInterface = IMarker
        self.register([destInterface], origInterface, '', adapterFactory)

> > I decided that Twisted would have its own (global) registry, and append
> > it to z.i.interface.adapter_hooks. Make sense?
> 
> Is that because you don't want to depend on zope.component?

Well... partially. z.c seems to have lots of complex stuff I'm not sure
we actually need or want. Perhaps I am mistaken?

The reason I mistakenly thought I'd *have* to have one was
Componentized, a simplified version of which would look like so:

class Componentized:

    def __init__(self):
        self.components = {}

    def setComponent(self, iface, obj):
        self.components[iface] = obj
  
    def __conform___(self, iface):
        if self.components.has_key(iface):
            return self.components[iface]
        self.components[iface] = r = adapt(obj, iface)
        return r

The problem here is adapt() being recursive. In Twisted right now it
just goes to the adapter registry and looks it up, instead of doing full
adaptation algorithm. 

But, it occurs to me now I can just iterate over adapt_hooks and use
those, and then __conform__ won't be called again and there's no
infinite recursion. Not a reason to have our own registry then.

> Note that zope.interface.adapter.AdapterRegistry doesn't actually
> provide adapter factory sementics.  That's what
> zope.component.adapter.GlobalAdapterService does.  I'm open to providing
> those sementics in zope.interface.adapter.

I'm doing the actual creation of adapters myself, manually, yes. I'll
take a look at the factory stuff and see if I see any stuff I'd want
moved.

-- 
Itamar Shtull-Trauring    http://itamarst.org
Looking for a job -- http://itamarst.org/resume.html




More information about the Interface-dev mailing list