Hash: SHA1

Chris Withers wrote:
> Fred Drake wrote:
>> On 1/24/07, Chris Withers <[EMAIL PROTECTED]> wrote:
>>> queryAdapter, for me, is "starting with the supplied object, get me
>>> something that implements the supplied interface and return None if no
>>> such object can be obtained".
>>  o = IFoo(ob, None)
>>  if os is not None:
> Ah, now that's what I was looking for, thanks :-)

Note that for regularity, I assert that the calling the IFoo interface
should allow the follwing use cases, based on the asserition that
utilities are "zero-order" adapters, just as views are "binary order"

First the setup::

  >>> from zope.interface import Interface, Attribute
  >>> from zope.interface import implements, directlyProvides
  >>> from zope.component import provideUtility, provideAdapter, adapts
  >>> class IFoo(Interface):
  ...     name = Attribute(u'Name')
  >>> class FooUtil:
  ...     implements(IFoo)
  ...     def __init__(self, name):
  ...         self.name = name
  >>> fooUtil = FooUtil('default')
  >>> provideUtility(fooUtil)
  >>> fooUtilNamed = FooUtil('named')
  >>> provideUtility(fooUtilNamed, name='named')
  >>> class FooAdapter:
  ...     implements(IFoo)
  ...     adapts(None)
  ...     name = 'default'
  ...     def __init__(self, context):
  ...         self.context = context
  >>> class FooAdapterNamed(FooAdapter):
  ...     name = 'named'
  >>> provideAdapter(FooAdapter)
  >>> provideAdapter(FooAdapterNamed, name='named')

Now the usage.  Things which *don't* work today are marked '(SF)'.

Interfaces called with *no* context argument return
utilities, not adapters (SF)::

  >>> u_default = IFoo() # return the default utility
  >>> u_default.__class__.__name__, u_default.name
  ('FooUtil', 'default')

Named utilities can also be looked up (SF)::

  >>> u_named = IFoo(name='named')
  >>> u_named.__class__.__name__, u_named.name
  ('FooUtil', 'named')

Default adapter lookup works as expected::

  >>> context = object()
  >>> a_default = IFoo(context)
  >>> a_default.__class__.__name__, a_default.name
  ('FooAdapter', 'default')

But we can also do named adapter lookup (SF)::

  >>> a_named = IFoo(context, name='named')
  >>> a_named.__class__.__name__, a_named.name
  ('FooAdapterNamed', 'named')

- --
Tres Seaver          +1 540-429-0999          [EMAIL PROTECTED]
Palladion Software   "Excellence by Design"    http://palladion.com
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org


Zope3-dev mailing list
Unsub: http://mail.zope.org/mailman/options/zope3-dev/archive%40mail-archive.com

Reply via email to