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:Python24Libsite-packagesmyappmyapp.conf"
Then, from the command-line, run the following:
C:Python24Libsite-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
fumanchu amor.org
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-users googlegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribe googlegroups.com
For more options, visit this group at http://groups-beta.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|