Hide
Free your code from a slow death on your hard drive Join Siafoo Now or Learn More

Filter a Pylons App with Another, Based On URL Atom Feed 0

In Brief This is a piece of middleware you can use to filter one app with another. Call it in your middleware.py with:... more
# 's
 1class FilterWith(object):
2 def __init__(self, app, filter, path):
3 self.app = app
4 self.filter = filter
5 self.path = path
6
7 def __call__(self, environ, start_response):
8 if self.path and environ['PATH_INFO'].startswith(self.path):
9 environ['PATH_INFO'] = environ['PATH_INFO'][len(self.path):] or '/'
10 environ['SCRIPT_NAME'] += self.path
11 return self.filter(environ, start_response)
12 else:
13 return self.app(environ, start_response)

This is a piece of middleware you can use to filter one app with another. Call it in your middleware.py with:

# 's
1app = FilterWith(app, some_other_filter_app, '/path/to/filter/app')

The first app passed will receive the request unless the URL starts with path (here, '/path/to/filter/app'). In that case the second app will receive the request.

This middleware chops off the base path so the filtering application treats the base path as its root, '/'. This middleware also tells your filtering app's Pylons and Routes its base path using SCRIPT_NAME; this path is prepended to any generated URLs. (You'll still have to prepend it manually if you write out any URLs yourself.)