Accessing configuration

1 view
Skip to first unread message

Aly Merchant

unread,
Jun 23, 2009, 2:03:26 PM6/23/09
to TurboGears
I'm having some trouble with accessing config parameters. The docs
indicate that something like:

tg.config.get('mail.from.administrator', 'notcon...@nodomain.com')

should work. However, I am not able to access anything I have set.
I'm not sure if tg.config is supposed to be a dict but I am see it
populated with:

{'pylons.c_attach_args': True, 'buffet.template_options': {},
'pylons.request_options': {'errors': 'replace', 'decode_param_names':
False, 'charset': 'utf-8', 'language': 'en-us'}, 'pylons.paths':
{'controllers': None, 'templates': [], 'static_files': None, 'root':
None}, 'pylons.environ_config': {'session': 'beaker.session', 'cache':
'beaker.cache'}, 'pylons.db_engines': {}, 'pylons.strict_c': False,
'pylons.h': None, 'pylons.package': None, 'debug': False, 'pylons.g':
None, 'buffet.template_engines': [], 'pylons.response_options':
{'headers': {'Pragma': 'no-cache', 'Cache-Control': 'no-cache'},
'errors': 'strict', 'charset': 'utf-8', 'content_type': 'text/html'}}

This is with 2.0.1, I'm running in a virtualenv setup with the default
"paster server development.ini" command.

Thanks,
Aly

percious

unread,
Jun 23, 2009, 7:33:12 PM6/23/09
to TurboGears
Try using getattr instead of get. the TGConfig is a "Bunch" object.

cheers.
-chris

On Jun 23, 12:03 pm, Aly Merchant <alymerch...@gmail.com> wrote:
> I'm having some trouble with accessing config parameters.  The docs
> indicate that something like:
>
> tg.config.get('mail.from.administrator', 'notconfigu...@nodomain.com')

Aly Merchant

unread,
Jun 24, 2009, 3:30:30 PM6/24/09
to TurboGears
Still no luck: getattr will work with arguments like 'pylons.environ',
but I cannot find a way to access custom settings. I am using the
[default] section in development.ini .

KMCB

unread,
Jun 24, 2009, 7:51:21 PM6/24/09
to TurboGears
Aly,

This seems to work for me. Hope it helps.

Regards,
KMCB


from tg import config
import os
.
.
.
permanent_file = open(os.path.join(
config['app_conf']['permanent_store'],
fileobj.filename.replace(os.sep, '_')
),
'wb'
)

Aly Merchant

unread,
Jun 29, 2009, 9:30:50 AM6/29/09
to TurboGears
I was able to figure out the cause of the issue. It seems that the
code was executed before the configuration had been fully read and I
was trying to read out configuration options when the module was
loaded.

Is there some way to ensure that the configuration has been read
before this point? Or is there a better place to move this code (it
was under a module I added under model)?

Thanks,
Aly

Diez B. Roggisch

unread,
Jul 6, 2009, 5:05:36 AM7/6/09
to turbo...@googlegroups.com
On Monday 29 June 2009 15:30:50 Aly Merchant wrote:
> I was able to figure out the cause of the issue. It seems that the
> code was executed before the configuration had been fully read and I
> was trying to read out configuration options when the module was
> loaded.
>
> Is there some way to ensure that the configuration has been read
> before this point? Or is there a better place to move this code (it
> was under a module I added under model)?

Try not accessing config during import, but instead provide an explicit
init-function which you call from e.g. yourapp/config/middleware.py.

Thus you can be sure the proper config is in place.

Diez

Reply all
Reply to author
Forward
0 new messages