<s> try : <EOL> from django . http import HttpResponse as DjangoHttpResponse <EOL> has_django = True <EOL> except : <EOL> has_django = False <EOL> try : <EOL> from flask import Response as FlaskResponse <EOL> has_flask = True <EOL> except ImportError : <EOL> has_flask = False <EOL> from simpleapi . message import formatters , wrappers <EOL> from preformat import Preformatter <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class UnformattedResponse ( object ) : <EOL> def __init__ ( self , content , mimetype = "<STR_LIT>" ) : <EOL> self . content = content <EOL> self . mimetype = mimetype <EOL> class ResponseMerger ( object ) : <EOL> def __init__ ( self , sapi_request , responses ) : <EOL> self . sapi_request = sapi_request <EOL> self . responses = responses <EOL> def build ( self ) : <EOL> if len ( self . responses ) == <NUM_LIT:1> : <EOL> return self . responses [ <NUM_LIT:0> ] . build ( ) <EOL> else : <EOL> results = [ ] <EOL> for response in self . responses : <EOL> if response . has_errors ( ) : <EOL> results . append ( response . build ( <EOL> managed = True , <EOL> skip_features = True <EOL> ) ) <EOL> else : <EOL> results . append ( response . build ( <EOL> managed = True <EOL> ) ) <EOL> result = u'<STR_LIT>' % u'<STR_LIT:U+002C>' . join ( map ( lambda x : x [ '<STR_LIT:result>' ] , results ) ) <EOL> return Response . _build_response_obj ( <EOL> sapi_request = self . sapi_request , <EOL> response = { <EOL> '<STR_LIT:result>' : result , <EOL> '<STR_LIT>' : results [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> } <EOL> ) <EOL> class ResponseException ( object ) : pass <EOL> class Response ( object ) : <EOL> def __init__ ( self , sapi_request , namespace = None , output_formatter = None , <EOL> wrapper = None , errors = None , result = None , mimetype = None , <EOL> callback = None , function = None ) : <EOL> assert isinstance ( errors , ( basestring , list ) ) or errors is None <EOL> self . sapi_request = sapi_request <EOL> self . namespace = namespace <EOL> self . errors = errors <EOL> self . result = self . _preformat ( result ) <EOL> self . mimetype = mimetype <EOL> self . callback = None <EOL> self . function = function <EOL> self . output_formatter = output_formatter or formatters [ '<STR_LIT>' ] <EOL> self . wrapper = wrapper or wrappers [ '<STR_LIT:default>' ] <EOL> self . mimetype = mimetype or self . output_formatter . __mime__ <EOL> self . session = self . sapi_request . session <EOL> def has_errors ( self ) : <EOL> return self . errors is not None <EOL> def add_error ( self , errmsg ) : <EOL> if self . errors is None : <EOL> self . errors = [ errmsg , ] <EOL> else : <EOL> if isinstance ( self . errors , list ) : <EOL> self . errors . append ( errmsg ) <EOL> elif isinstance ( self . errors , basestring ) : <EOL> self . errors = [ self . errors , errmsg ] <EOL> def _preformat ( self , value ) : <EOL> if not isinstance ( value , UnformattedResponse ) : <EOL> preformatter = Preformatter ( ) <EOL> return preformatter . run ( value ) <EOL> return value <EOL> def build ( self , skip_features = False , managed = False ) : <EOL> if hasattr ( self . session . _internal , '<STR_LIT>' ) : <EOL> namespace_instance = self . session . _internal . namespace [ '<STR_LIT>' ] <EOL> if hasattr ( namespace_instance , '<STR_LIT>' ) : <EOL> getattr ( namespace_instance , '<STR_LIT>' ) ( self , self . session ) <EOL> if self . namespace and not skip_features : <EOL> for feature in self . namespace [ '<STR_LIT>' ] : <EOL> feature . _handle_response ( self ) <EOL> if not isinstance ( self . result , UnformattedResponse ) : <EOL> if self . function : <EOL> self . result = self . function [ '<STR_LIT>' ] ( self . result ) <EOL> if isinstance ( self . output_formatter , type ) : <EOL> self . output_formatter = self . output_formatter ( <EOL> sapi_request = self . sapi_request , <EOL> callback = self . callback <EOL> ) <EOL> if isinstance ( self . wrapper , type ) : <EOL> self . wrapper = self . wrapper ( <EOL> sapi_request = self . sapi_request <EOL> ) <EOL> wrapper_result = self . wrapper . _build ( <EOL> errors = self . errors , <EOL> result = self . result , <EOL> ) <EOL> formatter_result = self . output_formatter . build ( wrapper_result ) <EOL> else : <EOL> self . mimetype = self . result . mimetype <EOL> formatter_result = self . result . content <EOL> result = { '<STR_LIT:result>' : formatter_result , '<STR_LIT>' : self . mimetype } <EOL> if managed : <EOL> return result <EOL> else : <EOL> return self . _build_response_obj ( self . sapi_request , result ) <EOL> @ staticmethod <EOL> def _build_response_obj ( sapi_request , response ) : <EOL> if sapi_request . route . is_flask ( ) : <EOL> assert has_flask , '<STR_LIT>' <EOL> return FlaskResponse ( <EOL> response = response [ '<STR_LIT:result>' ] , <EOL> mimetype = response [ '<STR_LIT>' ] <EOL> ) <EOL> elif sapi_request . route . is_django ( ) : <EOL> assert has_django , '<STR_LIT>' <EOL> return DjangoHttpResponse ( <EOL> response [ '<STR_LIT:result>' ] , <EOL> content_type = response [ '<STR_LIT>' ] <EOL> ) <EOL> else : <EOL> return response </s>
