I've written a small patch that allows multi-word controller names, e.g. front_page / FrontPageController, to be used. Without this patch Pylons assumes that the class in 'front_page.py' is called 'Front_pageController'.

I also renamed the locals used in PylonsBaseWSGIApp#resolve() since they were confusingly too similar to each other (2x 'controller', 'controller_name', 'controller_class' and 'classname'). I hope the new names ('full_module_name', 'module_name', 'class_name') communicate the meaning of the variables better.

--
noid
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/pylons-discuss
-~----------~----~----~----~------~----~------~--~---

Index: pylons/commands.py
===================================================================
--- pylons/commands.py	(revision 1062)
+++ pylons/commands.py	(working copy)
@@ -19,6 +19,8 @@
 import paste.deploy.config
 import paste.fixture
 
+from pylons.util import class_name_from_module_name
+
 class ControllerCommand(Command):
     """Create a Controller and functional test for it
     
@@ -64,7 +66,7 @@
             fullname = os.path.join(dir, name)
             if not fullname.startswith(os.sep): fullname = os.sep + fullname
             testname = fullname.replace(os.sep, '_')[1:]
-            fo.template_vars.update({'name': name.title().replace('-', '_'),
+            fo.template_vars.update({'name': class_name_from_module_name(name),
                                   'fullname': fullname,
                                   'fname': os.path.join(dir, name),
                                   'lname': name})
Index: pylons/wsgiapp.py
===================================================================
--- pylons/wsgiapp.py	(revision 1062)
+++ pylons/wsgiapp.py	(working copy)
@@ -22,6 +22,7 @@
 from pylons.util import RequestLocal
 from pylons.legacy import Myghty_Compat
 from pylons.controllers import Controller, WSGIController
+from pylons.util import class_name_from_module_name
 
 class PylonsBaseWSGIApp(object):
     """Basic Pylons WSGI Application
@@ -144,13 +145,12 @@
         # Pull the controllers class name, import controller
         # @@ TODO: Encapsulate in try/except to raise error if controller
         #          doesn't exist, or class in controller file doesn't exist.
-        controller_name = self.package_name + '.controllers.' \
+        full_module_name = self.package_name + '.controllers.' \
             + controller.lower().replace('/', '.')
-        __import__(controller_name)
-        controller_class = controller.split('/')[-1].title().replace('-', '_')
-        classname = controller_class + 'Controller'
-        controller = getattr(sys.modules[controller_name], classname)
-        return controller
+        __import__(full_module_name)
+        module_name = full_module_name.split('.')[-1]
+        class_name = class_name_from_module_name(module_name) + 'Controller'
+        return getattr(sys.modules[full_module_name], class_name)
         
     def dispatch(self, controller, environ, start_response):
         """Dispatches to a controller, will instantiate the controller if
Index: pylons/util.py
===================================================================
--- pylons/util.py	(revision 1062)
+++ pylons/util.py	(working copy)
@@ -27,6 +27,21 @@
         prefix = ''
     return prefix
 
+def class_name_from_module_name(module_name):
+    """Takes a module name and returns the name of the class it defines.
+
+    If the module name contains dashes, they are replaced with underscores.
+
+    >>> class_name_from_module_name('with-dashes')
+    'WithDashes'
+    >>> class_name_from_module_name('with_underscores')
+    'WithUnderscores'
+    >>> class_name_from_module_name('oneword')
+    'Oneword'
+    """
+    words = module_name.replace('-', '_').split('_')
+    return ''.join([w.title() for w in words])
+
 class RequestLocal(object):
     """This object emulates a dict and supports the full set of dict functions
     and operations.

Reply via email to