<s> import sqlite3 <EOL> class AchievementBackend ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _tracked = { } <EOL> def achievement_for_id ( self , tracked_id , achievement ) : <EOL> """<STR_LIT>""" <EOL> if tracked_id not in self . _tracked : <EOL> self . _tracked [ tracked_id ] = { } <EOL> if achievement . __name__ not in self . _tracked [ tracked_id ] : <EOL> self . _tracked [ tracked_id ] [ achievement . __name__ ] = achievement ( ) <EOL> return self . _tracked [ tracked_id ] [ achievement . __name__ ] <EOL> def achievements_for_id ( self , tracked_id , achievements ) : <EOL> """<STR_LIT>""" <EOL> r = [ ] <EOL> for a in achievements : <EOL> r . append ( self . achievement_for_id ( tracked_id , a ) ) <EOL> return r <EOL> def set_level_for_id ( self , tracked_id , achievement , level ) : <EOL> """<STR_LIT>""" <EOL> if tracked_id not in self . _tracked : <EOL> self . _tracked [ tracked_id ] = { } <EOL> if achievement . __name__ not in self . _tracked [ tracked_id ] : <EOL> self . _tracked [ tracked_id ] [ achievement . __name__ ] = achievement ( current = level ) <EOL> self . _tracked [ tracked_id ] [ achievement . __name__ ] . set_level ( level ) <EOL> def get_tracked_ids ( self ) : <EOL> return self . _tracked . keys ( ) <EOL> def remove_id ( self , tracked_id ) : <EOL> """<STR_LIT>""" <EOL> if tracked_id in self . _tracked : <EOL> del self . _tracked [ tracked_id ] <EOL> class SQLiteAchievementBackend ( AchievementBackend ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dbfile ) : <EOL> self . conn = sqlite3 . connect ( dbfile ) <EOL> with self . conn : <EOL> c = self . conn . cursor ( ) <EOL> c . execute ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def achievement_for_id ( self , tracked_id , achievement ) : <EOL> with self . conn : <EOL> c = self . conn . cursor ( ) <EOL> c . execute ( '<STR_LIT>' , <EOL> ( achievement . __name__ , str ( tracked_id ) ) ) <EOL> rows = c . fetchall ( ) <EOL> if not rows : <EOL> c . execute ( '<STR_LIT>' , <EOL> ( str ( tracked_id ) , achievement . __name__ , <NUM_LIT:0> ) ) <EOL> return achievement ( current = <NUM_LIT:0> ) <EOL> return achievement ( current = rows [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> def achievements_for_id ( self , tracked_id , achievements ) : <EOL> r = [ ] <EOL> achievements = dict ( ( _ . __name__ , _ ) for _ in achievements ) <EOL> with self . conn : <EOL> c = self . conn . cursor ( ) <EOL> c . execute ( '<STR_LIT>' <EOL> '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( '<STR_LIT:?>' * len ( achievements . keys ( ) ) ) , <EOL> [ str ( tracked_id ) ] + list ( achievements . keys ( ) ) ) <EOL> rows = c . fetchall ( ) <EOL> for i , _ in enumerate ( rows ) : <EOL> r . append ( achievements [ _ [ <NUM_LIT:0> ] ] ( current = _ [ <NUM_LIT:1> ] ) ) <EOL> return r <EOL> def set_level_for_id ( self , tracked_id , achievement , level ) : <EOL> with self . conn : <EOL> c = self . conn . cursor ( ) <EOL> c . execute ( '<STR_LIT>' , <EOL> ( level , achievement . __name__ , str ( tracked_id ) ) ) <EOL> def get_tracked_ids ( self ) : <EOL> with self . conn : <EOL> c = self . conn . cursor ( ) <EOL> c . execute ( '<STR_LIT>' ) <EOL> rows = c . fetchall ( ) <EOL> return [ _ [ <NUM_LIT:0> ] for _ in rows ] <EOL> def remove_id ( self , tracked_id ) : <EOL> with self . conn : <EOL> c = self . conn . cursor ( ) <EOL> c . execute ( '<STR_LIT>' , ( str ( tracked_id ) , ) ) </s>
