<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> import shelve <EOL> from time import time as timestamp <EOL> from click import get_app_dir <EOL> logger = logging . getLogger ( __name__ ) <EOL> class Cache : <EOL> cache_dir = get_app_dir ( "<STR_LIT>" ) <EOL> cache_name = "<STR_LIT>" <EOL> def __init__ ( self , cache_file , cache , update_interval ) : <EOL> """<STR_LIT>""" <EOL> self . cache_file = cache_file <EOL> self . cache = cache <EOL> self . update_interval = update_interval <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> return self . close ( ) <EOL> @ classmethod <EOL> def from_file ( cls , file , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> cache = shelve . open ( file ) <EOL> return cls ( file , cache , * args , ** kwargs ) <EOL> except OSError as e : <EOL> logger . debug ( "<STR_LIT>" . format ( file ) ) <EOL> raise e <EOL> @ classmethod <EOL> def discover ( cls , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> file = os . path . join ( Cache . cache_dir , Cache . cache_name ) <EOL> return cls . from_file ( file , * args , ** kwargs ) <EOL> @ property <EOL> def last_updated ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . cache [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> return <NUM_LIT:0> <EOL> @ property <EOL> def is_valid ( self ) : <EOL> """<STR_LIT>""" <EOL> if timestamp ( ) - self . last_updated <= self . update_interval : <EOL> return True <EOL> else : <EOL> return False <EOL> def mark_updated ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_valid : <EOL> self . cache [ "<STR_LIT>" ] = timestamp ( ) <EOL> def is_cached ( self , url ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return True if url in self . cache else False <EOL> except TypeError : <EOL> return False <EOL> def last_modified ( self , url ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . cache [ url ] [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> return None <EOL> def add_tweets ( self , url , last_modified , tweets ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . cache [ url ] = { "<STR_LIT>" : last_modified , "<STR_LIT>" : tweets } <EOL> self . mark_updated ( ) <EOL> return True <EOL> except TypeError : <EOL> return False <EOL> def get_tweets ( self , url , limit = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> tweets = self . cache [ url ] [ "<STR_LIT>" ] <EOL> self . mark_updated ( ) <EOL> return sorted ( tweets , reverse = True ) [ : limit ] <EOL> except KeyError : <EOL> return [ ] <EOL> def remove_tweets ( self , url ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> del self . cache [ url ] <EOL> self . mark_updated ( ) <EOL> return True <EOL> except KeyError : <EOL> return False <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . cache . close ( ) <EOL> return True <EOL> except AttributeError : <EOL> return False <EOL> def sync ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . cache . sync ( ) <EOL> return True <EOL> except AttributeError : <EOL> return False </s>
