[Zope3-Users] Adapting a builtin?

Jim Fulton jim at zope.com
Wed Dec 7 10:23:14 EST 2005


It is valid both to:

- Declare an interface for a built-in type and
   register adapters for that interface, and to

- Register adapters for builtin types directly.

I can't promise that this works now, but it ought to work.
I'm 90% sure that it does in fact work, at least in Zope 3.

Jim

Roger Ineichen wrote:
> 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  
> 
> _______________________________________________
> Zope3-users mailing list
> Zope3-users at zope.org
> http://mail.zope.org/mailman/listinfo/zope3-users


-- 
Jim Fulton           mailto:jim at zope.com       Python Powered!
CTO                  (540) 361-1714            http://www.python.org
Zope Corporation     http://www.zope.com       http://www.zope.org


More information about the Zope3-users mailing list