<s> from __future__ import unicode_literals <EOL> from datetime import timedelta , datetime <EOL> from functools import wraps <EOL> from six import iteritems <EOL> from flask import _request_ctx_stack <EOL> from werkzeug . datastructures import ResponseCacheControl <EOL> from werkzeug . local import LocalProxy <EOL> after_request = LocalProxy ( lambda : _request_ctx_stack . top . web_cache ) <EOL> def setup_for_this_request ( ) : <EOL> _request_ctx_stack . top . web_cache = [ ] <EOL> class BaseModifier ( object ) : <EOL> def __call__ ( self , func ) : <EOL> @ wraps ( func ) <EOL> def inner ( * args , ** kwargs ) : <EOL> self . after_this_request ( ) <EOL> return func ( * args , ** kwargs ) <EOL> return inner <EOL> def after_this_request ( self ) : <EOL> after_request . append ( self . modify_response ) <EOL> def modify_response ( self , response ) : <EOL> pass <EOL> class cache_for ( BaseModifier ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** durations ) : <EOL> self . durations = durations <EOL> def modify_response ( self , response ) : <EOL> delta = timedelta ( ** self . durations ) <EOL> response . cache_control . max_age = int ( delta . total_seconds ( ) ) <EOL> response . expires = datetime . utcnow ( ) + delta <EOL> class cache_control ( BaseModifier ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , ** kwargs ) : <EOL> for key , value in iteritems ( kwargs ) : <EOL> if not hasattr ( ResponseCacheControl , key ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> % ( self . __class__ . __name__ , key ) ) <EOL> self . kwargs = kwargs <EOL> def modify_response ( self , response ) : <EOL> for key , value in iteritems ( self . kwargs ) : <EOL> setattr ( response . cache_control , key , value ) </s>
