mirror of
https://github.com/reddit-archive/reddit.git
synced 2026-01-27 07:48:16 -05:00
Add basic plugin support.
This commit is contained in:
@@ -29,16 +29,16 @@ mimetypes.init()
|
||||
|
||||
import webhelpers
|
||||
|
||||
from r2.config.routing import make_map
|
||||
from r2.config import routing
|
||||
import r2.lib.app_globals as app_globals
|
||||
from r2.lib import rpc
|
||||
import r2.lib.helpers
|
||||
from r2.lib.plugin import load_plugins
|
||||
import r2.config as reddit_config
|
||||
|
||||
from r2.templates import tmpl_dirs
|
||||
|
||||
def load_environment(global_conf={}, app_conf={}, setup_globals=True):
|
||||
map = make_map(global_conf, app_conf)
|
||||
# Setup our paths
|
||||
root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
@@ -57,7 +57,9 @@ def load_environment(global_conf={}, app_conf={}, setup_globals=True):
|
||||
reddit_config.cache = g.cache
|
||||
|
||||
config['pylons.h'] = r2.lib.helpers
|
||||
config['routes.map'] = map
|
||||
|
||||
config['r2.plugins'] = load_plugins(getattr(g, 'plugins', []))
|
||||
config['routes.map'] = routing.make_map()
|
||||
|
||||
#override the default response options
|
||||
config['pylons.response_options']['headers'] = {}
|
||||
|
||||
@@ -24,12 +24,16 @@ Setup your Routes options here
|
||||
"""
|
||||
import os
|
||||
from routes import Mapper
|
||||
from pylons import config
|
||||
import admin_routes
|
||||
|
||||
def make_map(global_conf={}, app_conf={}):
|
||||
def make_map():
|
||||
map = Mapper()
|
||||
mc = map.connect
|
||||
|
||||
for plugin in config['r2.plugins'].itervalues():
|
||||
plugin.add_routes(mc)
|
||||
|
||||
admin_routes.add(mc)
|
||||
|
||||
mc('/login', controller='forms', action='login')
|
||||
|
||||
@@ -66,3 +66,7 @@ from oauth2 import OAuth2AccessController as Oauth2accessController
|
||||
from admin import AdminController
|
||||
from redirect import RedirectController
|
||||
from ipn import IpnController
|
||||
|
||||
from pylons import config
|
||||
for plugin in config['r2.plugins'].itervalues():
|
||||
locals().update(plugin.load_controllers())
|
||||
|
||||
@@ -170,6 +170,7 @@ class Globals(object):
|
||||
],
|
||||
|
||||
ConfigValue.tuple: [
|
||||
'plugins',
|
||||
'stalecaches',
|
||||
'memcaches',
|
||||
'lockcaches',
|
||||
|
||||
37
r2/r2/lib/plugin.py
Normal file
37
r2/r2/lib/plugin.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import sys
|
||||
import os.path
|
||||
import pkg_resources
|
||||
from pylons import config
|
||||
|
||||
|
||||
class Plugin(object):
|
||||
@property
|
||||
def path(self):
|
||||
module = sys.modules[type(self).__module__]
|
||||
return os.path.dirname(module.__file__)
|
||||
|
||||
@property
|
||||
def template_dirs(self):
|
||||
"""Add module/templates/ as a template directory."""
|
||||
return [os.path.join(self.path, 'templates')]
|
||||
|
||||
@property
|
||||
def static_dir(self):
|
||||
return os.path.join(self.path, 'public')
|
||||
|
||||
def add_routes(self, mc):
|
||||
pass
|
||||
|
||||
|
||||
def load_plugins(plugin_names):
|
||||
plugins = {}
|
||||
for name in plugin_names:
|
||||
try:
|
||||
entry_point = pkg_resources.iter_entry_points('r2.plugin', name).next()
|
||||
except StopIteration:
|
||||
config['pylons.g'].log.warning('Unable to locate plugin "%s". Skipping.' % name)
|
||||
continue
|
||||
plugin_cls = entry_point.load()
|
||||
plugin = plugins[name] = plugin_cls()
|
||||
config['pylons.paths']['templates'].extend(plugin.template_dirs)
|
||||
return plugins
|
||||
Reference in New Issue
Block a user