<s> import threading <EOL> from pump . util . response import skeleton <EOL> def build_app ( wsgi_app ) : <EOL> def app ( request ) : <EOL> data = threading . local ( ) <EOL> def start_response ( status , headers , _ = None ) : <EOL> data . status = status <EOL> data . headers = headers <EOL> body = wsgi_app ( build_wsgi_request ( request ) , start_response ) <EOL> return build_response ( ( data . status , data . headers , body ) ) <EOL> return app <EOL> def build_request ( wsgi_req ) : <EOL> return dict ( { <EOL> '<STR_LIT>' : wsgi_req . get ( '<STR_LIT>' ) or wsgi_req . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : wsgi_req . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : wsgi_req . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) , <EOL> '<STR_LIT>' : get_headers ( wsgi_req ) , <EOL> '<STR_LIT:body>' : wsgi_req . get ( '<STR_LIT>' ) <EOL> } , ** dict ( [ ( k . lower ( ) , v ) for k , v in wsgi_req . iteritems ( ) ] ) ) <EOL> def build_response ( wsgi_response ) : <EOL> status , headers , body = wsgi_response <EOL> return { <EOL> "<STR_LIT:status>" : int ( status [ : <NUM_LIT:3> ] ) , <EOL> "<STR_LIT>" : dict ( [ ( k . replace ( "<STR_LIT:->" , "<STR_LIT:_>" ) . lower ( ) , v ) for k , v in headers ] ) , <EOL> "<STR_LIT:body>" : body } <EOL> def build_middleware ( wsgi_middleware , * args ) : <EOL> def middleware ( app ) : <EOL> return build_app ( wsgi_middleware ( build_wsgi_app ( app ) , * args ) ) <EOL> return middleware <EOL> def build_wsgi_app ( app ) : <EOL> def wsgi_app ( request , start_response , _ = None ) : <EOL> response = app ( build_request ( request ) ) or { } <EOL> response_map = dict ( skeleton , ** response ) <EOL> return build_wsgi_response ( response_map , start_response ) <EOL> return wsgi_app <EOL> def build_wsgi_request ( request ) : <EOL> wsgi_request = { <EOL> "<STR_LIT>" : request . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : request . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : request . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : request . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : request . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : request . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : request . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : request . get ( "<STR_LIT>" , "<STR_LIT:GET>" ) . upper ( ) , <EOL> "<STR_LIT>" : request . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : request . get ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : request . get ( "<STR_LIT:body>" ) } <EOL> for key , value in request . get ( "<STR_LIT>" , { } ) . iteritems ( ) : <EOL> wsgi_request [ "<STR_LIT>" % key . upper ( ) ] = value <EOL> for key , value in request . iteritems ( ) : <EOL> if key . upper ( ) not in wsgi_request : <EOL> wsgi_request [ key ] = value <EOL> return wsgi_request <EOL> def build_wsgi_response ( response_map , start_response ) : <EOL> response = { } <EOL> response = set_status ( response , response_map [ "<STR_LIT:status>" ] ) <EOL> response = set_headers ( response , response_map [ "<STR_LIT>" ] ) <EOL> response = set_body ( response , response_map [ "<STR_LIT:body>" ] ) <EOL> start_response ( response [ "<STR_LIT:status>" ] , response [ "<STR_LIT>" ] ) <EOL> return response [ "<STR_LIT:body>" ] <EOL> def get_headers ( request ) : <EOL> return dict ( [ ( k . replace ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) , v ) for k , v in request . items ( ) <EOL> if k . startswith ( '<STR_LIT>' ) ] ) <EOL> def set_status ( response , status ) : <EOL> status_map = { <EOL> <NUM_LIT:200> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" , <EOL> <NUM_LIT> : "<STR_LIT>" } <EOL> response [ "<STR_LIT:status>" ] = status_map . get ( status , str ( status ) ) <EOL> return response <EOL> def set_headers ( response , headers ) : <EOL> response [ "<STR_LIT>" ] = [ ] <EOL> for key , val in headers . items ( ) : <EOL> key = key . replace ( "<STR_LIT:_>" , "<STR_LIT:->" ) . title ( ) <EOL> if isinstance ( val , list ) : <EOL> for v in val : <EOL> response [ "<STR_LIT>" ] . append ( ( key , v ) ) <EOL> else : <EOL> response [ "<STR_LIT>" ] . append ( ( key , val ) ) <EOL> return response <EOL> def set_body ( response , body ) : <EOL> if isinstance ( body , str ) or isinstance ( body , unicode ) : <EOL> response [ "<STR_LIT:body>" ] = body <EOL> elif isinstance ( body , list ) : <EOL> response [ "<STR_LIT:body>" ] = '<STR_LIT>' . join ( body ) <EOL> elif isinstance ( body , file ) : <EOL> response [ "<STR_LIT:body>" ] = body . read ( ) <EOL> elif not body : <EOL> response [ "<STR_LIT:body>" ] = "<STR_LIT>" <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % repr ( body ) ) <EOL> return response </s>
