Accessing INI configuration from within Pyramid

660 views
Skip to first unread message

Andreas Jung

unread,
Jan 17, 2013, 6:25:15 AM1/17/13
to pylons-...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I added some extra app specific sections to the ini file of
my Pyramid application. How can I get hold of the settings
from within my app (pyramid.config)?

Andreas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQGUBAEBAgAGBQJQ99+aAAoJEADcfz7u4AZjox4LvjlkavWCKerUcGUVWLwufSo6
A0BkOzOwunj+U0j+RGUWt2ujaJ8HN3q7tySF1Kth/LObAFuqCACBQCkRzWfQF5xD
M7v7V6XrnoEbt78myIv9/eQwHXCFJhECgNycLwnGKHYfLCqv44qGvQok0yi5g2py
2SingfsQjPEv9J7hUL68ouW5rsUmNdwgxiZf2+crsHyrE3JUkCxZcNJK0J3S9g7D
7JYS6+BR0uAoKetEzDGSvKWppU6PesYqbf0tGhZVg9/Bxu0j5+jHWMihS3BvHab2
T7JcIEnrH1g8OI9PSabsD2WKdZQG5Lq8gsEj7ZENoXKKNgCGlaGd0oNxUaPwqalr
acWMbGIXRq4woOghNt4chyfP5XtvBbDC7iuGlB++tQyoWg2yxIQidQTJqqF+kmJY
jq9Qoxtd4Cw/ud1QZL/tMK5UfxYx80n5GOFr4aTJ/507c40WZ7LV0bcVDOYkjDSc
FRhF86KPlqvECWRAMAOz8waoM/5KTYE=
=oDpZ
-----END PGP SIGNATURE-----
lists.vcf

Paul Everitt

unread,
Jan 17, 2013, 6:43:57 AM1/17/13
to pylons-...@googlegroups.com, li...@zopyx.com
http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html#adding-a-custom-setting

You can use request.registry.settings, config.registry.settings, and other means, depending on the place in the code.

--Paul

Paul Everitt

unread,
Jan 17, 2013, 6:45:07 AM1/17/13
to pylons-...@googlegroups.com, li...@zopyx.com

Just realized, you said a separate *section*.

--Paul

Wyatt Baldwin

unread,
Jan 17, 2013, 12:04:28 PM1/17/13
to pylons-...@googlegroups.com, li...@zopyx.com
On Thursday, January 17, 2013 3:25:15 AM UTC-8, Andreas Jung wrote:
I added some extra app specific sections to the ini file of
my Pyramid application. How can I get hold of the settings
from within my app (pyramid.config)?

Pyramid doesn't automatically parse settings. You can use the `pyramid.paster.get_appsettings(config_uri, name=None)` function to get at them.

Reference: http://docs.pylonsproject.org/projects/pyramid/en/latest/api/paster.html#pyramid.paster.get_appsettings

Chris McDonough

unread,
Jan 17, 2013, 12:11:51 PM1/17/13
to pylons-...@googlegroups.com, li...@zopyx.com
Fraid all the answers I've seen so far are incorrect.

Pyramid doesn't know beans about sections other than the "app" secrion
in the config file. You'll need to use ConfigParser to parse the config
file again within your app.

- c



Michael Merickel

unread,
Jan 17, 2013, 12:25:22 PM1/17/13
to Pylons
Building on Chris's answer, you can get the path of the INI file easily in normal situations by checking global_config['__file__'] in your typical def main(global_config, **settings) signature.





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


Wyatt Baldwin

unread,
Jan 17, 2013, 1:08:04 PM1/17/13
to pylons-...@googlegroups.com, li...@zopyx.com
 
My suggestion will work, as long as the section looks like a normal app section:

[app:mysettings]
use = call:my.package:no_op
a = 1
b = 2

Where the `no_op` function would look like this:

    def no_op(global_config, **settings): pass

To me, that's a lot easier than manually using ConfigParser.


Wyatt Baldwin

unread,
Jan 17, 2013, 2:56:44 PM1/17/13
to pylons-...@googlegroups.com, li...@zopyx.com


Here's a gist demonstrating this approach: https://gist.github.com/4559038

Personally, I find ConfigParser (or is it RawConfigParser? SafeConfigParser?) a bit wonky. Pyramid (PasteDeploy under the hood) already adds a nice abstraction layer on top of it, so you might as well use it.


 
Reply all
Reply to author
Forward
0 new messages