Skip to content
This repository
Browse code

plugins: Ensure load_controllers is called in paster shell/run as well.

load_controllers is the safe place to do imports of things that rely on
`g` et al being in the correct state. It was not being called in paster
shell or run which meant that any hooks defined in a module that relies
on `g` at module scope (e.g. things importing model or controller code)
would not be connected in those contexts.
  • Loading branch information...
commit 5bd81b5f6e1da5140288dde0c9a18feb83cd45a8 1 parent 84e5778
Neil Williams authored March 21, 2013
12  r2/r2/commands.py
@@ -29,6 +29,8 @@
29 29
 from paste.script.pluginlib import find_egg_info_dir
30 30
 from pylons.wsgiapp import PylonsApp
31 31
 
  32
+from r2.config.middleware import RedditApp
  33
+
32 34
 #from pylons.commands import ShellCommand, ControllerCommand, \
33 35
 #     RestControllerCommand
34 36
 
@@ -64,7 +66,8 @@ def command(self):
64 66
 
65 67
         here_dir = os.getcwd()
66 68
 
67  
-        if self.args[0].lower() == 'standalone':
  69
+        is_standalone = self.args[0].lower() == 'standalone'
  70
+        if is_standalone:
68 71
             load_environment(setup_globals=False)
69 72
         else:
70 73
             config_name = 'config:%s' % self.args[0]
@@ -81,7 +84,12 @@ def command(self):
81 84
         sys.path.insert(0, here_dir)
82 85
 
83 86
         # Load the wsgi app first so that everything is initialized right
84  
-        wsgiapp = RegistryManager(PylonsApp())
  87
+        if not is_standalone:
  88
+            wsgiapp = RegistryManager(RedditApp())
  89
+        else:
  90
+            # in standalone mode we don't have an ini so we can't use
  91
+            # RedditApp since it imports all the fancy controllers.
  92
+            wsgiapp = RegistryManager(PylonsApp())
85 93
         test_app = paste.fixture.TestApp(wsgiapp)
86 94
 
87 95
         # Query the test app to setup the environment
24  r2/r2/config/middleware.py
@@ -21,6 +21,7 @@
21 21
 ###############################################################################
22 22
 
23 23
 """Pylons middleware initialization"""
  24
+import importlib
24 25
 import re
25 26
 import urllib
26 27
 import tempfile
@@ -363,22 +364,29 @@ def __init__(self, *args, **kwargs):
363 364
         self._loading_lock = Lock()
364 365
         self._controllers = None
365 366
 
  367
+    def setup_app_env(self, environ, start_response):
  368
+        PylonsApp.setup_app_env(self, environ, start_response)
  369
+        self.load_controllers()
  370
+
366 371
     def load_controllers(self):
  372
+        if self._controllers:
  373
+            return
  374
+
367 375
         with self._loading_lock:
368  
-            if not self._controllers:
369  
-                controllers = __import__(self.package_name + '.controllers').controllers
370  
-                controllers.load_controllers()
371  
-                config['r2.plugins'].load_controllers()
372  
-                self._controllers = controllers
  376
+            if self._controllers:
  377
+                return
373 378
 
374  
-        return self._controllers
  379
+            controllers = importlib.import_module(self.package_name +
  380
+                                                  '.controllers')
  381
+            controllers.load_controllers()
  382
+            config['r2.plugins'].load_controllers()
  383
+            self._controllers = controllers
375 384
 
376 385
     def find_controller(self, controller_name):
377 386
         if controller_name in self.controller_classes:
378 387
             return self.controller_classes[controller_name]
379 388
 
380  
-        controllers = self.load_controllers()
381  
-        controller_cls = controllers.get_controller(controller_name)
  389
+        controller_cls = self._controllers.get_controller(controller_name)
382 390
         self.controller_classes[controller_name] = controller_cls
383 391
         return controller_cls
384 392
 

0 notes on commit 5bd81b5

Please sign in to comment.
Something went wrong with that request. Please try again.