Google Groups Home
Help | Sign in
Mount apps in config
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all
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.
fumanchu  
View profile
 More options Jan 8 2007, 11:02 pm
From: "fumanchu" <fuman...@amor.org>
Date: Mon, 08 Jan 2007 20:02:18 -0800
Local: Mon, Jan 8 2007 11:02 pm
Subject: Re: Mount apps in config

I wrote:
> 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.

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"

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.
Jorge Godoy  
View profile
 More options Jan 9 2007, 5:39 am
From: Jorge Godoy <jgo...@gmail.com>
Date: Tue, 09 Jan 2007 08:39:12 -0200
Local: Tues, Jan 9 2007 5:39 am
Subject: Re: [cherrypy-users] Re: Mount apps in config

"fumanchu" <fuman...@amor.org> writes:
> 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>


    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.
End of messages
« Back to Discussions « Newer topic     Older topic »

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