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.
This commit is contained in:
Neil Williams
2013-03-21 17:55:05 -07:00
parent 84e57785f4
commit 5bd81b5f6e
2 changed files with 28 additions and 12 deletions

View File

@@ -29,6 +29,8 @@ from r2.config.environment import load_environment
from paste.script.pluginlib import find_egg_info_dir
from pylons.wsgiapp import PylonsApp
from r2.config.middleware import RedditApp
#from pylons.commands import ShellCommand, ControllerCommand, \
# RestControllerCommand
@@ -64,7 +66,8 @@ class RunCommand(command.Command):
here_dir = os.getcwd()
if self.args[0].lower() == 'standalone':
is_standalone = self.args[0].lower() == 'standalone'
if is_standalone:
load_environment(setup_globals=False)
else:
config_name = 'config:%s' % self.args[0]
@@ -81,7 +84,12 @@ class RunCommand(command.Command):
sys.path.insert(0, here_dir)
# Load the wsgi app first so that everything is initialized right
wsgiapp = RegistryManager(PylonsApp())
if not is_standalone:
wsgiapp = RegistryManager(RedditApp())
else:
# in standalone mode we don't have an ini so we can't use
# RedditApp since it imports all the fancy controllers.
wsgiapp = RegistryManager(PylonsApp())
test_app = paste.fixture.TestApp(wsgiapp)
# Query the test app to setup the environment

View File

@@ -21,6 +21,7 @@
###############################################################################
"""Pylons middleware initialization"""
import importlib
import re
import urllib
import tempfile
@@ -363,22 +364,29 @@ class RedditApp(PylonsApp):
self._loading_lock = Lock()
self._controllers = None
def load_controllers(self):
with self._loading_lock:
if not self._controllers:
controllers = __import__(self.package_name + '.controllers').controllers
controllers.load_controllers()
config['r2.plugins'].load_controllers()
self._controllers = controllers
def setup_app_env(self, environ, start_response):
PylonsApp.setup_app_env(self, environ, start_response)
self.load_controllers()
return self._controllers
def load_controllers(self):
if self._controllers:
return
with self._loading_lock:
if self._controllers:
return
controllers = importlib.import_module(self.package_name +
'.controllers')
controllers.load_controllers()
config['r2.plugins'].load_controllers()
self._controllers = controllers
def find_controller(self, controller_name):
if controller_name in self.controller_classes:
return self.controller_classes[controller_name]
controllers = self.load_controllers()
controller_cls = controllers.get_controller(controller_name)
controller_cls = self._controllers.get_controller(controller_name)
self.controller_classes[controller_name] = controller_cls
return controller_cls