Grok uses a few decorators to register functions or methods for specific functionality.
Declare that the decorated function subscribes to an event or a combination of objects and events.
Applicable on module-level for functions. Requires at least one class or interface as argument.
Decorator that defines an action factory based on a form method. The method receives the form data as keyword parameters.
The decorated method will be protected by the permission. Used in web service views such as REST or XML-RPC.
These decorators are always used in tandem to declare an adapter factory.
Describes that a function adapts an object or a combination of objects.
Describes that a function that’s used as an adapter implements an interface or a number of interfaces.
Example 1: Adapt to an interface
@grok.adapter(ICave)
@grok.implementer(IHome)
def home_for_cave(cave):
return Home()
Example 2: Adapt a regular class instead of an interface
@grok.adapter(Cave)
@grok.implementer(IHome)
def home_for_cave(cave):
return Home()
Example 3: Declare a multi-adapter factory
@grok.adapter(ICave, IFire)
@grok.implementer(ICozy)
def cozy_dwelling(cave, fire):
return Dwelling()