<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import datetime <EOL> import json <EOL> import logging <EOL> import os <EOL> import tempfile <EOL> import threading <EOL> try : <EOL> from oauth2client . contrib . locked_file import LockedFile <EOL> except ImportError : <EOL> from oauth2client . locked_file import LockedFile <EOL> from . import base <EOL> from . . discovery_cache import DISCOVERY_DOC_MAX_AGE <EOL> LOGGER = logging . getLogger ( __name__ ) <EOL> FILENAME = '<STR_LIT>' <EOL> EPOCH = datetime . datetime . utcfromtimestamp ( <NUM_LIT:0> ) <EOL> def _to_timestamp ( date ) : <EOL> try : <EOL> return ( date - EPOCH ) . total_seconds ( ) <EOL> except AttributeError : <EOL> delta = date - EPOCH <EOL> return ( ( delta . microseconds + ( delta . seconds + delta . days * <NUM_LIT> * <NUM_LIT> ) <EOL> * <NUM_LIT:10> ** <NUM_LIT:6> ) / <NUM_LIT:10> ** <NUM_LIT:6> ) <EOL> def _read_or_initialize_cache ( f ) : <EOL> f . file_handle ( ) . seek ( <NUM_LIT:0> ) <EOL> try : <EOL> cache = json . load ( f . file_handle ( ) ) <EOL> except Exception : <EOL> cache = { } <EOL> f . file_handle ( ) . truncate ( <NUM_LIT:0> ) <EOL> f . file_handle ( ) . seek ( <NUM_LIT:0> ) <EOL> json . dump ( cache , f . file_handle ( ) ) <EOL> return cache <EOL> class Cache ( base . Cache ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , max_age ) : <EOL> """<STR_LIT>""" <EOL> self . _max_age = max_age <EOL> self . _file = os . path . join ( tempfile . gettempdir ( ) , FILENAME ) <EOL> f = LockedFile ( self . _file , '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> try : <EOL> f . open_and_lock ( ) <EOL> if f . is_locked ( ) : <EOL> _read_or_initialize_cache ( f ) <EOL> except Exception as e : <EOL> LOGGER . warning ( e , exc_info = True ) <EOL> finally : <EOL> f . unlock_and_close ( ) <EOL> def get ( self , url ) : <EOL> f = LockedFile ( self . _file , '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> try : <EOL> f . open_and_lock ( ) <EOL> if f . is_locked ( ) : <EOL> cache = _read_or_initialize_cache ( f ) <EOL> if url in cache : <EOL> content , t = cache . get ( url , ( None , <NUM_LIT:0> ) ) <EOL> if _to_timestamp ( datetime . datetime . now ( ) ) < t + self . _max_age : <EOL> return content <EOL> return None <EOL> else : <EOL> LOGGER . debug ( '<STR_LIT>' ) <EOL> return None <EOL> except Exception as e : <EOL> LOGGER . warning ( e , exc_info = True ) <EOL> finally : <EOL> f . unlock_and_close ( ) <EOL> def set ( self , url , content ) : <EOL> f = LockedFile ( self . _file , '<STR_LIT>' , '<STR_LIT:r>' ) <EOL> try : <EOL> f . open_and_lock ( ) <EOL> if f . is_locked ( ) : <EOL> cache = _read_or_initialize_cache ( f ) <EOL> cache [ url ] = ( content , _to_timestamp ( datetime . datetime . now ( ) ) ) <EOL> for k , ( _ , timestamp ) in list ( cache . items ( ) ) : <EOL> if _to_timestamp ( datetime . datetime . now ( ) ) >= timestamp + self . _max_age : <EOL> del cache [ k ] <EOL> f . file_handle ( ) . truncate ( <NUM_LIT:0> ) <EOL> f . file_handle ( ) . seek ( <NUM_LIT:0> ) <EOL> json . dump ( cache , f . file_handle ( ) ) <EOL> else : <EOL> LOGGER . debug ( '<STR_LIT>' ) <EOL> except Exception as e : <EOL> LOGGER . warning ( e , exc_info = True ) <EOL> finally : <EOL> f . unlock_and_close ( ) <EOL> cache = Cache ( max_age = DISCOVERY_DOC_MAX_AGE ) </s>
