import os from paste.request import path_info_pop from paste.deploy import config from wareweb import wsgiapp from commentary import filter def make_app_filter( application, global_conf, # Required configuration parameters (if any): storage, **kw): """ Filters a WSGI application, adding commenting abilities. """ kw['storage'] = storage comment_app = wsgiapp.make_wareweb_app( global_conf, package_name='commentary', root_path=os.path.dirname(__file__), **kw) filtered_app = filter.filter_app(application) full_app = AppDispatch(filtered_app, comment_app) conf = global_conf.copy() conf.update(kw) full_app = config.ConfigMiddleware(full_app, conf) return full_app def make_app( global_conf, href, **kw): """ Creates a commentary instance that proxies (with link rewriting) another site. Can be used for local demos. """ from commentary.lxmlproxy import LXMLProxy app = LXMLProxy(href) app = make_app_filter(global_conf, app, **kw) return app class AppDispatch(object): def __init__(self, app, comment): self.app = app self.comment = comment def __call__(self, environ, start_response): environ['commentary.comment_root'] = environ['SCRIPT_NAME'] environ['commentary.base_href'] = environ['SCRIPT_NAME'] + '/_comment' if environ.get('PATH_INFO', '').startswith('/_comment/'): path_info_pop(environ) return self.comment(environ, start_response) return self.app(environ, start_response)