[Zope3-Users] Adapting a builtin?

Roger Ineichen dev at projekt01.ch
Wed Dec 7 03:38:50 EST 2005


Hi Jim,
can you confirm that this is a valid concept for 
the adapter registry or do we get trouble in the future?

I didn't find any sample in the README.txt files about adapting
builtin strings like a "principal_id").


Hi Michael and Mac

> -----Original Message-----
> From: zope3-users-bounces at zope.org 
> [mailto:zope3-users-bounces at zope.org] On Behalf Of Michael Howitz
> Sent: Wednesday, December 07, 2005 8:59 AM
> To: Paul Winkler
> Cc: zope3-users at zope.org
> Subject: Re: [Zope3-Users] Adapting a builtin?
> 
> On Tue, 2005-12-06 at 18:44 -0500, Paul Winkler wrote:
> > Hi,
> > 
> > Is it possible to register an adapter for a builtin type
> > such as str?  All the adapter examples I see use an 
> interface for the
> > "for" attribute.
> > 
> > Something like:
> > 
> >   <adapter for="str"
> >     provides=".interfaces.IHTTPConnection"
> >     factory="httplib.HTTPConnection"
> >   />
> 
> Hi,
> 
> in plain Zope3 (I'm not using Five, but it may be similar there.) you
> can declare an interface for the buitin: (e.g. in interfaces.py)
> 
> class interface IStr(Interface):
>    # define the methods here which you need to access
>    def upper():
>        "upper ... as an example"
> 
> In configure.zcml, you declare the interface for the builtin:
> 
> <class class="str">
>    <implements interface=".interfaces.IStr" />
> </class>
> 
> <adapter
>    for=".interfaces.IStr"
>    provides=".interfaces.IHTTPConnection"
>    factory="httplib.HTTPConnection"
>    />
> 
> Then you can do:
>   IHTTPConnection('string') to get the adapter.
> 
> HTH,
>  mac

That's a interesting question. I do it also, but in a different 
way. It works for me like this and the adapter registry and 
persistence work well.

--------------
class PrincipalMessagesForPrincipalId(BaseMessages):
    """Base implementation for principal annotation based messages."""

    adapts(IAttributeAnnotatable, str)

    implements(IPrincipalMessages)

    messageQueueKey = 'PrincipalMessages'

    def __init__(self, context, principalId):
        """Initialize with a context."""
        self.context = context
        self.principalId = principalId

--------------

With adapts() and implements() you can use a simply registration
like:

--------------
  <adapter factory=".adapters.PrincipalMessagesForPrincipalId" />
--------------

Regards
Roger Ineichen

Projekt01 GmbH
www.projekt01.ch
_____________________________
END OF MESSAGE  



More information about the Zope3-users mailing list