<s> import json , os , threading , time , copy , traceback <EOL> class Storage : <EOL> def __init__ ( self , name , isWorld = None , root = "<STR_LIT>" ) : <EOL> self . name = name <EOL> self . root = root <EOL> self . data = { } <EOL> self . dataOld = { } <EOL> self . load ( ) <EOL> self . abort = False <EOL> self . time = time . time ( ) <EOL> t = threading . Thread ( target = self . periodicSave , args = ( ) ) <EOL> t . daemon = True <EOL> t . start ( ) <EOL> def __del__ ( self ) : <EOL> self . abort = True <EOL> self . save ( ) <EOL> def __getitem__ ( self , index ) : <EOL> if not type ( index ) == str : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> return self . data [ index ] <EOL> def __setitem__ ( self , index , value ) : <EOL> if not type ( index ) == str : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> self . data [ index ] = value <EOL> return self . data [ index ] <EOL> def __delattr__ ( self , index ) : <EOL> if not type ( index ) == str : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> del self . data [ index ] <EOL> def __iter__ ( self ) : <EOL> for i in self . data : <EOL> yield i <EOL> def periodicSave ( self ) : <EOL> while not self . abort : <EOL> if time . time ( ) - self . time > <NUM_LIT:10> : <EOL> if not self . data == self . dataOld : <EOL> try : <EOL> self . save ( ) <EOL> except : <EOL> print traceback . format_exc ( ) <EOL> self . time = time . time ( ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> def load ( self ) : <EOL> l = "<STR_LIT>" <EOL> for i in self . root . split ( "<STR_LIT:/>" ) : <EOL> l += i + "<STR_LIT:/>" <EOL> if not os . path . exists ( l ) : <EOL> try : os . mkdir ( l ) <EOL> except : pass <EOL> if not os . path . exists ( "<STR_LIT>" % ( self . root , self . name ) ) : <EOL> self . save ( ) <EOL> with open ( "<STR_LIT>" % ( self . root , self . name ) , "<STR_LIT:r>" ) as f : <EOL> self . data = json . loads ( f . read ( ) ) <EOL> self . dataOld = copy . deepcopy ( self . data ) <EOL> def save ( self ) : <EOL> with open ( "<STR_LIT>" % ( self . root , self . name ) , "<STR_LIT:w>" ) as f : <EOL> f . write ( json . dumps ( self . data ) ) <EOL> self . flush = False <EOL> def key ( self , key , value = None ) : <EOL> if value == None : <EOL> return self . getKey ( key ) <EOL> else : <EOL> self . setKey ( key , value ) <EOL> def getKey ( self , key ) : <EOL> if key in self . data : <EOL> return self . data [ key ] <EOL> else : <EOL> return None <EOL> def setKey ( self , key , value = None ) : <EOL> if value == None : <EOL> if key in self . data : del self . data [ key ] <EOL> else : <EOL> self . data [ key ] = value <EOL> self . flush = True </s>
