<s> """<STR_LIT>""" <EOL> import logging <EOL> import sys <EOL> import types <EOL> from google . appengine import runtime <EOL> from google . appengine . api import lib_config <EOL> _DEADLINE_DURING_LOADING = <NUM_LIT> <EOL> class Error ( Exception ) : <EOL> pass <EOL> class InvalidResponseError ( Error ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _GetTypeName ( x ) : <EOL> """<STR_LIT>""" <EOL> if type ( x ) is types . InstanceType : <EOL> return x . __class__ . __name__ <EOL> else : <EOL> return type ( x ) . __name__ <EOL> class WsgiRequest ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , environ , handler_name , url , post_data , error ) : <EOL> """<STR_LIT>""" <EOL> self . _handler = handler_name <EOL> self . _status = <NUM_LIT> <EOL> self . _response_headers = [ ] <EOL> self . _started_handling = False <EOL> self . _body = [ ] <EOL> self . _written_body = [ ] <EOL> environ [ '<STR_LIT>' ] = True <EOL> environ [ '<STR_LIT>' ] = False <EOL> environ [ '<STR_LIT>' ] = ( <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> environ . setdefault ( '<STR_LIT>' , False ) <EOL> self . _error = error <EOL> environ [ '<STR_LIT>' ] = url . scheme <EOL> environ [ '<STR_LIT>' ] = post_data <EOL> environ [ '<STR_LIT>' ] = self . _error <EOL> self . _environ = environ <EOL> def _Write ( self , body_data ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( body_data , str ) : <EOL> raise InvalidResponseError ( '<STR_LIT>' % <EOL> _GetTypeName ( body_data ) ) <EOL> self . _written_body . append ( body_data ) <EOL> def _StartResponse ( self , status , response_headers , exc_info = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( status , str ) : <EOL> raise InvalidResponseError ( '<STR_LIT>' % <EOL> ( _GetTypeName ( status ) , status ) ) <EOL> if not status : <EOL> raise InvalidResponseError ( '<STR_LIT>' ) <EOL> if not isinstance ( response_headers , list ) : <EOL> raise InvalidResponseError ( '<STR_LIT>' % <EOL> _GetTypeName ( response_headers ) ) <EOL> for header in response_headers : <EOL> if not isinstance ( header , tuple ) : <EOL> raise InvalidResponseError ( '<STR_LIT>' <EOL> '<STR_LIT>' % _GetTypeName ( header ) ) <EOL> if len ( header ) != <NUM_LIT:2> : <EOL> raise InvalidResponseError ( '<STR_LIT>' <EOL> '<STR_LIT>' % len ( header ) ) <EOL> name , value = header <EOL> if not isinstance ( name , str ) : <EOL> raise InvalidResponseError ( '<STR_LIT>' % <EOL> ( _GetTypeName ( name ) , name ) ) <EOL> if not isinstance ( value , str ) : <EOL> raise InvalidResponseError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( _GetTypeName ( value ) , value , name ) ) <EOL> try : <EOL> status_number = int ( status . split ( '<STR_LIT:U+0020>' ) [ <NUM_LIT:0> ] ) <EOL> except ValueError : <EOL> raise InvalidResponseError ( '<STR_LIT>' % status ) <EOL> if status_number < <NUM_LIT:200> or status_number >= <NUM_LIT> : <EOL> raise InvalidResponseError ( '<STR_LIT>' <EOL> '<STR_LIT>' % status_number ) <EOL> if exc_info is not None : <EOL> self . _status = status_number <EOL> self . _response_headers = response_headers <EOL> exc_info = None <EOL> elif self . _started_handling : <EOL> raise InvalidResponseError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> self . _status = status_number <EOL> self . _response_headers = response_headers <EOL> self . _started_handling = True <EOL> self . _body = [ ] <EOL> self . _written_body = [ ] <EOL> return self . _Write <EOL> def Handle ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> handler = _config_handle . add_wsgi_middleware ( self . _LoadHandler ( ) ) <EOL> except runtime . DeadlineExceededError : <EOL> exc_info = sys . exc_info ( ) <EOL> try : <EOL> logging . error ( '<STR_LIT>' , exc_info = exc_info ) <EOL> except runtime . DeadlineExceededError : <EOL> logging . exception ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> logging . error ( '<STR_LIT>' , exc_info = exc_info ) <EOL> return { '<STR_LIT:error>' : _DEADLINE_DURING_LOADING } <EOL> except : <EOL> logging . exception ( '<STR_LIT>' ) <EOL> return { '<STR_LIT:error>' : <NUM_LIT:1> } <EOL> result = None <EOL> try : <EOL> result = handler ( dict ( self . _environ ) , self . _StartResponse ) <EOL> for chunk in result : <EOL> if not isinstance ( chunk , str ) : <EOL> raise InvalidResponseError ( '<STR_LIT>' ) <EOL> self . _body . append ( chunk ) <EOL> body = '<STR_LIT>' . join ( self . _written_body + self . _body ) <EOL> return { '<STR_LIT>' : self . _status , '<STR_LIT>' : <EOL> self . _response_headers , '<STR_LIT:body>' : body } <EOL> except : <EOL> logging . exception ( '<STR_LIT>' ) <EOL> return { '<STR_LIT:error>' : <NUM_LIT:1> } <EOL> finally : <EOL> if hasattr ( result , '<STR_LIT>' ) : <EOL> result . close ( ) <EOL> def _LoadHandler ( self ) : <EOL> """<STR_LIT>""" <EOL> path = self . _handler . split ( '<STR_LIT:.>' ) <EOL> handler = __import__ ( path [ <NUM_LIT:0> ] ) <EOL> is_parent_package = True <EOL> cumulative_path = path [ <NUM_LIT:0> ] <EOL> for name in path [ <NUM_LIT:1> : ] : <EOL> if hasattr ( handler , '<STR_LIT>' ) : <EOL> self . _environ [ '<STR_LIT>' ] = handler . __file__ <EOL> is_parent_package = is_parent_package and hasattr ( handler , '<STR_LIT>' ) <EOL> cumulative_path += '<STR_LIT:.>' + name <EOL> if hasattr ( handler , name ) : <EOL> handler = getattr ( handler , name ) <EOL> elif is_parent_package : <EOL> __import__ ( cumulative_path ) <EOL> handler = getattr ( handler , name ) <EOL> else : <EOL> raise ImportError ( '<STR_LIT>' % ( handler , name ) ) <EOL> return handler <EOL> def HandleRequest ( environ , handler_name , url , post_data , error ) : <EOL> """<STR_LIT>""" <EOL> return WsgiRequest ( environ , handler_name , url , post_data , error ) . Handle ( ) <EOL> _config_handle = lib_config . register ( <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : lambda app : app } ) </s>
