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

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


Author: torsten
Date: 2010-02-03 14:51:25 +0100 (Wed, 03 Feb 2010)
New Revision: 1214

Modified:
   wasko/branches/2.0/waskaweb/config/routing.py
Log:
* waskaweb/config/routing.py (controller_scan): New. Function to scan
for available controller in different locations. These locations can
be configured.


Modified: wasko/branches/2.0/waskaweb/config/routing.py
===================================================================
--- wasko/branches/2.0/waskaweb/config/routing.py	2010-02-03 13:46:30 UTC (rev 1213)
+++ wasko/branches/2.0/waskaweb/config/routing.py	2010-02-03 13:51:25 UTC (rev 1214)
@@ -27,12 +27,53 @@
 may take precedent over the more generic routes. For more information
 refer to the routes manual at http://routes.groovie.org/docs/
 """
+import os
+import re
+import logging
 from pylons import config
 from routes import Mapper
 
+log = logging.getLogger(__name__)
+
+def controller_scan(directory=None):
+    """Scan a directory for python files and use them as controllers""" 
+    log.debug('Scanning for controllers in %s' % directory)
+    controllers = []
+    if directory is None:
+        return []
+
+    def find_controllers(dirname, prefix=''):
+        """Locate controllers in a directory"""
+        controllers = []
+        for fname in os.listdir(dirname):
+            filename = os.path.join(dirname, fname)
+            if os.path.isfile(filename) and \
+                re.match('^[^_]{1,1}.*\.py$', fname):
+                controllers.append(prefix + fname[:-3])
+            elif os.path.isdir(filename):
+                controllers.extend(find_controllers(filename, prefix=prefix+fname+'/'))
+        return controllers
+    def longest_first(fst, lst):
+        """Compare the length of one string to another, shortest goes first""" 
+        return cmp(len(lst), len(fst))
+    for dirname in directory:
+        controllers.extend(find_controllers(dirname))
+    controllers.sort(longest_first)
+    log.debug('Found controllers: %s' % controllers)
+    return controllers
+
 def make_map():
     """Create, configure and return the routes Mapper"""
-    map = Mapper(directory=config['pylons.paths']['controllers'],
+    # First try to search to controllers in the instance (WASKO, WASKA, JMD ...)
+    controller_dirs = []
+    basepath = config['pylons.paths']['controllers']
+    controller_dirs.append(basepath) 
+    # Then append controllers in the Baseapplication
+    controller_dirs.append(os.path.join(config['mpuls.app.path'], 'controllers'))
+    # Remove duplicate in controller dirs
+    controller_dirs = list(set(controller_dirs))
+    map = Mapper(controller_scan=controller_scan,
+                 directory=controller_dirs,
                  always_scan=config['debug'])
 
     # The ErrorController route (handles 404/500 error pages); it should



More information about the Mpuls-commits mailing list