Mercurial > hg > wsgintegrate
annotate wsgintegrate/dispatcher.py @ 0:ec815b7cb142
initial commit of wsgintegrate
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 07 Jun 2011 08:03:09 -0700 |
parents | |
children | 88b611ed2c5e |
rev | line source |
---|---|
0
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 """ |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 multi-application dispatcher for WSGI apps |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 """ |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 from webob import Request |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 from webob import exc |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 class Dispatcher(object): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 def __init__(self, *apps): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 self.apps = apps |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 self.codes = set([404]) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 def __call__(self, environ, start_response): |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 request = Request(environ) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 for app in self.apps: |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 try: |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 response = request.get_response(app) |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 if response.status_int in self.codes: |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 continue |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 break |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 except exc.HTTPNotFound: |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 continue |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 else: |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 response = exc.HTTPNotFound() |
ec815b7cb142
initial commit of wsgintegrate
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
26 return response(environ, start_response) |