[Mpuls-commits] r1215 - wasko/branches/2.0/waskaweb/config

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Feb 3 14:53:28 CET 2010


Author: torsten
Date: 2010-02-03 14:53:28 +0100 (Wed, 03 Feb 2010)
New Revision: 1215

Modified:
   wasko/branches/2.0/waskaweb/config/middleware.py
Log:
* waskaweb/config/middleware.py (MyPylonsApp.find_controller): Search
  for controller first in instance dir. Then in basedir


Modified: wasko/branches/2.0/waskaweb/config/middleware.py
===================================================================
--- wasko/branches/2.0/waskaweb/config/middleware.py	2010-02-03 13:51:25 UTC (rev 1214)
+++ wasko/branches/2.0/waskaweb/config/middleware.py	2010-02-03 13:53:28 UTC (rev 1215)
@@ -1,16 +1,72 @@
 """Pylons middleware initialization"""
+import sys
+import logging
+
 from beaker.middleware import CacheMiddleware, SessionMiddleware
 from paste.cascade import Cascade
 from paste.registry import RegistryManager
 from paste.urlparser import StaticURLParser
 from paste.deploy.converters import asbool
 from pylons import config
+from pylons.util import  class_name_from_module_name
 from pylons.middleware import ErrorHandler, StatusCodeRedirect
 from pylons.wsgiapp import PylonsApp
 from routes.middleware import RoutesMiddleware
 
 from waskaweb.config.environment import load_environment
 
+log = logging.getLogger(__name__)
+
+class MyPylonsApp(PylonsApp):
+
+    def find_controller(self, controller):
+        """Locates a controller by attempting to import it then grab
+        the SomeController instance from the imported module.
+        
+        Override this to change how the controller object is found once
+        the URL has been resolved.
+        
+        """
+        try:
+            # Check to see if we've cached the class instance for this name
+            if controller in self.controller_classes:
+                return self.controller_classes[controller]
+            
+            # Pull the controllers class name, import controller
+            full_module_name = config['mpuls.app.instance'] + \
+                               '.controllers.' + controller.replace('/', '.')
+            
+            # Hide the traceback here if the import fails (bad syntax and such)
+            __traceback_hide__ = 'before_and_this'
+
+            # Try to import the controller. 
+            # First try to import it from the configured configuration instance (WASKA, WASKO, JMD...)
+            # If the controller can not be imported (e.g it was not defined
+            # here) try to load it from the base appication
+            try:
+                __import__(full_module_name)
+            except ImportError:
+                full_module_name = 'mpulsweb' + \
+                                   '.controllers.' + controller.replace('/', '.')
+                log.debug("Module not found! Trying to load from base location: %s" % full_module_name)
+                __import__(full_module_name)
+
+            if hasattr(sys.modules[full_module_name], '__controller__'):
+                mycontroller = getattr(sys.modules[full_module_name],
+                    sys.modules[full_module_name].__controller__)
+            else:
+                module_name = controller.split('/')[-1]
+                class_name = class_name_from_module_name(module_name) + 'Controller'
+                if self.log_debug:
+                    log.debug("Found controller, module: '%s', class: '%s'",
+                              full_module_name, class_name)
+                mycontroller = getattr(sys.modules[full_module_name], class_name)
+            self.controller_classes[controller] = mycontroller
+            return mycontroller
+        except Exception, e:
+            log.debug(e)
+
+
 def make_app(global_conf, full_stack=True, static_files=True, **app_conf):
     """Create a Pylons WSGI application and return it
 
@@ -38,7 +94,8 @@
     load_environment(global_conf, app_conf)
 
     # The Pylons WSGI app
-    app = PylonsApp()
+    app = MyPylonsApp()
+    #app = PylonsApp()
 
     # Routing/Session/Cache Middleware
     app = RoutesMiddleware(app, config['routes.map'])



More information about the Mpuls-commits mailing list