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
fuma...@amor.org
And here are those two lines:
def mount_all(self):
if not self.mounted:
for appname, kwargs in self.kwargsets.iteritems():
# The two new lines:
if 'wsgi_callable' in kwargs:
cherrypy.tree.graft(**kwargs)
else:
cherrypy.tree.mount(**kwargs)
self.mounted = True
This allows you to graft any WSGI apps into the WSGI stack via config:
----
[global]
environment: 'production'
tree.core_app.root = mycpapp.root
tree.core_app.script_name = "/"
tree.core_app.config =
r"C:\Python24\Lib\site-packages\mycpapp\mycpapp.conf"
tree.other.wsgi_callable = PylonsBaseWSGIApp("mypackage",
mypackage.globals)
tree.other.script_name = "/hello"
> And here are those two lines:
(...)
Are those being added to code / docs or just being published here? It would
be nice having these on a more permanent place... There have been some nice
improvements here (and on tg-trunk while you were talking to Ian).
Thanks!
--
Jorge Godoy <jgo...@gmail.com>