[Grok-dev] Time for a global_adapter?
Martin Aspeli
optilude+lists at gmail.com
Thu Apr 9 05:00:43 EDT 2009
Martijn Faassen wrote:
> Hey,
>
> Martin Aspeli wrote:
>> If I'm not missing something, it's not possible to do this without ZCML:
>>
>> from z3c.form.widget import ComputedWidgetAttribute(
>> def getDefault(data):
>> return "blah"
>>
>> DefaultValueProvider = ComputedWidgetAttribute(
>> getDefault, field=IMySchema['some_field'])
>>
>> <adapter factory=".DefaultValueProvider" name="default" />
>>
>> That is - I'm using a factory (ComputedWidgetAttribute in this case) to
>> obtain an adapter, which I then need to register under a name.
>>
>> I could imagine something like:
>>
>> grok.global_adapter(DefaultValueProvider, name="default")
>>
>> to mimic grok.global_utility().
>>
>> Do we need something like this?
>
> At the top of my head I cannot think of something that does this
> directly in grokcore.component, so this might indeed something we need
> to add there.
There's no such thing as far as I can see.
> Note that you can often get away with creating a subclass just for
> registration purposes, but that does look a bit more complicated in this
> case.
DefaultValueProvider above is an instance (with a __call__ acting as the
adapter factory) so I can't subclass it.
>> As I've said before, I also think we should support named @adapter
>> decorators, e.g.
>>
>> @adapter(IFoo, name="foo")
>> def foo(context):
>> ...
>>
>> although that's slightly different.
>
> This is supported. See the 'function decorators' section at the bottom here:
>
> http://pypi.python.org/pypi/grokcore.component
>
> What it doesn't support is registering named adapters, I think. It could
> be extended to do so.
That's what I meant. I know @adapter works.
> +1 from me for an effort to add these features to grokcore.component
> (also expose them in the 'grok' namespace).
If we do, how quickly could we turn around a new release of
grokcore.component so that I can depend on it in this project?
Cheers,
Martin
--
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book
More information about the Grok-dev
mailing list