<s> import hashlib <EOL> import os <EOL> from django . conf import settings <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . utils . encoding import force_text <EOL> class Cache ( object ) : <EOL> """<STR_LIT>""" <EOL> def exists ( self , name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def size ( self , name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def modified_time ( self , name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def save ( self , name , size , mtime ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def remove ( self , name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> class FileSystemCache ( Cache ) : <EOL> def __init__ ( self , cache_dir = None ) : <EOL> if cache_dir is None : <EOL> cache_dir = getattr ( settings , '<STR_LIT>' , None ) <EOL> if cache_dir is None : <EOL> raise ImproperlyConfigured ( <EOL> '<STR_LIT>' % type ( self ) <EOL> ) <EOL> self . cache_dir = cache_dir <EOL> def _path ( self , name ) : <EOL> name = force_text ( name ) . encode ( '<STR_LIT:utf-8>' ) <EOL> return os . path . join ( self . cache_dir , hashlib . md5 ( name ) . hexdigest ( ) ) <EOL> def exists ( self , name ) : <EOL> return None <EOL> def size ( self , name ) : <EOL> try : <EOL> file = open ( self . _path ( name ) ) <EOL> size = int ( file . readlines ( ) [ <NUM_LIT:1> ] ) <EOL> file . close ( ) <EOL> except : <EOL> size = None <EOL> return size <EOL> def modified_time ( self , name ) : <EOL> try : <EOL> file = open ( self . _path ( name ) ) <EOL> mtime = float ( file . readlines ( ) [ <NUM_LIT:2> ] ) <EOL> file . close ( ) <EOL> except : <EOL> mtime = None <EOL> return mtime <EOL> def save ( self , name , size , mtime ) : <EOL> if not os . path . exists ( self . cache_dir ) : <EOL> os . makedirs ( self . cache_dir ) <EOL> file = open ( self . _path ( name ) , '<STR_LIT:wb>' ) <EOL> file . write ( ( '<STR_LIT>' % ( name , size , mtime ) ) . encode ( '<STR_LIT:utf-8>' ) ) <EOL> file . close ( ) <EOL> def remove ( self , name ) : <EOL> name = self . _path ( name ) <EOL> if os . path . exists ( name ) : <EOL> os . remove ( name ) </s>
