<s> """<STR_LIT>""" <EOL> import os as _os <EOL> import __builtin__ <EOL> import UserDict <EOL> _open = __builtin__ . open <EOL> _BLOCKSIZE = <NUM_LIT> <EOL> error = IOError <EOL> class _Database ( UserDict . DictMixin ) : <EOL> _os = _os <EOL> _open = _open <EOL> def __init__ ( self , filebasename , mode ) : <EOL> self . _mode = mode <EOL> self . _dirfile = filebasename + _os . extsep + '<STR_LIT>' <EOL> self . _datfile = filebasename + _os . extsep + '<STR_LIT>' <EOL> self . _bakfile = filebasename + _os . extsep + '<STR_LIT>' <EOL> self . _index = None <EOL> try : <EOL> f = _open ( self . _datfile , '<STR_LIT:r>' ) <EOL> except IOError : <EOL> f = _open ( self . _datfile , '<STR_LIT:w>' , self . _mode ) <EOL> f . close ( ) <EOL> self . _update ( ) <EOL> def _update ( self ) : <EOL> self . _index = { } <EOL> try : <EOL> f = _open ( self . _dirfile ) <EOL> except IOError : <EOL> pass <EOL> else : <EOL> for line in f : <EOL> line = line . rstrip ( ) <EOL> key , pos_and_siz_pair = eval ( line ) <EOL> self . _index [ key ] = pos_and_siz_pair <EOL> f . close ( ) <EOL> def _commit ( self ) : <EOL> if self . _index is None : <EOL> return <EOL> try : <EOL> self . _os . unlink ( self . _bakfile ) <EOL> except self . _os . error : <EOL> pass <EOL> try : <EOL> self . _os . rename ( self . _dirfile , self . _bakfile ) <EOL> except self . _os . error : <EOL> pass <EOL> f = self . _open ( self . _dirfile , '<STR_LIT:w>' , self . _mode ) <EOL> for key , pos_and_siz_pair in self . _index . iteritems ( ) : <EOL> f . write ( "<STR_LIT>" % ( key , pos_and_siz_pair ) ) <EOL> f . close ( ) <EOL> sync = _commit <EOL> def __getitem__ ( self , key ) : <EOL> pos , siz = self . _index [ key ] <EOL> f = _open ( self . _datfile , '<STR_LIT:rb>' ) <EOL> f . seek ( pos ) <EOL> dat = f . read ( siz ) <EOL> f . close ( ) <EOL> return dat <EOL> def _addval ( self , val ) : <EOL> f = _open ( self . _datfile , '<STR_LIT>' ) <EOL> f . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> pos = int ( f . tell ( ) ) <EOL> npos = ( ( pos + _BLOCKSIZE - <NUM_LIT:1> ) // _BLOCKSIZE ) * _BLOCKSIZE <EOL> f . write ( '<STR_LIT>' * ( npos - pos ) ) <EOL> pos = npos <EOL> f . write ( val ) <EOL> f . close ( ) <EOL> return ( pos , len ( val ) ) <EOL> def _setval ( self , pos , val ) : <EOL> f = _open ( self . _datfile , '<STR_LIT>' ) <EOL> f . seek ( pos ) <EOL> f . write ( val ) <EOL> f . close ( ) <EOL> return ( pos , len ( val ) ) <EOL> def _addkey ( self , key , pos_and_siz_pair ) : <EOL> self . _index [ key ] = pos_and_siz_pair <EOL> f = _open ( self . _dirfile , '<STR_LIT:a>' , self . _mode ) <EOL> f . write ( "<STR_LIT>" % ( key , pos_and_siz_pair ) ) <EOL> f . close ( ) <EOL> def __setitem__ ( self , key , val ) : <EOL> if not type ( key ) == type ( '<STR_LIT>' ) == type ( val ) : <EOL> raise TypeError , "<STR_LIT>" <EOL> if key not in self . _index : <EOL> self . _addkey ( key , self . _addval ( val ) ) <EOL> else : <EOL> pos , siz = self . _index [ key ] <EOL> oldblocks = ( siz + _BLOCKSIZE - <NUM_LIT:1> ) // _BLOCKSIZE <EOL> newblocks = ( len ( val ) + _BLOCKSIZE - <NUM_LIT:1> ) // _BLOCKSIZE <EOL> if newblocks <= oldblocks : <EOL> self . _index [ key ] = self . _setval ( pos , val ) <EOL> else : <EOL> self . _index [ key ] = self . _addval ( val ) <EOL> def __delitem__ ( self , key ) : <EOL> del self . _index [ key ] <EOL> self . _commit ( ) <EOL> def keys ( self ) : <EOL> return self . _index . keys ( ) <EOL> def has_key ( self , key ) : <EOL> return key in self . _index <EOL> def __contains__ ( self , key ) : <EOL> return key in self . _index <EOL> def iterkeys ( self ) : <EOL> return self . _index . iterkeys ( ) <EOL> __iter__ = iterkeys <EOL> def __len__ ( self ) : <EOL> return len ( self . _index ) <EOL> def close ( self ) : <EOL> self . _commit ( ) <EOL> self . _index = self . _datfile = self . _dirfile = self . _bakfile = None <EOL> __del__ = close <EOL> def open ( file , flag = None , mode = <NUM_LIT:0> <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> return _Database ( file , mode ) </s>
