> It depends on what you me by "linked into TG". For most people,
> there's no need to hook into startup and shutdown.

My content management system needs to take the database tree of objects and 
instantiate classes and attach them to the project root.  E.g. if you have 
the following structure in your CMS...

        Folder(/)
        Page(/welcome)
        Page(/contact)
        Folder(/company)
        Page(/company/about)
        Page(/company/positions)

Startup will attach the representitive classes to your root node, to emulate 
the following structure:

        class Root(...): (Folder /)
                welcome = Page()
                contact = Page()
                company = Folder()
                company.about = Page()
                company.positions = Page()

My content.controllers module defines the following, allowing CMS projects to 
use content.controllers.RootController in place of 
turbogears.controllers.RootController.  This is nessicary to get the root to 
have the properties of a Folder CMS object, and to create the root tree.

def __descend(self, base, root, path = '/'):
  for atom in root.Children:
    # Step 1: Determine if the object already exists in our tree.
    if hasattr(base, atom.Name):
      raise Exception, "Object by the name %s already exists!" % atom.Name
      continue
                
    # Step 2: Load the correct controller module.
    className = atom.__class__.__name__
    moduleClass = getattr(content.controllers, className)
                
    # Step 3: Initialize controller and place in our document tree.
    classInstance = moduleClass(atom.id, "%s%s/" % (path, atom.Name))
    setattr(base, atom.Name.replace('-', '_'), classInstance)
                
    # Step 4: Descend into children nodes.
    if len(atom.Children) > 0:
      self._descend(classInstance, atom)

class RootController(Folder, controllers.RootController):
  def __init__(self):
    Folder.__init__(base=self, root=1, path="/")
    root = content.model.Atom.get(1)
    __descend(self, root)

I was wary to override __init__ on the root controller, but it seems to have 
had no negative impact.

  - Matthew

Attachment: pgpXCOS06clrh.pgp
Description: PGP signature

Reply via email to