Re: [Zope3-Users] Adapting a builtin?
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?
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', 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?
On Wed, Dec 07, 2005 at 10:23:14AM -0500, Jim Fulton wrote: > 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. Neither one appears to work in a Zope2 trunk checkout. Am I doing something wrong? In lib/python/Products/StandardCacheManagers, I have: # configure.zcml ### http://namespaces.zope.org/zope";> interfaces.py ### from zope.interface import Interface, Attribute class IHTTPConnection(Interface): """Represents an HTTP connection. """ def request(method, path): """Send an HTTP request. """ def getresponse(): """Get a list of strings representing a response. """ class IString(Interface): """Dummy interface to let me adapt strings """ # XXX what should be in this? # 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: [EMAIL PROTECTED] Zope-Trunk $ python test.py -vv -m AcceleratedH Running tests at level 1 Running unit tests: Running: test_PURGE_passes_Host_header (Products.StandardCacheManagers.tests.test_AcceleratedHTTPCacheManager.AcceleratedHTTPCacheTests) test_XXXinterface (Products.StandardCacheManagers.tests.test_AcceleratedHTTPCacheManager.AcceleratedHTTPCacheTests) Error in test test_XXXinterface (Products.StandardCacheManagers.tests.test_AcceleratedHTTPCacheManager.AcceleratedHTTPCacheTests) Traceback (most recent call last): File "/usr/lib/python2.4/unittest.py", line 260, in run testMethod() File "/home/pw/Downloads/Apps/Net/Zope-Trunk/lib/python/Products/StandardCacheManagers/tests/test_AcceleratedHTTPCacheManager.py", line 100, in test_XXXinterface conn = IHTTPConnection(url) File "/home/pw/Downloads/Apps/Net/Zope-Trunk/lib/python/zope/interface/interface.py", line 682, in __call__ raise TypeError("Could not adapt", obj, self) TypeError: ('Could not adapt', 'http://www.google.com', ) Ran 2 tests with 0 failures and 1 errors in 0.261 seconds. Tests with errors: test_XXXinterface (Products.StandardCacheManagers.tests.test_AcceleratedHTTPCacheManager.AcceleratedHTTPCacheTests) -- 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?
Martijn Faassen wrote: Jim Fulton wrote: 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. If exceptions are counted as built-ins, Nope. Builtins are things like str and list. The interface package actually provides some special support for types like these that can't have interface-declaration data added to them. Jim -- 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
Re: [Zope3-Users] Adapting a builtin?
Jim Fulton wrote: 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. If exceptions are counted as built-ins, it certainly does work -- the interface package registers interfaces for the built-in Python exceptions and the Zope 3 framework relies on it for error pages. I ported this functionality to Zope 2 (in experimental code, FiveException on the z3 base), and it works there too. :) Regards, Martijn ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
Re: [Zope3-Users] Adapting a builtin?
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: 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: 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: -- -- 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
RE: [Zope3-Users] Adapting a builtin?
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: > > > >> 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: > > > > > > 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: -- -- 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?
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: > >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: Then you can do: IHTTPConnection('string') to get the adapter. HTH, mac ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
[Zope3-Users] Adapting a builtin?
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: This doesn't seem to work, although it doesn't give any errors. I'm using five in a Zope 2 trunk sandbox. Later I'll dig through it with pdb and see what I can find out, but meanwhile I hoped somebody here had already solved this problem... -- Paul Winkler http://www.slinkp.com ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users