[pyramid] Make it possible to setup CubicWeb instance from an existing repo in includeme()
authorDenis Laxalde <denis.laxalde@logilab.fr>
Tue, 21 Feb 2017 17:35:16 +0100
changeset 11970 b282ef22b5ab
parent 11969 2fb941d12d74
child 11971 6c035cfef04f
[pyramid] Make it possible to setup CubicWeb instance from an existing repo in includeme()
cubicweb/pyramid/__init__.py
--- a/cubicweb/pyramid/__init__.py	Tue Feb 21 17:42:46 2017 +0100
+++ b/cubicweb/pyramid/__init__.py	Tue Feb 21 17:35:16 2017 +0100
@@ -26,6 +26,7 @@
 
 from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
 from pyramid.config import Configurator
+from pyramid.exceptions import ConfigurationError
 from pyramid.settings import asbool, aslist
 
 try:
@@ -178,6 +179,12 @@
 
     The CubicWeb instance can be set in several ways:
 
+    -   Provide an already loaded CubicWeb repository in the registry:
+
+        .. code-block:: python
+
+            config.registry['cubicweb.repository'] = your_repo_instance
+
     -   Provide an already loaded CubicWeb config instance in the registry:
 
         .. code-block:: python
@@ -187,8 +194,24 @@
     -   Provide an instance name in the pyramid settings with
         :confval:`cubicweb.instance`.
 
+    A CubicWeb repository is instantiated and attached in
+    'cubicweb.repository' registry key if not already present.
+
+    The CubicWeb instance registry is attached in 'cubicweb.registry' registry
+    key.
     """
     cwconfig = config.registry.get('cubicweb.config')
+    repo = config.registry.get('cubicweb.repository')
+
+    if repo is not None:
+        if cwconfig is None:
+            config.registry['cubicweb.config'] = cwconfig = repo.config
+        elif cwconfig is not repo.config:
+            raise ConfigurationError(
+                'CubicWeb config instance (found in "cubicweb.config" '
+                'registry key) mismatches with that of the repository '
+                '(registry["cubicweb.repository"])'
+            )
 
     if cwconfig is None:
         debugmode = asbool(
@@ -197,7 +220,8 @@
             config.registry.settings['cubicweb.instance'], debugmode=debugmode)
         config.registry['cubicweb.config'] = cwconfig
 
-    config.registry['cubicweb.repository'] = repo = cwconfig.repository()
+    if repo is None:
+        repo = config.registry['cubicweb.repository'] = cwconfig.repository()
     config.registry['cubicweb.registry'] = repo.vreg
 
     if asbool(config.registry.settings.get('cubicweb.defaults', True)):