<s> """<STR_LIT>""" <EOL> import anydbm <EOL> import thread <EOL> class BaseDB : <EOL> def __init__ ( self , filename , type ) : <EOL> self . type = type <EOL> self . filename = filename <EOL> if self . filename : <EOL> self . db = None <EOL> else : <EOL> self . db = { } <EOL> self . lock = thread . allocate_lock ( ) <EOL> def create ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . filename : <EOL> self . db = anydbm . open ( self . filename , "<STR_LIT:n>" ) <EOL> self . db [ "<STR_LIT>" ] = self . type <EOL> self . db . sync ( ) <EOL> else : <EOL> self . db = { } <EOL> def open ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . filename : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . db = anydbm . open ( self . filename , "<STR_LIT:w>" ) <EOL> try : <EOL> if self . db [ "<STR_LIT>" ] != self . type : <EOL> raise ValueError ( "<STR_LIT>" % self . type ) <EOL> except KeyError : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def __getitem__ ( self , username ) : <EOL> if self . db == None : <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> self . lock . acquire ( ) <EOL> try : <EOL> valueStr = self . db [ username ] <EOL> finally : <EOL> self . lock . release ( ) <EOL> return self . _getItem ( username , valueStr ) <EOL> def __setitem__ ( self , username , value ) : <EOL> if self . db == None : <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> valueStr = self . _setItem ( username , value ) <EOL> self . lock . acquire ( ) <EOL> try : <EOL> self . db [ username ] = valueStr <EOL> if self . filename : <EOL> self . db . sync ( ) <EOL> finally : <EOL> self . lock . release ( ) <EOL> def __delitem__ ( self , username ) : <EOL> if self . db == None : <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> self . lock . acquire ( ) <EOL> try : <EOL> del ( self . db [ username ] ) <EOL> if self . filename : <EOL> self . db . sync ( ) <EOL> finally : <EOL> self . lock . release ( ) <EOL> def __contains__ ( self , username ) : <EOL> """<STR_LIT>""" <EOL> if self . db == None : <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> self . lock . acquire ( ) <EOL> try : <EOL> return self . db . has_key ( username ) <EOL> finally : <EOL> self . lock . release ( ) <EOL> def check ( self , username , param ) : <EOL> value = self . __getitem__ ( username ) <EOL> return self . _checkItem ( value , username , param ) <EOL> def keys ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . db == None : <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> self . lock . acquire ( ) <EOL> try : <EOL> usernames = self . db . keys ( ) <EOL> finally : <EOL> self . lock . release ( ) <EOL> usernames = [ u for u in usernames if not u . startswith ( "<STR_LIT>" ) ] <EOL> return usernames </s>
