<s> from logging import getLogger <EOL> from pylons import config <EOL> import yaml <EOL> from os import remove , listdir , rmdir <EOL> from os . path import join as join_path , exists as path_exists <EOL> from threading import Lock <EOL> from turbulenz_local . models . userlist import get_user <EOL> from turbulenz_local . tools import get_absolute_path , create_dir <EOL> LOG = getLogger ( __name__ ) <EOL> class GameProfileError ( Exception ) : <EOL> pass <EOL> class GameProfile ( object ) : <EOL> def __init__ ( self , user , game ) : <EOL> self . lock = Lock ( ) <EOL> self . game = game <EOL> self . user = user <EOL> try : <EOL> path = config [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> LOG . error ( '<STR_LIT>' ) <EOL> return <EOL> path = join_path ( path , game . slug ) <EOL> if not create_dir ( path ) : <EOL> error_msg = '<STR_LIT>' % path <EOL> LOG . error ( error_msg ) <EOL> raise GameProfileError ( error_msg ) <EOL> self . path = get_absolute_path ( path ) <EOL> self . defaults = { } <EOL> default_yaml_path = unicode ( get_absolute_path ( join_path ( game . path , '<STR_LIT>' ) ) ) <EOL> if path_exists ( default_yaml_path ) : <EOL> with open ( default_yaml_path , '<STR_LIT:r>' ) as f : <EOL> try : <EOL> file_defaults = yaml . load ( f ) <EOL> self . defaults = dict ( ( v [ '<STR_LIT:user>' ] , v [ '<STR_LIT:value>' ] ) for v in file_defaults [ '<STR_LIT>' ] ) <EOL> except ( yaml . YAMLError , KeyError , TypeError ) as e : <EOL> LOG . error ( '<STR_LIT>' , str ( e ) ) <EOL> def get ( self , usernames ) : <EOL> path = self . path <EOL> game_profiles = { } <EOL> with self . lock : <EOL> for username in usernames : <EOL> profile_path = join_path ( path , username + '<STR_LIT>' ) <EOL> try : <EOL> with open ( unicode ( profile_path ) , '<STR_LIT:r>' ) as fin : <EOL> value = fin . read ( ) <EOL> except IOError : <EOL> if username in self . defaults : <EOL> value = self . defaults [ username ] <EOL> else : <EOL> continue <EOL> game_profiles [ username ] = { '<STR_LIT:value>' : value } <EOL> return { '<STR_LIT>' : game_profiles } <EOL> def set ( self , value ) : <EOL> profile_path = join_path ( self . path , self . user . username + '<STR_LIT>' ) <EOL> with self . lock : <EOL> try : <EOL> with open ( unicode ( profile_path ) , '<STR_LIT:w>' ) as fout : <EOL> fout . write ( value ) <EOL> except IOError as e : <EOL> error_msg = '<STR_LIT>' % str ( e ) <EOL> LOG . error ( error_msg ) <EOL> raise GameProfileError ( error_msg ) <EOL> return True <EOL> def remove ( self ) : <EOL> profile_path = join_path ( self . path , self . user . username + '<STR_LIT>' ) <EOL> with self . lock : <EOL> try : <EOL> if path_exists ( profile_path ) : <EOL> remove ( profile_path ) <EOL> except IOError as e : <EOL> error_msg = '<STR_LIT>' % str ( e ) <EOL> LOG . error ( error_msg ) <EOL> raise GameProfileError ( error_msg ) <EOL> return True <EOL> @ classmethod <EOL> def remove_all ( cls , game ) : <EOL> try : <EOL> path = join_path ( config [ '<STR_LIT>' ] , game . slug ) <EOL> except KeyError : <EOL> LOG . error ( '<STR_LIT>' ) <EOL> return <EOL> for f in listdir ( path ) : <EOL> split_ext = f . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> if split_ext [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> GameProfile ( get_user ( split_ext [ <NUM_LIT:0> ] ) , game ) . remove ( ) <EOL> try : <EOL> rmdir ( path ) <EOL> except OSError : <EOL> pass </s>
