moksha/__init__.py | 8 +++--- moksha/api/widgets/feed/feed.py | 4 +-- moksha/controllers/root.py | 18 +++++++++++--- moksha/lib/helpers.py | 2 - moksha/middleware/middleware.py | 51 ++++++++++++++-------------------------- moksha/wsgiapp.py | 27 +++++++-------------- 6 files changed, 50 insertions(+), 60 deletions(-)
New commits: commit 2c7d701b518cc790edb15bf2aa1f166589685e84 Author: Luke Macken lmacken@redhat.com Date: Thu Feb 5 14:31:42 2009 -0500
Simplify up our Middleware, WSGIApp, and application dispatching.
We now dispatch application requests to Moksha's RootController.lookup method, instead of doing lots of crazy stuff in the moksha.wsgiapp.
diff --git a/moksha/controllers/root.py b/moksha/controllers/root.py index 1e50870..148e2a4 100644 --- a/moksha/controllers/root.py +++ b/moksha/controllers/root.py @@ -27,15 +27,19 @@ from widgetbrowser import WidgetBrowser from moksha import _ from moksha.model import DBSession from moksha.lib.base import BaseController -from moksha.controllers.secc import AdminController +#from moksha.controllers.secc import AdminController +from moksha.exc import ApplicationNotFound from moksha.controllers.error import ErrorController
from moksha.widgets.container import MokshaContainer container = MokshaContainer('moksha_container')
+#import os +#os.putenv('TW_BROWSER_PREFIX', '/docs') + class RootController(BaseController):
- admin = AdminController() + #admin = AdminController() error = ErrorController()
# ToscaWidgets WidgetBrowser integration @@ -45,6 +49,14 @@ class RootController(BaseController): full_stack=False, docs_dir='docs'))
+ @expose() + def lookup(self, resource, *remainder): + if resource == 'appz': + app = remainder[0] + if app not in moksha.apps: + raise AppNotFoundException(app) + return moksha.apps[app]['controller'], remainder[1:] + @expose('mako:moksha.templates.index') def index(self): tmpl_context.menu_widget = moksha.menus['default_menu'] @@ -54,6 +66,7 @@ class RootController(BaseController): @expose('mako:moksha.templates.widget') def widgets(self, widget, chrome=None, **kw): options = {} + options.update(kw) w = moksha._widgets.get(widget) if not w: raise WidgetNotFound(widget) @@ -64,7 +77,6 @@ class RootController(BaseController): options['id'] = widget + '_container' else: tmpl_context.widget = w['widget'] - options.update(kw) return dict(options=options)
@expose('moksha.templates.login') diff --git a/moksha/middleware/middleware.py b/moksha/middleware/middleware.py index 85d9071..4d06161 100644 --- a/moksha/middleware/middleware.py +++ b/moksha/middleware/middleware.py @@ -73,12 +73,7 @@ class MokshaMiddleware(object): self.register_stomp(environ) request = Request(environ) if request.path.startswith('/appz'): - app = request.path.split('/')[1] - environ['moksha.apps'] = self.apps - try: - response = request.get_response(self.mokshaapp) - except ApplicationNotFound: - response = Response(status='404 Not Found') + response = request.get_response(self.mokshaapp) else: response = request.get_response(self.application) return response(environ, start_response) diff --git a/moksha/wsgiapp.py b/moksha/wsgiapp.py index 8a2a911..6d74781 100644 --- a/moksha/wsgiapp.py +++ b/moksha/wsgiapp.py @@ -17,7 +17,9 @@ # Authors: Luke Macken lmacken@redhat.com
from pylons.wsgiapp import PylonsApp -from moksha.exc import ApplicationNotFound + +import moksha +from moksha.controllers.root import RootController
class MokshaAppDispatcher(PylonsApp): """ Moksha WSGI Application Dispatcher. @@ -26,6 +28,10 @@ class MokshaAppDispatcher(PylonsApp): It is instantiated and utilized by the :class:`moksha.middleware.MokshaMiddleware`. """ + def __init__(self, *args, **kw): + super(MokshaAppDispatcher, self).__init__(*args, **kw) + self.root = RootController() + def resolve(self, environ, start_response): """ Uses dispatching information found in ``environ['wsgiorg.routing_args']`` to retrieve the application @@ -33,20 +39,5 @@ class MokshaAppDispatcher(PylonsApp): moksha application.
""" - # Update the Routes config object in case we're using Routes - # (Do we even need/want this routes configuration?) - #config = request_config() - #config.redirect = self.redirect_to - # http://www.wsgi.org/wsgi/Specifications/routing_args - match = environ['wsgiorg.routing_args'][1] - - app = match['url'].split('/')[1] - if not app in environ['moksha.apps']: - raise ApplicationNotFound(app) - - # Remaining arguments - match['url'] = '/'.join(match['url'].strip('/').split('/')[2:]) - - environ['pylons.routes_dict'] = match - - return environ['moksha.apps'][app]['controller'] + environ['pylons.routes_dict'] = environ['wsgiorg.routing_args'][1] + return self.root
commit fef6c11bb85358c37cb81d9cd5f15121f0236394 Author: Luke Macken lmacken@redhat.com Date: Thu Feb 5 14:30:13 2009 -0500
Replace the unnecessary moksha.{apps,_widgets,menus} StackedObjectProxies and replace them with dictionaries setup by the Moksha Middleware.
diff --git a/moksha/__init__.py b/moksha/__init__.py index 4f6f386..e34a9dd 100644 --- a/moksha/__init__.py +++ b/moksha/__init__.py @@ -2,16 +2,16 @@ from pylons.i18n import ugettext as _ from paste.registry import StackedObjectProxy
# The central feed cache, used by the Feed widget. -feed_cache = StackedObjectProxy() +feed_cache = None
# All loaded moksha applications -apps = StackedObjectProxy() +apps = None
# All loaded ToscaWidgets -_widgets = StackedObjectProxy() +_widgets = None
# All loaded moksha menus -menus = StackedObjectProxy() +menus = None
# Per-request stomp callbacks registered by rendered widget stomp = StackedObjectProxy() diff --git a/moksha/api/widgets/feed/feed.py b/moksha/api/widgets/feed/feed.py index 252bda9..864b2fe 100644 --- a/moksha/api/widgets/feed/feed.py +++ b/moksha/api/widgets/feed/feed.py @@ -90,9 +90,9 @@ class Feed(Widget):
def iterentries(self, d=None): url = self.url or d.get('url') - try: + if moksha.feed_cache: feed = moksha.feed_cache.fetch(url) - except TypeError, e: + else: # MokshaMiddleware not running, so setup our own feed cache. # This allows us to use this object outside of WSGI requests. global cache diff --git a/moksha/lib/helpers.py b/moksha/lib/helpers.py index 74a2521..01f88a7 100644 --- a/moksha/lib/helpers.py +++ b/moksha/lib/helpers.py @@ -206,7 +206,7 @@ class MokshaApp(App):
def process(self): # We return a placeholder if the app is not registered - if not moksha._apps.has_key(self.app): + if not moksha.apps.has_key(self.app): return MokshaWidget(self.label, 'placeholder', self.content_id, {'appname':self.app}, self.auth).process() diff --git a/moksha/middleware/middleware.py b/moksha/middleware/middleware.py index 4e0ef8c..85d9071 100644 --- a/moksha/middleware/middleware.py +++ b/moksha/middleware/middleware.py @@ -49,14 +49,14 @@ class MokshaMiddleware(object):
""" def __init__(self, application): - log.info('Creating MokshaMiddleware') + log.info('Creating Moksha Middleware') self.application = application self.mokshaapp = MokshaAppDispatcher()
- self.apps = {} # {'app name': WSGI Controller} - self.menus = {} # {'menu name': MokshaMenu} - self.widgets = {} # {'widget name': tw.api.Widget} - self.engines = {} # {'app name': sqlalchemy.engine.base.Engine} + moksha.apps = {} # {'app name': tg.TGController} + moksha._widgets = {} # {'widget name': tw.api.Widget} + moksha.menus = {} # {'menu name': moksha.api.menus.MokshaMenu} + self.engines = {} # {'app name': sqlalchemy.engine.base.Engine}
self.load_paths() self.load_renderers() @@ -67,16 +67,11 @@ class MokshaMiddleware(object): self.load_menus()
self.feed_storage = Shove(config['feed_cache']) - self.feed_cache = Cache(self.feed_storage) + moksha.feed_cache = Cache(self.feed_storage)
def __call__(self, environ, start_response): - # environ['paste.registry'].register(moksha.apps, self.apps) - # environ['paste.registry'].register(moksha._widgets, self.widgets) - environ['paste.registry'].register(moksha.feed_cache, self.feed_cache) - environ['paste.registry'].register(moksha.menus, self.menus) self.register_stomp(environ) request = Request(environ) - if request.path.startswith('/appz'): app = request.path.split('/')[1] environ['moksha.apps'] = self.apps @@ -86,7 +81,6 @@ class MokshaMiddleware(object): response = Response(status='404 Not Found') else: response = request.get_response(self.application) - return response(environ, start_response)
def register_stomp(self, environ): @@ -107,20 +101,20 @@ class MokshaMiddleware(object): beforehand. """ for app_entry in pkg_resources.iter_entry_points('moksha.application'): - if app_entry.name in self.apps: + if app_entry.name in moksha.apps: raise MokshaException('Duplicate application name: %s' % app_entry.name) app_path = app_entry.dist.location - self.apps[app_entry.name] = { + moksha.apps[app_entry.name] = { 'name': app_entry.name, 'path': app_path, } for widget_entry in pkg_resources.iter_entry_points('moksha.widget'): - if widget_entry.name in self.widgets: + if widget_entry.name in moksha._widgets: raise MokshaException('Duplicate widget name: %s' % widget_entry.name) widget_path = widget_entry.dist.location - self.widgets[widget_entry.name] = { + moksha._widgets[widget_entry.name] = { 'name': widget_entry.name, 'path': widget_path, } @@ -131,7 +125,7 @@ class MokshaMiddleware(object): log.info('Loading %s application' % app_entry.name) app_class = app_entry.load() app_path = app_entry.dist.location - self.apps[app_entry.name] = { + moksha.apps[app_entry.name] = { 'name': getattr(app_class, 'name', app_entry.name), 'controller': app_class(), 'path': app_path, @@ -141,12 +135,10 @@ class MokshaMiddleware(object): model = __import__('%s.model' % app_entry.name, globals(), locals(), [app_entry.name]) - self.apps[app_entry.name]['model'] = model + moksha.apps[app_entry.name]['model'] = model except ImportError: pass
- moksha._apps = self.apps - def load_widgets(self): log.info('Loading moksha widgets') for widget_entry in pkg_resources.iter_entry_points('moksha.widget'): @@ -157,14 +149,14 @@ class MokshaMiddleware(object): widget = widget_class(widget_entry.name) else: widget = widget_class - self.widgets[widget_entry.name] = { + moksha._widgets[widget_entry.name] = { 'name': getattr(widget_class, '__name__', widget_entry.name), 'widget': widget, 'path': widget_path, }
- moksha._widgets = self.widgets + moksha._widgets = moksha._widgets
def load_menus(self): log.info('Loading moksha menus') @@ -172,7 +164,7 @@ class MokshaMiddleware(object): log.info('Loading %s menu' % menu_entry.name) menu_class = menu_entry.load() menu_path = menu_entry.dist.location - self.menus[menu_entry.name] = menu_class(menu_entry.name) + moksha.menus[menu_entry.name] = menu_class(menu_entry.name)
def load_renderers(self): """ Load our template renderers with our application paths. @@ -183,7 +175,7 @@ class MokshaMiddleware(object): """ #template_paths = config['pylons.paths']['templates'] #moksha_dir = os.path.abspath(__file__ + '/../../../') - #for app in [{'path': moksha_dir}] + self.apps.values(): + #for app in [{'path': moksha_dir}] + moksha.apps.values(): # if app['path'] not in template_paths: # template_paths.append(app['path'])
@@ -268,7 +260,7 @@ class MokshaMiddleware(object):
""" moksha_conf = os.path.abspath(__file__ + '/../../../') - for app in [{'path': moksha_conf}] + self.apps.values(): + for app in [{'path': moksha_conf}] + moksha.apps.values(): for configfile in ('production.ini', 'development.ini'): confpath = os.path.join(app['path'], configfile) if os.path.exists(confpath): @@ -294,7 +286,7 @@ class MokshaMiddleware(object): if they don't already exist.
""" - for name, app in self.apps.items(): + for name, app in moksha.apps.items(): if app.get('model'): log.debug('Creating database engine for %s' % app['name']) self.engines[name] = create_engine(config['app_db'] % name)