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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Feb 16 16:20:32 CET 2010


Author: bh
Date: 2010-02-16 16:20:30 +0100 (Tue, 16 Feb 2010)
New Revision: 1629

Added:
   wasko/branches/2.0/mpulsweb/config/importer.py
Modified:
   wasko/branches/2.0/ChangeLog
   wasko/branches/2.0/mpulsweb/config/middleware.py
Log:
* mpulsweb/config/importer.py: New module with one function:
(import_overridable_module): Load a module either from the
specialization or mpulsweb.

* mpulsweb/config/middleware.py (MyPylonsApp.find_controller): Use
import_overridable_module to import the controller module and use
the module returned by that function directly, instead of always
going through sys.modules.
(controller_module): Removed. Not used anymore.


Modified: wasko/branches/2.0/ChangeLog
===================================================================
--- wasko/branches/2.0/ChangeLog	2010-02-16 14:58:02 UTC (rev 1628)
+++ wasko/branches/2.0/ChangeLog	2010-02-16 15:20:30 UTC (rev 1629)
@@ -1,5 +1,17 @@
 2010-02-16  Bernhard Herzog  <bh at intevation.de>
 
+	* mpulsweb/config/importer.py: New module with one function:
+	(import_overridable_module): Load a module either from the
+	specialization or mpulsweb.
+
+	* mpulsweb/config/middleware.py (MyPylonsApp.find_controller): Use
+	import_overridable_module to import the controller module and use
+	the module returned by that function directly, instead of always
+	going through sys.modules.
+	(controller_module): Removed. Not used anymore.
+
+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

Added: wasko/branches/2.0/mpulsweb/config/importer.py
===================================================================
--- wasko/branches/2.0/mpulsweb/config/importer.py	2010-02-16 14:58:02 UTC (rev 1628)
+++ wasko/branches/2.0/mpulsweb/config/importer.py	2010-02-16 15:20:30 UTC (rev 1629)
@@ -0,0 +1,35 @@
+"""Functions to import modules from a specialization or the base"""
+
+import logging
+
+from paste.util.import_string import try_import_module, import_module
+
+from pylons import config
+
+
+log = logging.getLogger(__name__)
+
+
+def import_overridable_module(modulename):
+    """Import a module that an MPuls specialization can override.
+
+    The modulename should be a name relative to the mpulsweb package,
+    e.g. 'controllers.foo' for the foo controller.  This function first
+    tries to import the module from the specialization's main package,
+    as configured with the mpuls.app.instance setting.  If that fails
+    the module will be imported from the mpulsweb package.
+
+    If the module can be imported successfully, it will be returned.  If
+    it cannot be imported from either location, an exception will be
+    raised (most likely an ImportError).
+    """
+    instance_module = config.get('mpuls.app.instance') + "." + modulename
+    module = try_import_module(instance_module)
+    if module is not None:
+        return module
+
+    base_module = "mpulsweb." + modulename
+    log.debug("Module %r doesn't seem to exist, importing %r instead",
+              instance_module, base_module)
+    return import_module(base_module)
+


Property changes on: wasko/branches/2.0/mpulsweb/config/importer.py
___________________________________________________________________
Name: svn:keywords
   + Id Revision
Name: svn:eol-style
   + native

Modified: wasko/branches/2.0/mpulsweb/config/middleware.py
===================================================================
--- wasko/branches/2.0/mpulsweb/config/middleware.py	2010-02-16 14:58:02 UTC (rev 1628)
+++ wasko/branches/2.0/mpulsweb/config/middleware.py	2010-02-16 15:20:30 UTC (rev 1629)
@@ -8,7 +8,6 @@
 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
@@ -16,14 +15,12 @@
 from routes.middleware import RoutesMiddleware
 
 from mpulsweb.config.environment import load_environment
+from mpulsweb.config.importer import import_overridable_module
 
+
 log = logging.getLogger(__name__)
 
 
-def controller_module(appname, controller):
-    return appname + ".controllers." + controller.replace('/', '.')
-
-
 class MyPylonsApp(PylonsApp):
 
     def find_controller(self, controller):
@@ -40,28 +37,18 @@
         # Hide the traceback here if the import fails (bad syntax and such)
         __traceback_hide__ = 'before_and_this'
 
-        # 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)
+        module = import_overridable_module("controllers."
+                                           + controller.replace('/', '.'))
 
-        if hasattr(sys.modules[full_module_name], '__controller__'):
-            mycontroller = getattr(sys.modules[full_module_name],
-                                   sys.modules[full_module_name].__controller__)
+        if hasattr(module, '__controller__'):
+            mycontroller = getattr(module, module.__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)
+                log.debug("Found controller, module: %r, class: %r",
+                          module, class_name)
+            mycontroller = getattr(module, class_name)
         self.controller_classes[controller] = mycontroller
         return mycontroller
 



More information about the Mpuls-commits mailing list