<s> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> import cgi <EOL> import sys <EOL> import json <EOL> import datetime <EOL> import exceptions <EOL> from werkzeug . utils import get_content_type <EOL> from werkzeug . debug import DebuggedApplication <EOL> from werkzeug . wrappers import BaseResponse <EOL> import sinter <EOL> from . tbutils import ExceptionInfo , ContextualExceptionInfo <EOL> from . render . simple import ClasticJSONEncoder <EOL> from . _contextual_errors import CONTEXTUAL_ENV <EOL> ERROR_CODE_MAP = None <EOL> STDLIB_EXC_URL = '<STR_LIT>' <EOL> def _module_init ( ) : <EOL> global ERROR_CODE_MAP <EOL> ERROR_CODE_MAP = { } <EOL> for k , v in globals ( ) . items ( ) : <EOL> try : <EOL> if issubclass ( v , HTTPException ) : <EOL> ERROR_CODE_MAP [ v . code ] = v <EOL> except ( TypeError , AttributeError ) : <EOL> pass <EOL> MIME_SUPPORT_MAP = { '<STR_LIT>' : '<STR_LIT:html>' , <EOL> '<STR_LIT:application/json>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:text>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> DEFAULT_MIME = '<STR_LIT>' <EOL> class HTTPException ( BaseResponse , Exception ) : <EOL> code = None <EOL> message = '<STR_LIT>' <EOL> detail = '<STR_LIT>' <EOL> def __init__ ( self , detail = None , ** kwargs ) : <EOL> self . detail = detail or self . detail <EOL> self . message = kwargs . pop ( '<STR_LIT:message>' , self . message ) <EOL> self . code = kwargs . pop ( '<STR_LIT:code>' , self . code ) <EOL> self . error_type = kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . is_breaking = kwargs . pop ( '<STR_LIT>' , True ) <EOL> self . source_route = kwargs . pop ( '<STR_LIT>' , None ) <EOL> headers = kwargs . pop ( '<STR_LIT>' , None ) <EOL> mimetype = kwargs . pop ( '<STR_LIT>' , DEFAULT_MIME ) <EOL> content_type = kwargs . pop ( '<STR_LIT>' , None ) <EOL> super ( HTTPException , self ) . __init__ ( response = self . to_text ( ) , <EOL> status = self . code , <EOL> headers = headers , <EOL> mimetype = mimetype , <EOL> content_type = content_type ) <EOL> def adapt ( self , mimetype = None ) : <EOL> try : <EOL> fmt_name = MIME_SUPPORT_MAP [ mimetype ] <EOL> except KeyError : <EOL> fmt_name , mimetype = '<STR_LIT:text>' , '<STR_LIT>' <EOL> _method = getattr ( self , '<STR_LIT>' + fmt_name ) <EOL> self . data = _method ( ) <EOL> self . headers [ '<STR_LIT:Content-Type>' ] = get_content_type ( mimetype , self . charset ) <EOL> def transcribe ( self , request ) : <EOL> pass <EOL> def to_dict ( self ) : <EOL> ret = { '<STR_LIT>' : self . detail , <EOL> '<STR_LIT:message>' : self . message , <EOL> '<STR_LIT:code>' : self . code , <EOL> '<STR_LIT>' : self . error_type } <EOL> return ret <EOL> def to_escaped_dict ( self ) : <EOL> ret = { } <EOL> for k , v in self . to_dict ( ) . items ( ) : <EOL> if v is None : <EOL> ret [ k ] = '<STR_LIT>' <EOL> continue <EOL> try : <EOL> ret [ k ] = cgi . escape ( v , True ) <EOL> except Exception as e : <EOL> ret [ k ] = cgi . escape ( repr ( v ) , True ) <EOL> return ret <EOL> def to_json ( self , indent = <NUM_LIT:2> , sort_keys = True , skipkeys = True ) : <EOL> encoder = ClasticJSONEncoder ( dev_mode = True , indent = indent , <EOL> sort_keys = sort_keys , ensure_ascii = False , <EOL> skipkeys = skipkeys ) <EOL> return encoder . encode ( self . to_dict ( ) ) <EOL> def to_text ( self ) : <EOL> lines = [ '<STR_LIT>' % ( self . code , self . message ) ] <EOL> if self . detail : <EOL> lines . extend ( [ '<STR_LIT>' , self . detail ] ) <EOL> if self . error_type : <EOL> lines . extend ( [ '<STR_LIT>' , '<STR_LIT>' % self . error_type ] ) <EOL> return '<STR_LIT:\n>' . join ( lines ) <EOL> def to_html ( self ) : <EOL> params = self . to_escaped_dict ( ) <EOL> lines = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> if params [ '<STR_LIT>' ] : <EOL> lines . append ( '<STR_LIT>' ) <EOL> if params [ '<STR_LIT>' ] : <EOL> if params [ '<STR_LIT>' ] . startswith ( '<STR_LIT:http>' ) : <EOL> lines . append ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> lines . append ( '<STR_LIT>' ) <EOL> lines . append ( '<STR_LIT>' ) <EOL> return '<STR_LIT:\n>' . join ( lines ) . format ( ** params ) <EOL> def to_xml ( self ) : <EOL> params = self . to_escaped_dict ( ) <EOL> ret = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( ** params ) <EOL> return ret <EOL> def __repr__ ( self ) : <EOL> cn = self . __class__ . __name__ <EOL> return '<STR_LIT>' % ( cn , getattr ( self , '<STR_LIT:message>' , '<STR_LIT>' ) ) <EOL> class BadRequest ( HTTPException ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class Unauthorized ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class PaymentRequired ( BadRequest ) : <EOL> "<STR_LIT>" <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class Forbidden ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class NotFound ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = "<STR_LIT>" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . dispatch_state = kwargs . get ( '<STR_LIT>' , None ) <EOL> super ( NotFound , self ) . __init__ ( * args , ** kwargs ) <EOL> class MethodNotAllowed ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = "<STR_LIT>" <EOL> def __init__ ( self , allowed_methods = None , * args , ** kwargs ) : <EOL> self . allowed_methods = set ( allowed_methods or [ ] ) <EOL> if self . allowed_methods : <EOL> method_list = sorted ( self . allowed_methods ) <EOL> self . detail = '<STR_LIT>' % ( self . detail , <EOL> method_list ) <EOL> super ( MethodNotAllowed , self ) . __init__ ( * args , ** kwargs ) <EOL> class NotAcceptable ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class ProxyAuthenticationRequired ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class RequestTimeout ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class Conflict ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class Gone ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class LengthRequired ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class PreconditionFailed ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class RequestEntityTooLarge ( BadRequest ) : <EOL> "<STR_LIT>" <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class RequestURITooLong ( BadRequest ) : <EOL> "<STR_LIT>" <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class UnsupportedMediaType ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class RequestedRangeNotSatisfiable ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class ExpectationFailed ( BadRequest ) : <EOL> "<STR_LIT>" <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class ImATeapot ( BadRequest ) : <EOL> "<STR_LIT>" <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class UnprocessableEntity ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class UpgradeRequired ( BadRequest ) : <EOL> "<STR_LIT>" <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class PreconditionRequired ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class TooManyRequests ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class RequestHeaderFieldsTooLarge ( BadRequest ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class UnavailableForLegalReasons ( BadRequest ) : <EOL> "<STR_LIT>" <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class InternalServerError ( HTTPException ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def __init__ ( self , detail = None , ** kwargs ) : <EOL> self . exc_info = kwargs . pop ( '<STR_LIT>' , None ) <EOL> super ( InternalServerError , self ) . __init__ ( detail , ** kwargs ) <EOL> if self . error_type is None : <EOL> try : <EOL> exc_type_name = self . exc_info . exc_type <EOL> exc_type = getattr ( exceptions , exc_type_name ) <EOL> self . error_type = STDLIB_EXC_URL + exc_type . __name__ <EOL> except : <EOL> pass <EOL> def to_dict ( self ) : <EOL> ret = super ( InternalServerError , self ) . to_dict ( ) <EOL> try : <EOL> ret [ '<STR_LIT>' ] = self . exc_info . to_dict ( ) <EOL> except : <EOL> ret [ '<STR_LIT>' ] = None <EOL> return ret <EOL> class NotImplemented ( InternalServerError ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class BadGateway ( InternalServerError ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class ServiceUnavailable ( InternalServerError ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class GatewayTimeout ( InternalServerError ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class HTTPVersionNotSupported ( InternalServerError ) : <EOL> code = <NUM_LIT> <EOL> message = "<STR_LIT>" <EOL> detail = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> class ErrorHandler ( object ) : <EOL> wsgi_wrapper = None <EOL> not_found_type = NotFound <EOL> method_not_allowed_type = MethodNotAllowed <EOL> exc_info_type = ExceptionInfo <EOL> server_error_type = InternalServerError <EOL> def __init__ ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . reraise_uncaught = kwargs . get ( '<STR_LIT>' ) <EOL> def render_error ( self , request , _error , ** kwargs ) : <EOL> best_match = request . accept_mimetypes . best_match ( MIME_SUPPORT_MAP ) <EOL> _error . adapt ( best_match ) <EOL> return _error <EOL> def uncaught_to_response ( self , _application , _route , ** kwargs ) : <EOL> if self . reraise_uncaught : <EOL> raise <EOL> eh = _application . error_handler <EOL> exc_info = eh . exc_info_type . from_current ( ) <EOL> return eh . server_error_type ( repr ( exc_info ) , <EOL> exc_info = exc_info , <EOL> source_route = _route ) <EOL> class ContextualInternalServerError ( InternalServerError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * a , ** kw ) : <EOL> self . request = kw . get ( '<STR_LIT>' ) <EOL> self . hide_internal_frames = kw . pop ( '<STR_LIT>' , True ) <EOL> super ( ContextualInternalServerError , self ) . __init__ ( * a , ** kw ) <EOL> def to_dict ( self , * a , ** kw ) : <EOL> ret = super ( ContextualInternalServerError , self ) . to_dict ( * a , ** kw ) <EOL> del ret [ '<STR_LIT>' ] <EOL> exc_info = getattr ( self , '<STR_LIT>' , None ) <EOL> if not exc_info : <EOL> return ret <EOL> exc_tb = exc_info . tb_info . to_dict ( ) <EOL> for i , frame in enumerate ( exc_tb [ '<STR_LIT>' ] ) : <EOL> if self . hide_internal_frames : <EOL> if not frame [ '<STR_LIT>' ] and frame [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> frame [ '<STR_LIT>' ] = True <EOL> elif frame [ '<STR_LIT>' ] == '<STR_LIT>' and frame [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> frame [ '<STR_LIT>' ] = True <EOL> frame [ '<STR_LIT:id>' ] = i <EOL> try : <EOL> pre_start_lineno = frame [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> except IndexError : <EOL> pre_start_lineno = <NUM_LIT:1> <EOL> frame [ '<STR_LIT>' ] = pre_start_lineno <EOL> frame [ '<STR_LIT>' ] = frame [ '<STR_LIT>' ] + <NUM_LIT:1> <EOL> try : <EOL> last_frame = exc_tb [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] <EOL> except : <EOL> last_frame = None <EOL> eid = { '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : exc_info . exc_type , <EOL> '<STR_LIT>' : exc_info . exc_msg , <EOL> '<STR_LIT>' : exc_tb , <EOL> '<STR_LIT>' : last_frame , <EOL> '<STR_LIT>' : str ( exc_info . tb_info ) , <EOL> '<STR_LIT>' : str ( datetime . datetime . now ( ) ) , <EOL> '<STR_LIT>' : str ( datetime . datetime . utcnow ( ) ) , <EOL> '<STR_LIT>' : { '<STR_LIT>' : sys . executable , <EOL> '<STR_LIT:version>' : sys . version . replace ( '<STR_LIT:\n>' , '<STR_LIT:U+0020>' ) , <EOL> '<STR_LIT:path>' : sys . path } } <EOL> request = self . request <EOL> if request : <EOL> eid [ '<STR_LIT>' ] = { '<STR_LIT:path>' : request . path , <EOL> '<STR_LIT>' : request . url , <EOL> '<STR_LIT>' : request . method , <EOL> '<STR_LIT>' : request . path , <EOL> '<STR_LIT>' : request . args , <EOL> '<STR_LIT>' : request . cookies , <EOL> '<STR_LIT>' : request . headers , <EOL> '<STR_LIT>' : request . files } <EOL> ret . update ( eid ) <EOL> return ret <EOL> def to_html ( self , * a , ** kw ) : <EOL> render_ctx = self . to_dict ( ) <EOL> return CONTEXTUAL_ENV . render ( '<STR_LIT>' , render_ctx ) <EOL> class ContextualNotFound ( NotFound ) : <EOL> def __init__ ( self , * a , ** kw ) : <EOL> self . request = kw . get ( '<STR_LIT>' ) <EOL> self . application = kw . get ( '<STR_LIT>' ) <EOL> self . dispatch_state = kw . get ( '<STR_LIT>' ) <EOL> super ( ContextualNotFound , self ) . __init__ ( * a , ** kw ) <EOL> def to_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> ret = super ( ContextualNotFound , self ) . to_dict ( ) <EOL> app = self . application <EOL> if not app : <EOL> return ret <EOL> route_results = [ ] <EOL> for route in app . routes : <EOL> cur = { '<STR_LIT>' : route . pattern , <EOL> '<STR_LIT>' : route . regex . pattern } <EOL> if route . methods : <EOL> cur [ '<STR_LIT>' ] = sorted ( route . methods ) <EOL> route_results . append ( cur ) <EOL> ret [ '<STR_LIT>' ] = route_results <EOL> if self . request : <EOL> ret [ '<STR_LIT>' ] = _req = { } <EOL> _req [ '<STR_LIT:path>' ] = self . request . path <EOL> _req [ '<STR_LIT>' ] = self . request . method <EOL> return ret <EOL> def to_html ( self , * a , ** kw ) : <EOL> render_ctx = self . to_dict ( ) <EOL> return CONTEXTUAL_ENV . render ( '<STR_LIT>' , render_ctx ) <EOL> class ContextualErrorHandler ( ErrorHandler ) : <EOL> exc_info_type = ContextualExceptionInfo <EOL> server_error_type = ContextualInternalServerError <EOL> not_found_type = ContextualNotFound <EOL> def __init__ ( self , * a , ** kw ) : <EOL> self . hide_internal_frames = kw . pop ( '<STR_LIT>' , True ) <EOL> super ( ContextualErrorHandler , self ) . __init__ ( * a , ** kw ) <EOL> def uncaught_to_response ( self , _application , _route , ** kwargs ) : <EOL> eh = _application . error_handler <EOL> exc_info = eh . exc_info_type . from_current ( ) <EOL> SEType = eh . server_error_type <EOL> return SEType ( repr ( exc_info ) , <EOL> exc_info = exc_info , <EOL> source_route = _route , <EOL> request = kwargs . get ( '<STR_LIT>' ) , <EOL> hide_internal_frames = self . hide_internal_frames ) <EOL> class _REPLDebuggedApplication ( DebuggedApplication ) : <EOL> def __init__ ( self , app , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = True <EOL> super ( _REPLDebuggedApplication , self ) . __init__ ( app , ** kwargs ) <EOL> class REPLErrorHandler ( ContextualErrorHandler ) : <EOL> wsgi_wrapper = _REPLDebuggedApplication <EOL> def uncaught_to_response ( self , ** kwargs ) : <EOL> print '<STR_LIT>' <EOL> raise <EOL> _module_init ( ) </s>
