annotate taginthemiddle/example.py @ 2:1182315b18ac

add rudimentary code for handlers
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 04 May 2010 19:15:21 -0700
parents 1c5cbbde4299
children c2bb8f873aee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 import os
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 from dispatcher import Dispatcher
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 from paste.httpexceptions import HTTPExceptionHandler
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 from paste.urlparser import StaticURLParser
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 from pkg_resources import resource_filename
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 class PassthroughFileserver(object):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 """serve files if they exist"""
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 def __init__(self, app, directory):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 self.app = app
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 self.directory = directory
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 self.fileserver = StaticURLParser(self.directory)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 def __call__(self, environ, start_response):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 path = environ['PATH_INFO'].strip('/')
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 if path and os.path.exists(os.path.join(self.directory, path)):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 return self.fileserver(environ, start_response)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 return self.app(environ, start_response)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 def factory(global_conf, **app_conf):
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 """create a webob view and wrap it in middleware"""
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 keystr = 'TagInTheMiddle.'
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 args = dict([(key.split(keystr, 1)[-1], value)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 for key, value in app_conf.items()
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 if key.startswith(keystr) ])
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 app = Dispatcher(**args)
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 return HTTPExceptionHandler(PassthroughFileserver(app, resource_filename(__name__, 'static')))
1c5cbbde4299 initial commit of middleware tagging; doesnt yet work
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31