Re: [Zope3-Users] Adapting a builtin?

2005-12-13 Thread Paul Winkler
On Tue, Dec 13, 2005 at 07:57:38AM +0100, Michael Howitz wrote:
 In unittests the ZCML-Directives are not used so you have to do the
 things from ZCML in your test-code before adaption.
 
 from zope.interface import classImplements
 from zope.app.testing import ztapi
 
 classImplements(str, IString)
 ztapi.provideAdapter(IString, IHTTPConnection, HTTPConnection)

Yes, that does the job. I better go read up on testing in z3.
Thanks!

-- 

Paul Winkler
http://www.slinkp.com
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] Adapting a builtin?

2005-12-12 Thread Michael Howitz
On Mon, 2005-12-12 at 18:06 -0500, Paul Winkler wrote:
[...]
 # toward the end of
 # tests/testtests/test_AcceleratedHTTPCacheManager.py #
 
 def test_XXXinterface(self):
 url = 'http://www.google.com'
 from Products.StandardCacheManagers.interfaces import IHTTPConnection
 conn = IHTTPConnection(url)
 
 #
 
 
 This raises an error, regardless of whether I remove the registration of 
 IString or not:
[...]
 TypeError: ('Could not adapt', 'http://www.google.com', InterfaceClass
 Products.StandardCacheManagers.interfaces.IHTTPConnection)

In unittests the ZCML-Directives are not used so you have to do the
things from ZCML in your test-code before adaption.

from zope.interface import classImplements
from zope.app.testing import ztapi

classImplements(str, IString)
ztapi.provideAdapter(IString, IHTTPConnection, HTTPConnection)

HTH,
 mac

___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


RE: [Zope3-Users] Adapting a builtin?

2005-12-07 Thread Roger Ineichen
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: [EMAIL PROTECTED] 
 [mailto:[EMAIL PROTECTED] On Behalf Of Michael Howitz
 Sent: Wednesday, December 07, 2005 8:59 AM
 To: Paul Winkler
 Cc: zope3-users@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@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] Adapting a builtin?

2005-12-07 Thread Jim Fulton

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: [EMAIL PROTECTED] 
[mailto:[EMAIL PROTECTED] On Behalf Of Michael Howitz

Sent: Wednesday, December 07, 2005 8:59 AM
To: Paul Winkler
Cc: zope3-users@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@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users



--
Jim Fulton   mailto:[EMAIL PROTECTED]   Python Powered!
CTO  (540) 361-1714http://www.python.org
Zope Corporation http://www.zope.com   http://www.zope.org
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users