<s> """<STR_LIT>""" <EOL> from __future__ import generators <EOL> import os , sys , stat , sha , string , urllib , codecs <EOL> __author__ = ( '<STR_LIT>' ) <EOL> __revision__ = "<STR_LIT>" <EOL> __version__ = "<STR_LIT>" <EOL> __all__ = [ '<STR_LIT>' ] <EOL> try : <EOL> import cPickle as pickle <EOL> except ImportError : <EOL> import pickle <EOL> INTP_VER = sys . version_info [ : <NUM_LIT:2> ] <EOL> if INTP_VER < ( <NUM_LIT:2> , <NUM_LIT:3> ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> class Cache ( dict ) : <EOL> def __init__ ( self , path ) : <EOL> self . __path = path <EOL> dict . __init__ ( self ) <EOL> def key2path ( self , key ) : <EOL> """<STR_LIT>""" <EOL> raw = sha . new ( key ) <EOL> digest = raw . hexdigest ( ) <EOL> steps = [ digest [ i : i + <NUM_LIT:2> ] for i in range ( <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:2> ) ] <EOL> steps . append ( digest [ <NUM_LIT:4> : ] ) <EOL> return os . path . join ( self . __path , string . join ( steps , os . path . sep ) ) <EOL> def __eq__ ( self , other ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __ne__ ( self , other ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __lt__ ( self , other ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __le__ ( self , other ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __gt__ ( self , other ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __ge__ ( self , other ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def __repr__ ( self , other ) : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> def expire ( self , when ) : <EOL> """<STR_LIT>""" <EOL> for folder , subfolders , files in os . walk ( self . __path ) : <EOL> for f in files : <EOL> mtime = os . stat ( os . path . join ( folder , f ) ) [ stat . ST_MTIME ] <EOL> if mtime < when : <EOL> os . remove ( os . path . join ( folder , f ) ) <EOL> os . rmdir ( folder ) <EOL> def keys ( self ) : <EOL> """<STR_LIT>""" <EOL> for folder , subfolders , files in os . walk ( self . __path ) : <EOL> for f in files : <EOL> yield urllib . unquote ( f ) <EOL> def __setitem__ ( self , key , val ) : <EOL> """<STR_LIT>""" <EOL> path = self . key2path ( key ) <EOL> quote = urllib . quote ( key , '<STR_LIT>' ) <EOL> if ( not os . path . exists ( os . path . join ( path , quote ) ) ) : <EOL> os . makedirs ( path ) <EOL> open ( os . path . join ( path , quote ) , '<STR_LIT:wb>' ) . write ( pickle . dumps ( val ) ) <EOL> def __delitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> path = self . key2path ( key ) <EOL> quote = urllib . quote ( key , '<STR_LIT>' ) <EOL> if os . path . exists ( path ) : <EOL> os . remove ( os . path . join ( path , quote ) ) <EOL> os . rmdir ( path ) <EOL> else : <EOL> raise KeyError <EOL> def __getitem__ ( self , key ) : <EOL> """<STR_LIT>""" <EOL> path = self . key2path ( key ) <EOL> quote = urllib . quote ( key , '<STR_LIT>' ) <EOL> try : <EOL> return pickle . loads ( open ( os . path . join ( path , quote ) , "<STR_LIT:rb>" ) . read ( ) ) <EOL> except : <EOL> raise KeyError <EOL> def mtime ( self , key ) : <EOL> """<STR_LIT>""" <EOL> return self . stats ( key ) [ stat . ST_MTIME ] <EOL> def stats ( self , key ) : <EOL> """<STR_LIT>""" <EOL> path = self . key2path ( key ) <EOL> quote = urllib . quote ( key , '<STR_LIT>' ) <EOL> try : <EOL> return os . stat ( os . path . join ( path , quote ) ) <EOL> except : <EOL> raise KeyError <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> c = Cache ( '<STR_LIT>' ) <EOL> c [ '<STR_LIT:foo>' ] = '<STR_LIT:bar>' <EOL> print c [ '<STR_LIT:foo>' ] <EOL> del c [ '<STR_LIT:foo>' ] <EOL> c [ '<STR_LIT>' ] = "<STR_LIT:test>" <EOL> for i in c . keys ( ) : <EOL> print i </s>
