Writing configuration decoration?

16 views
Skip to first unread message

Peter Hartmann

unread,
May 16, 2012, 8:05:05 AM5/16/12
to pylons-discuss
Hi,

I find myself in need to write a custom configuration decoration that
would mark a resource to be included in config during a scan(). Quite
surprisingly, the only configuration decoration (needed a special term
in glossary) in Pyramid seems to be pyramid.view.view_config. I've
looked into it, as well as pyramid_handlers.action. Both look
different in what they do and I suspect what actually matters is how
they are called. Can anyone offer explanation on this process? A
guidance on how to write a simple configuration decoration myself
would be appreciated, too.

Regards,
Peter

Michael Merickel

unread,
May 16, 2012, 12:14:37 PM5/16/12
to pylons-...@googlegroups.com
The way Pyramid thinks about decorators is that when the decorator's
callback is invoked upon scan, the configurator object is passed into
the callback. From there you can do what you need to do via config or
config.registry or config.registry.settings, etc. The simplest way to
setup decorators is to create a new directive on the configurator
which does what you want imperatively. Then just have the decorator
call that.

For example, @view_config simply calls config.add_view(). This keeps
view_config very focused only on decorator logic and not how to add
views. For direction on writing a decorator like view_config just look
at config.add_directive and the view_config implementation (after
reading how venusian works). Yeah the docs could probably be improved
here. Below are the two links I know of in the docs which should get
you started.

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/hooks.html#registering-configuration-decorators
http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/extconfig.html#adding-methods-to-the-configurator-via-add-directive

The @action decorator in pyramid_handlers is different because it is
not found at scan-time. Rather, when you call add_handler(MyHandler),
the class methods are scanned for @action decorators.

HTH,
Michael
> --
> You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
> To post to this group, send email to pylons-...@googlegroups.com.
> To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
>

Peter Hartmann

unread,
May 18, 2012, 5:58:31 AM5/18/12
to pylons-discuss
On 16 May, 16:14, Michael Merickel <mmeri...@gmail.com> wrote:
> The way Pyramid thinks about decorators is that when the decorator's
> callback is invoked upon scan, the configurator object is passed into
> the callback. From there you can do what you need to do via config or
> config.registry or config.registry.settings, etc. The simplest way to
> setup decorators is to create a new directive on the configurator
> which does what you want imperatively. Then just have the decorator
> call that.
>
> For example, @view_config simply calls config.add_view(). This keeps
> view_config very focused only on decorator logic and not how to add
> views. For direction on writing a decorator like view_config just look
> at config.add_directive and the view_config implementation (after
> reading how venusian works). Yeah the docs could probably be improved
> here. Below are the two links I know of in the docs which should get
> you started.
>
> http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/hoo...http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/ext...
>
> The @action decorator in pyramid_handlers is different because it is
> not found at scan-time. Rather, when you call add_handler(MyHandler),
> the class methods are scanned for @action decorators.
>
> HTH,
> Michael

I've looked into Venusian docs before, and 1st of the links you
provided is _exactly_ what I need. Wouldn't think to look for it in
Hooks chapter of docs, though. Thanks so much for your help.

Best regards,
Peter
Reply all
Reply to author
Forward
0 new messages