[Mpuls-commits] r1610 - in wasko/branches/2.0: . mpulsweb/config

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Feb 16 12:00:22 CET 2010


Author: bh
Date: 2010-02-16 12:00:21 +0100 (Tue, 16 Feb 2010)
New Revision: 1610

Modified:
   wasko/branches/2.0/ChangeLog
   wasko/branches/2.0/mpulsweb/config/middleware.py
Log:
* mpulsweb/config/middleware.py (controller_module): New.  helper
function to determine the module name for a controller
(MyPylonsApp.find_controller): When trying to import the
specialization's controller, use try_import_module from
paste.util.import_string, which will suppress the ImportError only
when it's raised because the module itself cannot be found, but
not if it's raised indirectly by some other module that cannot be
imported.  Also, use controller_module and improve comments,
logging and the doc-string.


Modified: wasko/branches/2.0/ChangeLog
===================================================================
--- wasko/branches/2.0/ChangeLog	2010-02-16 10:49:22 UTC (rev 1609)
+++ wasko/branches/2.0/ChangeLog	2010-02-16 11:00:21 UTC (rev 1610)
@@ -1,5 +1,17 @@
 2010-02-16  Bernhard Herzog  <bh at intevation.de>
 
+	* mpulsweb/config/middleware.py (controller_module): New.  helper
+	function to determine the module name for a controller
+	(MyPylonsApp.find_controller): When trying to import the
+	specialization's controller, use try_import_module from
+	paste.util.import_string, which will suppress the ImportError only
+	when it's raised because the module itself cannot be found, but
+	not if it's raised indirectly by some other module that cannot be
+	imported.  Also, use controller_module and improve comments,
+	logging and the doc-string.
+
+2010-02-16  Bernhard Herzog  <bh at intevation.de>
+
 	* mpulsweb/config/middleware.py (MyPylonsApp.find_controller): Do
 	not catch Exceptions, especially ones raised by __import__, unless
 	necessary to implement the desired functionality, and even then,

Modified: wasko/branches/2.0/mpulsweb/config/middleware.py
===================================================================
--- wasko/branches/2.0/mpulsweb/config/middleware.py	2010-02-16 10:49:22 UTC (rev 1609)
+++ wasko/branches/2.0/mpulsweb/config/middleware.py	2010-02-16 11:00:21 UTC (rev 1610)
@@ -8,6 +8,7 @@
 from paste.registry import RegistryManager
 from paste.urlparser import StaticURLParser
 from paste.deploy.converters import asbool
+from paste.util.import_string import try_import_module
 from pylons import config
 from pylons.util import  class_name_from_module_name
 from pylons.middleware import ErrorHandler, StatusCodeRedirect
@@ -18,43 +19,42 @@
 
 log = logging.getLogger(__name__)
 
+
+def controller_module(appname, controller):
+    return appname + ".controllers." + controller.replace('/', '.')
+
+
 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.
-        
+        """Override this method to look for the controller in two places.
+
+        First, the controller is searched in the MPuls specialization's
+        controllers package, and if it cannot be found there, it's
+        imported from the mpulsweb.
         """
         # 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:
+        # Try to import the controller. Look in the specialization's
+        # controllers package first.  If cannot be found there, look in
+        # the base application's controllers package
+        full_module_name = controller_module(config['mpuls.app.instance'],
+                                             controller)
+        if try_import_module(full_module_name) is None:
+            log.debug("Module %r doesn't seem to exist", full_module_name)
+            full_module_name = controller_module('mpulsweb', controller)
+            log.debug("Trying to load from base location instead: %s",
+                      full_module_name)
             __import__(full_module_name)
-        except ImportError:
-            log.exception("Exception while loading module %r", full_module_name)
-            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__)
+                                   sys.modules[full_module_name].__controller__)
         else:
             module_name = controller.split('/')[-1]
             class_name = class_name_from_module_name(module_name) + 'Controller'



More information about the Mpuls-commits mailing list