<s> """<STR_LIT>""" <EOL> import six <EOL> from twisted . python . failure import Failure <EOL> from scrapy . middleware import MiddlewareManager <EOL> from scrapy . utils . defer import mustbe_deferred <EOL> from scrapy . utils . conf import build_component_list <EOL> def _isiterable ( possible_iterator ) : <EOL> return hasattr ( possible_iterator , '<STR_LIT>' ) <EOL> class SpiderMiddlewareManager ( MiddlewareManager ) : <EOL> component_name = '<STR_LIT>' <EOL> @ classmethod <EOL> def _get_mwlist_from_settings ( cls , settings ) : <EOL> return build_component_list ( settings . getwithbase ( '<STR_LIT>' ) ) <EOL> def _add_middleware ( self , mw ) : <EOL> super ( SpiderMiddlewareManager , self ) . _add_middleware ( mw ) <EOL> if hasattr ( mw , '<STR_LIT>' ) : <EOL> self . methods [ '<STR_LIT>' ] . append ( mw . process_spider_input ) <EOL> if hasattr ( mw , '<STR_LIT>' ) : <EOL> self . methods [ '<STR_LIT>' ] . insert ( <NUM_LIT:0> , mw . process_spider_output ) <EOL> if hasattr ( mw , '<STR_LIT>' ) : <EOL> self . methods [ '<STR_LIT>' ] . insert ( <NUM_LIT:0> , mw . process_spider_exception ) <EOL> if hasattr ( mw , '<STR_LIT>' ) : <EOL> self . methods [ '<STR_LIT>' ] . insert ( <NUM_LIT:0> , mw . process_start_requests ) <EOL> def scrape_response ( self , scrape_func , response , request , spider ) : <EOL> fname = lambda f : '<STR_LIT>' % ( <EOL> six . get_method_self ( f ) . __class__ . __name__ , <EOL> six . get_method_function ( f ) . __name__ ) <EOL> def process_spider_input ( response ) : <EOL> for method in self . methods [ '<STR_LIT>' ] : <EOL> try : <EOL> result = method ( response = response , spider = spider ) <EOL> assert result is None , '<STR_LIT>' '<STR_LIT>' % ( fname ( method ) , type ( result ) ) <EOL> except : <EOL> return scrape_func ( Failure ( ) , request , spider ) <EOL> return scrape_func ( response , request , spider ) <EOL> def process_spider_exception ( _failure ) : <EOL> exception = _failure . value <EOL> for method in self . methods [ '<STR_LIT>' ] : <EOL> result = method ( response = response , exception = exception , spider = spider ) <EOL> assert result is None or _isiterable ( result ) , '<STR_LIT>' % ( fname ( method ) , type ( result ) ) <EOL> if result is not None : <EOL> return result <EOL> return _failure <EOL> def process_spider_output ( result ) : <EOL> for method in self . methods [ '<STR_LIT>' ] : <EOL> result = method ( response = response , result = result , spider = spider ) <EOL> assert _isiterable ( result ) , '<STR_LIT>' % ( fname ( method ) , type ( result ) ) <EOL> return result <EOL> dfd = mustbe_deferred ( process_spider_input , response ) <EOL> dfd . addErrback ( process_spider_exception ) <EOL> dfd . addCallback ( process_spider_output ) <EOL> return dfd <EOL> def process_start_requests ( self , start_requests , spider ) : <EOL> return self . _process_chain ( '<STR_LIT>' , start_requests , spider ) </s>
