[Grok-dev] Time for a global_adapter?

Martijn Faassen faassen at startifact.com
Wed Apr 8 13:22:25 EDT 2009


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.

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.

> 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.

+1 from me for an effort to add these features to grokcore.component 
(also expose them in the 'grok' namespace).

Regards,

Martijn



More information about the Grok-dev mailing list