class WSGIApplication(object): """Wraps a set of webapp RequestHandlers in a WSGI-compatible application. To use this class, pass a list of (URI regular expression, RequestHandler) pairs to the constructor, and pass the class instance to a WSGI handler. See the example in the module comments for details. The URL mapping is first-match based on the list ordering. """ REQUEST_CLASS = Request RESPONSE_CLASS = Response def __init__(self, handlers,include_module_in_url=True, include_package_in_url=True, debug=False): self.handlers = handlers self.handlerMap = get_controllers(CONTROLLERS_DIR, include_module=include_module_in_url, include_package=include_package_in_url) self.mapper = Mapper(self.handlerMap.keys()) self.mapper.extend(handlers) """Initializes this application with the given URL mapping. Args: url_mapping: list of (URI regular expression, RequestHandler) pairs (e.g., [('/', ReqHan)]) debug: if true, we send Python stack traces to the browser on errors """ self.__debug = debug WSGIApplication.app = self self.current_request_args = () def __call__(self, environ, start_response): """Called by WSGI when a request comes in.""" request = self.REQUEST_CLASS(environ) response = self.RESPONSE_CLASS() self.url = URLGenerator(self.mapper, environ) result = self.mapper.routematch(environ = environ) if result: match, route = result handler = None groups = dict([(x,match[x]) for x in match if x not in ['controller', 'action']]) handler_cls = ClassImport(self.handlerMap[match['controller']])\ .new_factory(op=match['action']) handler = handler_cls() handler.initialize(request, response) try: handler.route(**groups) except Exception, e: handler.handle_exception(e, self.__debug) else:
def __init__(self, handlers,include_module_in_url=True, include_package_in_url=True, debug=False): self.handlers = handlers self.handlerMap = get_controllers(CONTROLLERS_DIR, include_module=include_module_in_url, include_package=include_package_in_url) self.mapper = Mapper(self.handlerMap.keys()) self.mapper.extend(handlers) """Initializes this application with the given URL mapping. Args: url_mapping: list of (URI regular expression, RequestHandler) pairs (e.g., [('/', ReqHan)]) debug: if true, we send Python stack traces to the browser on errors """ self.__debug = debug WSGIApplication.app = self self.current_request_args = ()