When I created a default pyramid app from a cookie cutter, it resulted in an INI file with sections like this:
[app:myapp]
use = egg:myproject#myapp
pyramid.reload_templates = true
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
pyramid.includes = pyramid_debugtoolbar
Now I'm experimenting with adding these same settings in python code instead, using the Configurator
object in __init__.py
, and I find that the following appears to work the same:
config.include('pyramid_debugtoolbar')
config.add_settings({
'pyramid.reload_templates' : 'true',
'pyramid.debug_authorization' : 'false',
'pyramid.debug_notfound' : 'false',
'pyramid.debug_routematch' : 'false',
'pyramid.default_locale_name' : 'en',
'pyramid.includes' : 'pyramid_debugtoolbar',
})
But when applying these setting in python, the first line config.include('pyramid_debugtoolbar')
is required or it doesn't work. Yet, in the INI version, it's sufficient to set pyramid.includes = pyramid_debugtoolbar
.
After Further Digging
Looking higher up the stack in my code, I found that the setting does work this way...
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application."""
settings.update({'pyramid.includes':'pyramid_debugtoolbar'}) # SETTING HERE WORKS!
with Configurator(settings=settings) as config:
config.include(common_config)
config.include('.routes')
config.scan()
return config.make_wsgi_app()
But NOT this way...
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application."""
with Configurator(settings=settings) as config:
config.add_settings({'pyramid.includes':'pyramid_debugtoolbar'}) # NO EFFECT!
config.include(common_config)
config.include('.routes')
config.scan()
return config.make_wsgi_app()
In the documentation for pyramid.config, I found this warning that I suspect is what I'm dealing with:
A configuration callable should be a callable that accepts a single argument named config, which will be an instance of a Configurator. However, be warned that it will not be the same configurator instance on which you call this method. The code which runs as a result of calling the callable should invoke methods on the configurator passed to it which add configuration state. The return value of a callable will be ignored.
In an effort to guess at the solution, I tried wrapping my config.add_settings(...)
with various combinations of config.commit()
and config.begin()
/config.end()
, and none of those worked either.
My Question:
How do I use config.add_settings(...)
to set pyramid.includes
? I want to do this in a common_config()
callable that is included by multiple pyramid apps.