<s> """<STR_LIT>""" <EOL> import re <EOL> import urlparse <EOL> import sys <EOL> try : <EOL> from string import Template <EOL> except ImportError : <EOL> from webob . util . stringtemplate import Template <EOL> import types <EOL> from webob import Response , Request , html_escape <EOL> tag_re = re . compile ( r'<STR_LIT>' , re . S ) <EOL> br_re = re . compile ( r'<STR_LIT>' , re . I | re . S ) <EOL> comment_re = re . compile ( r'<STR_LIT>' ) <EOL> def no_escape ( value ) : <EOL> if value is None : <EOL> return '<STR_LIT>' <EOL> if not isinstance ( value , basestring ) : <EOL> if hasattr ( value , '<STR_LIT>' ) : <EOL> value = unicode ( value ) <EOL> else : <EOL> value = str ( value ) <EOL> return value <EOL> def strip_tags ( value ) : <EOL> value = value . replace ( '<STR_LIT:\n>' , '<STR_LIT:U+0020>' ) <EOL> value = value . replace ( '<STR_LIT:\r>' , '<STR_LIT>' ) <EOL> value = br_re . sub ( '<STR_LIT:\n>' , value ) <EOL> value = comment_re . sub ( '<STR_LIT>' , value ) <EOL> value = tag_re . sub ( '<STR_LIT>' , value ) <EOL> return value <EOL> class HTTPException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message , wsgi_response ) : <EOL> Exception . __init__ ( self , message ) <EOL> self . __dict__ [ '<STR_LIT>' ] = wsgi_response <EOL> def __call__ ( self , environ , start_response ) : <EOL> return self . wsgi_response ( environ , start_response ) <EOL> def exception ( self ) : <EOL> return self <EOL> exception = property ( exception ) <EOL> if sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:5> ) : <EOL> def __getattr__ ( self , attr ) : <EOL> if not attr . startswith ( '<STR_LIT:_>' ) : <EOL> return getattr ( self . wsgi_response , attr ) <EOL> else : <EOL> raise AttributeError ( attr ) <EOL> def __setattr__ ( self , attr , value ) : <EOL> if attr . startswith ( '<STR_LIT:_>' ) or attr in ( '<STR_LIT:args>' , ) : <EOL> self . __dict__ [ attr ] = value <EOL> else : <EOL> setattr ( self . wsgi_response , attr , value ) <EOL> class WSGIHTTPException ( Response , HTTPException ) : <EOL> code = None <EOL> title = None <EOL> explanation = '<STR_LIT>' <EOL> body_template_obj = Template ( '''<STR_LIT>''' ) <EOL> plain_template_obj = Template ( '''<STR_LIT>''' ) <EOL> html_template_obj = Template ( '''<STR_LIT>''' ) <EOL> empty_body = False <EOL> def __init__ ( self , detail = None , headers = None , comment = None , <EOL> body_template = None ) : <EOL> Response . __init__ ( self , <EOL> status = '<STR_LIT>' % ( self . code , self . title ) , <EOL> content_type = '<STR_LIT>' ) <EOL> Exception . __init__ ( self , detail ) <EOL> if headers : <EOL> self . headers . update ( headers ) <EOL> self . detail = detail <EOL> self . comment = comment <EOL> if body_template is not None : <EOL> self . body_template = body_template <EOL> self . body_template_obj = Template ( body_template ) <EOL> if self . empty_body : <EOL> del self . content_type <EOL> del self . content_length <EOL> def _make_body ( self , environ , escape ) : <EOL> args = { <EOL> '<STR_LIT>' : escape ( self . explanation ) , <EOL> '<STR_LIT>' : escape ( self . detail or '<STR_LIT>' ) , <EOL> '<STR_LIT>' : escape ( self . comment or '<STR_LIT>' ) , <EOL> } <EOL> if self . comment : <EOL> args [ '<STR_LIT>' ] = '<STR_LIT>' % escape ( self . comment ) <EOL> else : <EOL> args [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> body_tmpl = self . body_template_obj <EOL> if WSGIHTTPException . body_template_obj is not self . body_template_obj : <EOL> for k , v in environ . items ( ) : <EOL> args [ k ] = escape ( v ) <EOL> for k , v in self . headers . items ( ) : <EOL> args [ k . lower ( ) ] = escape ( v ) <EOL> t_obj = self . body_template_obj <EOL> return t_obj . substitute ( args ) <EOL> def plain_body ( self , environ ) : <EOL> body = self . _make_body ( environ , no_escape ) <EOL> body = strip_tags ( body ) <EOL> return self . plain_template_obj . substitute ( status = self . status , <EOL> title = self . title , <EOL> body = body ) <EOL> def html_body ( self , environ ) : <EOL> body = self . _make_body ( environ , html_escape ) <EOL> return self . html_template_obj . substitute ( status = self . status , <EOL> body = body ) <EOL> def generate_response ( self , environ , start_response ) : <EOL> if self . content_length is not None : <EOL> del self . content_length <EOL> headerlist = list ( self . headerlist ) <EOL> accept = environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if accept and '<STR_LIT:html>' in accept or '<STR_LIT>' in accept : <EOL> body = self . html_body ( environ ) <EOL> if not self . content_type : <EOL> headerlist . append ( '<STR_LIT>' ) <EOL> else : <EOL> body = self . plain_body ( environ ) <EOL> if not self . content_type : <EOL> headerlist . append ( '<STR_LIT>' ) <EOL> headerlist . append ( ( '<STR_LIT>' , str ( len ( body ) ) ) ) <EOL> start_response ( self . status , headerlist ) <EOL> return [ body ] <EOL> def __call__ ( self , environ , start_response ) : <EOL> if environ [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> start_response ( self . status , self . headerlist ) <EOL> return [ ] <EOL> if not self . body and not self . empty_body : <EOL> return self . generate_response ( environ , start_response ) <EOL> return Response . __call__ ( self , environ , start_response ) <EOL> def wsgi_response ( self ) : <EOL> return self <EOL> wsgi_response = property ( wsgi_response ) <EOL> def exception ( self ) : <EOL> if sys . version_info >= ( <NUM_LIT:2> , <NUM_LIT:5> ) : <EOL> return self <EOL> else : <EOL> return HTTPException ( self . detail , self ) <EOL> exception = property ( exception ) <EOL> class HTTPError ( WSGIHTTPException ) : <EOL> """<STR_LIT>""" <EOL> class HTTPRedirection ( WSGIHTTPException ) : <EOL> """<STR_LIT>""" <EOL> class HTTPOk ( WSGIHTTPException ) : <EOL> """<STR_LIT>""" <EOL> code = <NUM_LIT:200> <EOL> title = '<STR_LIT:OK>' <EOL> class HTTPCreated ( HTTPOk ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> class HTTPAccepted ( HTTPOk ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = '<STR_LIT>' <EOL> class HTTPNonAuthoritativeInformation ( HTTPOk ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> class HTTPNoContent ( HTTPOk ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> empty_body = True <EOL> class HTTPResetContent ( HTTPOk ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> empty_body = True <EOL> class HTTPPartialContent ( HTTPOk ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> class _HTTPMove ( HTTPRedirection ) : <EOL> """<STR_LIT>""" <EOL> explanation = '<STR_LIT>' <EOL> body_template_obj = Template ( '''<STR_LIT>''' ) <EOL> def __init__ ( self , detail = None , headers = None , comment = None , <EOL> body_template = None , location = None , add_slash = False ) : <EOL> super ( _HTTPMove , self ) . __init__ ( <EOL> detail = detail , headers = headers , comment = comment , <EOL> body_template = body_template ) <EOL> if location is not None : <EOL> self . location = location <EOL> if add_slash : <EOL> raise TypeError ( <EOL> "<STR_LIT>" ) <EOL> self . add_slash = add_slash <EOL> def __call__ ( self , environ , start_response ) : <EOL> req = Request ( environ ) <EOL> if self . add_slash : <EOL> url = req . path_url <EOL> url += '<STR_LIT:/>' <EOL> if req . environ . get ( '<STR_LIT>' ) : <EOL> url += '<STR_LIT:?>' + req . environ [ '<STR_LIT>' ] <EOL> self . location = url <EOL> self . location = urlparse . urljoin ( req . path_url , self . location ) <EOL> return super ( _HTTPMove , self ) . __call__ ( <EOL> environ , start_response ) <EOL> class HTTPMultipleChoices ( _HTTPMove ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> class HTTPMovedPermanently ( _HTTPMove ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> class HTTPFound ( _HTTPMove ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = '<STR_LIT>' <EOL> class HTTPSeeOther ( _HTTPMove ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> class HTTPNotModified ( HTTPRedirection ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> empty_body = True <EOL> class HTTPUseProxy ( _HTTPMove ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( <EOL> '<STR_LIT>' ) <EOL> class HTTPTemporaryRedirect ( _HTTPMove ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> class HTTPClientError ( HTTPError ) : <EOL> """<STR_LIT>""" <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class HTTPBadRequest ( HTTPClientError ) : <EOL> pass <EOL> class HTTPUnauthorized ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class HTTPPaymentRequired ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' ) <EOL> class HTTPForbidden ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' ) <EOL> class HTTPNotFound ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' ) <EOL> class HTTPMethodNotAllowed ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> body_template_obj = Template ( '''<STR_LIT>''' ) <EOL> class HTTPNotAcceptable ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> template = Template ( '''<STR_LIT>''' ) <EOL> class HTTPProxyAuthenticationRequired ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' ) <EOL> class HTTPRequestTimeout ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class HTTPConflict ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class HTTPGone ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class HTTPLengthRequired ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' ) <EOL> class HTTPPreconditionFailed ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' ) <EOL> class HTTPRequestEntityTooLarge ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' ) <EOL> class HTTPRequestURITooLong ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' ) <EOL> class HTTPUnsupportedMediaType ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> template_obj = Template ( '''<STR_LIT>''' ) <EOL> class HTTPRequestRangeNotSatisfiable ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' ) <EOL> class HTTPExpectationFailed ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' ) <EOL> class HTTPUnprocessableEntity ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = '<STR_LIT>' <EOL> class HTTPLocked ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' ) <EOL> class HTTPFailedDependency ( HTTPClientError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class HTTPServerError ( HTTPError ) : <EOL> """<STR_LIT>""" <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class HTTPInternalServerError ( HTTPServerError ) : <EOL> pass <EOL> class HTTPNotImplemented ( HTTPServerError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> template = Template ( '''<STR_LIT>''' ) <EOL> class HTTPBadGateway ( HTTPServerError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' ) <EOL> class HTTPServiceUnavailable ( HTTPServerError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> class HTTPGatewayTimeout ( HTTPServerError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' ) <EOL> class HTTPVersionNotSupported ( HTTPServerError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' ) <EOL> class HTTPInsufficientStorage ( HTTPServerError ) : <EOL> code = <NUM_LIT> <EOL> title = '<STR_LIT>' <EOL> explanation = ( '<STR_LIT>' ) <EOL> class HTTPExceptionMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , application ) : <EOL> self . application = application <EOL> def __call__ ( self , environ , start_response ) : <EOL> try : <EOL> return self . application ( environ , start_response ) <EOL> except HTTPException , exc : <EOL> parent_exc_info = sys . exc_info ( ) <EOL> def repl_start_response ( status , headers , exc_info = None ) : <EOL> if exc_info is None : <EOL> exc_info = parent_exc_info <EOL> return start_response ( status , headers , exc_info ) <EOL> return exc ( environ , repl_start_response ) <EOL> try : <EOL> from paste import httpexceptions <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> for name in dir ( httpexceptions ) : <EOL> obj = globals ( ) . get ( name ) <EOL> if ( obj and isinstance ( obj , type ) and issubclass ( obj , HTTPException ) <EOL> and obj is not HTTPException <EOL> and obj is not WSGIHTTPException ) : <EOL> obj . __bases__ = obj . __bases__ + ( getattr ( httpexceptions , name ) , ) <EOL> del name , obj , httpexceptions <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> status_map = { } <EOL> for name , value in globals ( ) . items ( ) : <EOL> if ( isinstance ( value , ( type , types . ClassType ) ) and issubclass ( value , HTTPException ) <EOL> and not name . startswith ( '<STR_LIT:_>' ) ) : <EOL> __all__ . append ( name ) <EOL> if getattr ( value , '<STR_LIT:code>' , None ) : <EOL> status_map [ value . code ] = value <EOL> del name , value </s>
