[etwist] pass the repository to the root resource
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Tue, 18 Jun 2013 15:12:25 +0200
changeset 9036 cf519fd876c6
parent 9035 63f3d25bab14
child 9037 6fd0ac6506cb
[etwist] pass the repository to the root resource This is another step toward a cleaner instantiation scheme for the repo. The http test now pass an already created object to the etwist server instead of relying on the config cache. Related to #2249513
devtools/httptest.py
etwist/server.py
--- a/devtools/httptest.py	Mon Jun 24 12:03:37 2013 +0200
+++ b/devtools/httptest.py	Tue Jun 18 15:12:25 2013 +0200
@@ -101,7 +101,7 @@
 
         reactor.addSystemEventTrigger('after', 'startup', semaphore.release)
         t = threading.Thread(target=safe_run, name='cubicweb_test_web_server',
-                             args=(self.config, True))
+                args=(self.config, True), kwargs={'repo': self.repo})
         self.web_thread = t
         t.start()
         semaphore.acquire()
--- a/etwist/server.py	Mon Jun 24 12:03:37 2013 +0200
+++ b/etwist/server.py	Tue Jun 18 15:12:25 2013 +0200
@@ -57,12 +57,11 @@
 
 
 class CubicWebRootResource(resource.Resource):
-    def __init__(self, config):
+    def __init__(self, config, repo):
         resource.Resource.__init__(self)
         self.config = config
         # instantiate publisher here and not in init_publisher to get some
         # checks done before daemonization (eg versions consistency)
-        repo = config.repository()
         self.appli = CubicWebPublisher(repo, config)
         self.base_url = config['base-url']
         self.https_url = config['https-url']
@@ -271,12 +270,20 @@
 LOGGER = getLogger('cubicweb.twisted')
 set_log_methods(CubicWebRootResource, LOGGER)
 
-def run(config, debug=None):
+def run(config, debug=None, repo=None):
+    # repo may by passed during test.
+    #
+    # Test has already created a repo object so we should not create a new one.
+    # Explicitly passing the repo object avoid relying on the fragile
+    # config.repository() cache. We could imagine making repo a mandatory
+    # argument and receives it from the starting command directly.
     if debug is not None:
         config.debugmode = debug
     config.check_writeable_uid_directory(config.appdatahome)
     # create the site
-    root_resource = CubicWebRootResource(config)
+    if repo is None:
+        repo = config.repository()
+    root_resource = CubicWebRootResource(config, repo)
     website = server.Site(root_resource)
     # serve it via standard HTTP on port set in the configuration
     port = config['port'] or 8080