<s> """<STR_LIT>""" <EOL> from ants . http import Request , Response <EOL> from ants . utils . middleware import MiddlewareManager <EOL> from ants . utils . defer import mustbe_deferred <EOL> from ants . utils . conf import build_component_list <EOL> class DownloaderMiddlewareManager ( MiddlewareManager ) : <EOL> component_name = '<STR_LIT>' <EOL> @ classmethod <EOL> def _get_mwlist_from_settings ( cls , settings ) : <EOL> return build_component_list ( <EOL> settings [ '<STR_LIT>' ] , <EOL> settings [ '<STR_LIT>' ] ) <EOL> def _add_middleware ( self , mw ) : <EOL> if hasattr ( mw , '<STR_LIT>' ) : <EOL> self . methods [ '<STR_LIT>' ] . append ( mw . process_request ) <EOL> if hasattr ( mw , '<STR_LIT>' ) : <EOL> self . methods [ '<STR_LIT>' ] . insert ( <NUM_LIT:0> , mw . process_response ) <EOL> if hasattr ( mw , '<STR_LIT>' ) : <EOL> self . methods [ '<STR_LIT>' ] . insert ( <NUM_LIT:0> , mw . process_exception ) <EOL> def download ( self , download_func , request , spider ) : <EOL> def process_request ( request ) : <EOL> for method in self . methods [ '<STR_LIT>' ] : <EOL> response = method ( request = request , spider = spider ) <EOL> assert response is None or isinstance ( response , ( Response , Request ) ) , '<STR_LIT>' % ( method . im_self . __class__ . __name__ , response . __class__ . __name__ ) <EOL> if response : <EOL> return response <EOL> return download_func ( request = request , spider = spider ) <EOL> def process_response ( response ) : <EOL> assert response is not None , '<STR_LIT>' <EOL> if isinstance ( response , Request ) : <EOL> return response <EOL> for method in self . methods [ '<STR_LIT>' ] : <EOL> response = method ( request = request , response = response , spider = spider ) <EOL> assert isinstance ( response , ( Response , Request ) ) , '<STR_LIT>' % ( method . im_self . __class__ . __name__ , type ( response ) ) <EOL> if isinstance ( response , Request ) : <EOL> return response <EOL> return response <EOL> def process_exception ( _failure ) : <EOL> exception = _failure . value <EOL> for method in self . methods [ '<STR_LIT>' ] : <EOL> response = method ( request = request , exception = exception , spider = spider ) <EOL> assert response is None or isinstance ( response , ( Response , Request ) ) , '<STR_LIT>' % ( method . im_self . __class__ . __name__ , type ( response ) ) <EOL> if response : <EOL> return response <EOL> return _failure <EOL> deferred = mustbe_deferred ( process_request , request ) <EOL> deferred . addErrback ( process_exception ) <EOL> deferred . addCallback ( process_response ) <EOL> return deferred </s>
