<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import cStringIO <EOL> import httplib <EOL> import re <EOL> from . . import util <EOL> __all__ = [ '<STR_LIT>' , <EOL> '<STR_LIT:error>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> _STATUS_PATTERN = re . compile ( '<STR_LIT>' ) <EOL> @ util . positional ( <NUM_LIT:1> ) <EOL> def static_page ( content = '<STR_LIT>' , <EOL> status = '<STR_LIT>' , <EOL> content_type = '<STR_LIT>' , <EOL> headers = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( status , ( int , long ) ) : <EOL> status = '<STR_LIT>' % ( status , httplib . responses . get ( status , '<STR_LIT>' ) ) <EOL> elif not isinstance ( status , basestring ) : <EOL> status = '<STR_LIT>' % tuple ( status ) <EOL> if isinstance ( headers , dict ) : <EOL> headers = headers . iteritems ( ) <EOL> headers = [ ( '<STR_LIT>' , str ( len ( content ) ) ) , <EOL> ( '<STR_LIT>' , content_type ) , <EOL> ] + list ( headers or [ ] ) <EOL> for index , ( key , value ) in enumerate ( headers ) : <EOL> if isinstance ( value , unicode ) : <EOL> value = value . encode ( '<STR_LIT:utf-8>' ) <EOL> headers [ index ] = key , value <EOL> if not isinstance ( key , str ) : <EOL> raise TypeError ( '<STR_LIT>' % ( key , ) ) <EOL> if not isinstance ( value , str ) : <EOL> raise TypeError ( <EOL> '<STR_LIT>' % ( key , value ) ) <EOL> def static_page_application ( environ , start_response ) : <EOL> start_response ( status , headers ) <EOL> return [ content ] <EOL> return static_page_application <EOL> @ util . positional ( <NUM_LIT:2> ) <EOL> def error ( status_code , status_message = None , <EOL> content_type = '<STR_LIT>' , <EOL> headers = None , content = None ) : <EOL> """<STR_LIT>""" <EOL> if status_message is None : <EOL> status_message = httplib . responses . get ( status_code , '<STR_LIT>' ) <EOL> if content is None : <EOL> content = status_message <EOL> content = util . pad_string ( content ) <EOL> return static_page ( content , <EOL> status = ( status_code , status_message ) , <EOL> content_type = content_type , <EOL> headers = headers ) <EOL> def first_found ( apps ) : <EOL> """<STR_LIT>""" <EOL> apps = tuple ( apps ) <EOL> not_found = error ( httplib . NOT_FOUND ) <EOL> def first_found_app ( environ , start_response ) : <EOL> """<STR_LIT>""" <EOL> final_result = { } <EOL> def first_found_start_response ( status , response_headers ) : <EOL> """<STR_LIT>""" <EOL> status_match = _STATUS_PATTERN . match ( status ) <EOL> assert status_match , ( '<STR_LIT>' <EOL> '<STR_LIT>' % status ) <EOL> status_code = status_match . group ( <NUM_LIT:0> ) <EOL> if int ( status_code ) == httplib . NOT_FOUND : <EOL> return <EOL> final_result [ '<STR_LIT:status>' ] = status <EOL> final_result [ '<STR_LIT>' ] = response_headers <EOL> for app in apps : <EOL> response = app ( environ , first_found_start_response ) <EOL> if final_result : <EOL> start_response ( final_result [ '<STR_LIT:status>' ] , final_result [ '<STR_LIT>' ] ) <EOL> return response <EOL> return not_found ( environ , start_response ) <EOL> return first_found_app </s>
