Google Groups Home Help | Sign in
Message from discussion Mount apps in config
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
fumanchu  
View profile
 More options Jan 3 2007, 6:02 pm
From: "fumanchu" <fuman...@amor.org>
Date: Wed, 03 Jan 2007 15:02:17 -0800
Local: Wed, Jan 3 2007 6:02 pm
Subject: Mount apps in config
I was letting off steam on IRC today (after Alberto announced he wanted
TG to move away from CherryPy) and boasted "there's nothing in
paste.deploy that CP 3 doesn't do more beautifully". Elvelind kindly
called me on it, mentioning Paste's ability to mount multiple apps from
config. I whipped up a module to provide that feature, and thought some
of you might benefit from using it, and some others might benefit from
seeing how AMAZINGLY SIMPLE it can be for CP, you, or a larger
framework like TG to provide similar features. So here's deploy.py:

import sys
import cherrypy

class Mounter(object):

    def __init__(self, namespace='tree'):
        self.mounted = False
        self.namespace = namespace
        self.config = {}
        cherrypy.config.namespaces[namespace] = self.namespace_handler

    def namespace_handler(self, k, v):
        """Config handler for our namespace."""
        appname, arg = k.split(".", 1)
        bucket = self.config.setdefault(appname, {})
        bucket[arg] = v

    def mount_all(self):
        if not self.mounted:
            for appname, conf in self.config.iteritems():
                cherrypy.tree.mount(**conf)
            self.mounted = True

def mount(path):
    m = Mounter()
    cherrypy.config.update(path)
    m.mount_all()

if __name__ == '__main__':
    mount(sys.argv[1])
    cherrypy.server.quickstart()
    cherrypy.engine.start()

Add the following lines to your global config file:

[global]
environment: 'production'
tree.core_app.root = myapp.root
tree.core_app.script_name = "/"
tree.core_app.config =
r"C:\Python24\Lib\site-packages\myapp\myapp.conf"

Then, from the command-line, run the following:

C:\Python24\Lib\site-packages>python deploy.py myglobal.conf

How it works: this adds a handler for the "tree" namespace to the
global CherryPy config. This means any global config entry of the form
"tree.<appname>.<key> = <value>" will get handled by our Mounter. The
namespace_handler method simply stores these as they are read from the
config file. Then a call to Mounter.mount_all calls tree.mount for each
<appname>, passing all the key/value pairs to the mount call.

This example only mounts CherryPy WSGI Applications, but it would only
take a couple of lines to make it mount other WSGI applications via
cherrypy.tree.graft instead of cherrypy.tree.mount.

Robert Brewer
System Architect
Amor Ministries
fuman...@amor.org


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google