<s> from email . utils import formatdate <EOL> from ants import signals <EOL> from ants . utils . exceptions import NotConfigured , IgnoreRequest <EOL> from ants . utils . misc import load_object <EOL> class HttpCacheMiddleware ( object ) : <EOL> def __init__ ( self , settings , stats ) : <EOL> if not settings . getbool ( '<STR_LIT>' ) : <EOL> raise NotConfigured <EOL> self . policy = load_object ( settings [ '<STR_LIT>' ] ) ( settings ) <EOL> self . storage = load_object ( settings [ '<STR_LIT>' ] ) ( settings ) <EOL> self . ignore_missing = settings . getbool ( '<STR_LIT>' ) <EOL> self . stats = stats <EOL> @ classmethod <EOL> def from_crawler ( cls , crawler ) : <EOL> o = cls ( crawler . settings , crawler . stats ) <EOL> crawler . signals . connect ( o . spider_opened , signal = signals . spider_opened ) <EOL> crawler . signals . connect ( o . spider_closed , signal = signals . spider_closed ) <EOL> return o <EOL> def spider_opened ( self , spider ) : <EOL> self . storage . open_spider ( spider ) <EOL> def spider_closed ( self , spider ) : <EOL> self . storage . close_spider ( spider ) <EOL> def process_request ( self , request , spider ) : <EOL> if not self . policy . should_cache_request ( request ) : <EOL> request . meta [ '<STR_LIT>' ] = True <EOL> return <EOL> cachedresponse = self . storage . retrieve_response ( spider , request ) <EOL> if cachedresponse is None : <EOL> self . stats . inc_value ( '<STR_LIT>' , spider = spider ) <EOL> if self . ignore_missing : <EOL> self . stats . inc_value ( '<STR_LIT>' , spider = spider ) <EOL> raise IgnoreRequest ( "<STR_LIT>" % request ) <EOL> return <EOL> cachedresponse . flags . append ( '<STR_LIT>' ) <EOL> if self . policy . is_cached_response_fresh ( cachedresponse , request ) : <EOL> self . stats . inc_value ( '<STR_LIT>' , spider = spider ) <EOL> return cachedresponse <EOL> request . meta [ '<STR_LIT>' ] = cachedresponse <EOL> def process_response ( self , request , response , spider ) : <EOL> if '<STR_LIT>' in response . flags or '<STR_LIT>' in request . meta : <EOL> request . meta . pop ( '<STR_LIT>' , None ) <EOL> return response <EOL> if '<STR_LIT>' not in response . headers : <EOL> response . headers [ '<STR_LIT>' ] = formatdate ( usegmt = <NUM_LIT:1> ) <EOL> cachedresponse = request . meta . pop ( '<STR_LIT>' , None ) <EOL> if cachedresponse is None : <EOL> self . stats . inc_value ( '<STR_LIT>' , spider = spider ) <EOL> self . _cache_response ( spider , response , request , cachedresponse ) <EOL> return response <EOL> if self . policy . is_cached_response_valid ( cachedresponse , response , request ) : <EOL> self . stats . inc_value ( '<STR_LIT>' , spider = spider ) <EOL> return cachedresponse <EOL> self . stats . inc_value ( '<STR_LIT>' , spider = spider ) <EOL> self . _cache_response ( spider , response , request , cachedresponse ) <EOL> return response <EOL> def _cache_response ( self , spider , response , request , cachedresponse ) : <EOL> if self . policy . should_cache_response ( response , request ) : <EOL> self . stats . inc_value ( '<STR_LIT>' , spider = spider ) <EOL> self . storage . store_response ( spider , request , response ) <EOL> else : <EOL> self . stats . inc_value ( '<STR_LIT>' , spider = spider ) <EOL> from ants . contrib . httpcache import FilesystemCacheStorage as _FilesystemCacheStorage <EOL> class FilesystemCacheStorage ( _FilesystemCacheStorage ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> import warnings <EOL> from ants . utils . exceptions import ScrapyDeprecationWarning <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> category = ScrapyDeprecationWarning , stacklevel = <NUM_LIT:1> ) <EOL> super ( FilesystemCacheStorage , self ) . __init__ ( * args , ** kwargs ) </s>
