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