Correct way of detecting application package in third party package

69 views
Skip to first unread message

askel

unread,
Sep 18, 2012, 9:57:16 AM9/18/12
to pylons-discuss
I'm developing renderer library for Pyramid framework. It's just kind
of adapter to use existing template engine in Pyramid applications.
The library is using asset specs for template names. And I'd like it
to use application package name in case asset spec is missing package
part. Originally, I used caller_package

from pyramid.path import caller_package, AssetResolver

def includeme(config):
package = caller_package(3)
resolver = AssetResolver(package)

While that worked for "imperative" configuration method (by calling
config.include('my.library.package')), it did not work for
"declarative" one (via pyramid.includes in INI file).

I changed it to the following:

def includeme(config):
package = config.registry.__name__
resolver = AssetResolver(package)

And this way it works for both configuration methods. But I'm afraid
that I'm using undocumented implementation details and that could stop
working in the future.

Is there any "official" way to determine application package name?

- Alex K

Chris McDonough

unread,
Sep 18, 2012, 12:21:35 PM9/18/12
to pylons-...@googlegroups.com

askel

unread,
Sep 18, 2012, 2:09:00 PM9/18/12
to pylons-...@googlegroups.com
Hi Chris,

Thanks for your reply. The question is not how to convert relative asset spec to absolute one but how to get package_name attribute of the topmost configurator given configurator passed to includeme(config) of third party library.

- Alex K

Robert Forkel

unread,
Jul 23, 2013, 4:30:53 AM7/23/13
to pylons-...@googlegroups.com
Hi all,
I just came across the same problem as OP: How do I determine the "application" package from an includeme function. I found the two attributes "package" and "package_name" of the Configurator object and think this is what I want. But since these are not documented as public API, is it safe to access them? Or may this change with any new pyramid version?
best
robert

Chris McDonough

unread,
Jul 23, 2013, 4:50:17 AM7/23/13
to pylons-...@googlegroups.com
It's undefined. I'd suggest setting something in registry.settings and
using it if it exists.
> --
> You received this message because you are subscribed to the Google
> Groups "pylons-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to pylons-discus...@googlegroups.com.
> To post to this group, send email to pylons-...@googlegroups.com.
> Visit this group at http://groups.google.com/group/pylons-discuss.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>


askel

unread,
Jul 23, 2013, 10:44:47 AM7/23/13
to pylons-...@googlegroups.com
Robert,

Package and package_name of configurator passed to includeme reference includeme's package and not the application package.

- Alex K

Robert Forkel

unread,
Jul 24, 2013, 6:32:20 AM7/24/13
to pylons-discuss
Hi Alex,
I noticed this, and was a bit confused at first, because I had inspected config.package in a different function where config was passed in. So I ended up not using the "includeme" mechanism, which is not a big deal, because I'm only using imperative configuration anyway.
best
robert


--
Reply all
Reply to author
Forward
0 new messages