mirror of
https://github.com/reddit-archive/reddit.git
synced 2026-04-05 03:00:15 -04:00
Split plugin loading into two phases.
The first phase loads the entry points and instantiates plugin objects. This phase is useful for inspecting plugin metadata without having a full environment configured (routing etc.). This phase is moved earlier in the init process, to the first moment after the configuration has been parsed. The second phase does the heavy work of making individual plugins inject themselves into the otherwise ready reddit environment. This phase stays where plugin loading took place before (after global setup).
This commit is contained in:
@@ -30,7 +30,6 @@ import r2.lib.helpers
|
||||
from r2.config import routing
|
||||
from r2.lib.app_globals import Globals
|
||||
from r2.lib.configparse import ConfigValue
|
||||
from r2.lib.plugin import PluginLoader
|
||||
from r2.templates import tmpl_dirs
|
||||
|
||||
|
||||
@@ -58,10 +57,10 @@ def load_environment(global_conf={}, app_conf={}, setup_globals=True):
|
||||
if setup_globals:
|
||||
g.setup()
|
||||
r2.config.cache = g.cache
|
||||
g.plugins.load_plugins()
|
||||
config['r2.plugins'] = g.plugins
|
||||
|
||||
config['pylons.h'] = r2.lib.helpers
|
||||
|
||||
g.plugins = config['r2.plugins'] = PluginLoader().load_plugins(g.config.get('plugins', []))
|
||||
config['routes.map'] = routing.make_map()
|
||||
|
||||
#override the default response options
|
||||
|
||||
@@ -40,6 +40,8 @@ from r2.lib.translation import get_active_langs, I18N_PATH
|
||||
from r2.lib.lock import make_lock_factory
|
||||
from r2.lib.manager import db_manager
|
||||
from r2.lib.stats import Stats, CacheStats, StatsCollectingConnectionPool
|
||||
from r2.lib.plugin import PluginLoader
|
||||
|
||||
|
||||
class Globals(object):
|
||||
spec = {
|
||||
@@ -184,6 +186,7 @@ class Globals(object):
|
||||
|
||||
self.config = ConfigValueParser(global_conf)
|
||||
self.config.add_spec(self.spec)
|
||||
self.plugins = PluginLoader(self.config.get("plugins", []))
|
||||
|
||||
self.paths = paths
|
||||
|
||||
|
||||
@@ -66,9 +66,20 @@ class Plugin(object):
|
||||
|
||||
|
||||
class PluginLoader(object):
|
||||
def __init__(self):
|
||||
def __init__(self, plugin_names):
|
||||
self.plugins = {}
|
||||
|
||||
for name in plugin_names:
|
||||
try:
|
||||
entry_point = self.available_plugins(name).next()
|
||||
except StopIteration:
|
||||
print >> sys.stderr, ("Unable to locate plugin "
|
||||
"%s. Skipping." % name)
|
||||
continue
|
||||
|
||||
plugin_cls = entry_point.load()
|
||||
self.plugins[name] = plugin_cls()
|
||||
|
||||
def __len__(self):
|
||||
return len(self.plugins)
|
||||
|
||||
@@ -91,21 +102,13 @@ class PluginLoader(object):
|
||||
return None
|
||||
return os.path.join(plugin.dist.location, plugin.module_name)
|
||||
|
||||
def load_plugins(self, plugin_names):
|
||||
def load_plugins(self):
|
||||
g = config['pylons.g']
|
||||
for name in plugin_names:
|
||||
try:
|
||||
entry_point = self.available_plugins(name).next()
|
||||
except StopIteration:
|
||||
g.log.warning('Unable to locate plugin "%s". Skipping.' % name)
|
||||
continue
|
||||
plugin_cls = entry_point.load()
|
||||
plugin = self.plugins[name] = plugin_cls()
|
||||
for plugin in self:
|
||||
g.config.add_spec(plugin.config)
|
||||
config['pylons.paths']['templates'].extend(plugin.template_dirs)
|
||||
plugin.add_js()
|
||||
plugin.on_load()
|
||||
return self
|
||||
|
||||
def load_controllers(self):
|
||||
for plugin in self:
|
||||
|
||||
Reference in New Issue
Block a user