[Twisted-web] registerAdapter and implemented interfaces

en.karpachov at ospaz.ru en.karpachov at ospaz.ru
Thu Aug 25 01:11:57 MDT 2005

In nevow, when I adapt a context to ISession, I get a
twisted.web.server.Session object by default. If I want to adapt this
session-like object to my custom IUser interface, I register an adapter:

components.registerAdapter(UserAdapter, twisted.web.server.Session, IUser)

so I could write:

     session = ISession(ctx)
     user = IUser(session)

But, if I eventually move to the nevow guard, this code won't work because
ISession adapter now returns an object of nevow.guard.GuardSession.

What is the right way to register an adapter so the code won't break?

components.registerAdapter(UserAdapter, <what is here?>, IUser)

I can use the Componentized class, but it is not obvious and there is no
guarantee either that ISession will always adapt to the Componentized.

It would be easier if there was a precondition that an adapter itself
implemented the interface it adapts to, so one could write:

components.registerAdapter(UserAdapter, ISession, IUser)

Is there any rationale not to have such a precondition? What I missed here?


More information about the Twisted-web mailing list