[config] Read pyramid settings in a 'pyramid.ini' file
authorChristophe de Vienne <christophe@unlish.com>
Sat, 03 Jan 2015 02:36:06 +0100
changeset 11534 ceb1a5baca4f
parent 11533 4ced3782b90f
child 11535 dd875009cc47
[config] Read pyramid settings in a 'pyramid.ini' file If a 'pyramid-debug.ini' file is present, it will be used instead when debugmode is on. Closes #4811298
pyramid_cubicweb/__init__.py
--- a/pyramid_cubicweb/__init__.py	Sat Jan 03 01:24:38 2015 +0100
+++ b/pyramid_cubicweb/__init__.py	Sat Jan 03 02:36:06 2015 +0100
@@ -3,6 +3,12 @@
 
 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
 from pyramid.config import Configurator
+from pyramid.settings import asbool, aslist
+
+try:
+    from configparser import SafeConfigParser
+except ImportError:
+    from ConfigParser import SafeConfigParser
 
 
 def make_cubicweb_application(cwconfig):
@@ -11,24 +17,44 @@
 
     It is initialy meant to be used by the 'pyramid' command of cubicweb-ctl.
     """
-    settings = {
-        'session.secret': '11',  # XXX
-    }
+    settings_filenames = [os.path.join(cwconfig.apphome, 'pyramid.ini')]
+
+    settings = {}
+
     if cwconfig.debugmode:
+        settings_filenames.insert(
+            0, os.path.join(cwconfig.apphome, 'pyramid-debug.ini'))
+
         settings.update({
             'pyramid.debug_authorization': True,
             'pyramid.debug_notfound': True,
             'pyramid.debug_routematch': True,
         })
+
+    for fname in settings_filenames:
+        if os.path.exists(fname):
+            cp = SafeConfigParser()
+            cp.read(fname)
+            settings.update(cp.items('main'))
+            break
+
     config = Configurator(settings=settings)
     if cwconfig.debugmode:
         config.include('pyramid_debugtoolbar')
     config.registry['cubicweb.config'] = cwconfig
     config.registry['cubicweb.repository'] = repo = cwconfig.repository()
     config.registry['cubicweb.registry'] = repo.vreg
-    config.include('pyramid_cubicweb.defaults')
+
+    if asbool(config.registry.settings.get('cubicweb.defaults', True)):
+        config.include('pyramid_cubicweb.defaults')
+
+    for name in aslist(config.registry.settings.get('cubicweb.includes')):
+        config.include(name)
+
     config.include('pyramid_cubicweb.core')
-    config.include('pyramid_cubicweb.bwcompat')
+
+    if asbool(config.registry.settings.get('cubicweb.bwcompat', True)):
+        config.include('pyramid_cubicweb.bwcompat')
     return config