#813: Alternative Controller Tree
-----------------------------------------------------------+----------------
 Reporter:  Claudio Martínez <[EMAIL PROTECTED]>  |       Owner:  anonymous
     Type:  enhancement                                    |      Status:  new  
    
 Priority:  lowest                                         |   Milestone:       
    
Component:  TurboGears                                     |     Version:       
    
 Severity:  trivial                                        |    Keywords:       
    
-----------------------------------------------------------+----------------
 If you quickstart a project with:
 tg-admin quickstart -t tgbig

 You can replace your {{{controllers/__init__.py}}} with:
 {{{
 import os
 import stat
 import turbogears
 # replace yourproject
 from yourproject.controllers.root import Root

 def import_(module):
     return __import__(module.replace('/', '.'), globals(), locals(), '1')

 def load_controllers(path='', root=True):
     if not root:
         controller = getattr(import_(path), 'Controller', None)
         if controller:
             base_controller = controller()
         else:
             base_controller = turbogears.controllers.Controller()
     else:
         base_controller = Root

     for module in os.listdir(path):
         try:
             st = os.lstat(os.path.join(path, module))
         except os.error:
             continue

         if module == '__init__.py': continue
         if root and module == 'root.py': continue

         if stat.S_ISDIR(st.st_mode):
             controller = load_controllers(path+'/'+module, False)
         else:
             if not module.endswith('.py'): continue
             else: module = module[:-3]
             controller = import_(path+'/'+module).Controller()

         setattr(base_controller, getattr(controller, '__name__', module),
                 controller)

     return base_controller
 # replace yourproject
 load_controllers('yourproject/controllers')
 }}}

 This makes the controller tree work in a different way:

  * If you drop a new file inside the controller tree it will be added to
 the controller tree without needing to modify anything. You still need to
 restart the server of course.
  * The controller class in the files have to be named Controller
  * Translates the directory structure to the url structure automatically
  * The url portion will be given by the filename, so:
 controllers/test/foo.py  will be http://localhost:8080/test/foo . (setting
 {{{__name__}}} on the controller class overrides it)

 A controller directory example:
 {{{
 controllers/__init__.py (here's were we add this stuff)
 controllers/root.py (the default one)
 controllers/mycontroller.py (extend the root controller)
 controllers/test1/__init__.py (index, default, foo, bar)
 controllers/test1/other_controller.py (extend just by adding files)
 controllers/test2/one.py (you don't even need __init__.py)
 controllers/test3/__init__.py (only index)
 }}}

 The contents of every file, except for {{{controllers/__init__.py}}} and
 {{{controllers/root.py}}}, can be:
 {{{
 import cherrypy
 import turbogears
 from turbogears import controllers, expose, redirect

 class Controller(controllers.Controller):
     @expose()
     def index(self):
         return 'Hello from ' + __name__
 }}}

 If it's not possible already in TG/CP, we could add some path information
 so every module can know their position in the URL (shouldn't be
 difficult). Modules could export their controllers urls associated to an
 immutable name. I'm pretty sure it's possible to eliminate every last bit
 of url hardcoding adapting this.

 Just posting this before doing any hard work, I want to know if I'm not
 reinventing the wheel. I did need a way to add/remove controllers without
 modifying other files, works for that.

-- 
Ticket URL: <http://trac.turbogears.org/turbogears/ticket/813>
TurboGears <http://www.turbogears.org/>
TurboGears front-to-back web development
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears Tickets" 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/turbogears-tickets
-~----------~----~----~----~------~----~------~--~---

Reply via email to