# Path: turbulenz_local/models/userlist.py
# def get_user(self, username):
#     username_lower = username.lower()
#     with self.lock:
#         try:
#             return self.users[username_lower]
#         except KeyError:
#             LOG.info('No user with username "%s" adding user with defaults', username)
#             try:
#                 user = self._add_user(username_lower)
#                 self._write_users()
#                 return user
#             except ValueError as e:
#                 raise BadRequest(str(e))
# 
# Path: turbulenz_local/tools.py
# def get_absolute_path(directory):
#     """
#     Return absolute path by working out path relative to games root
#     """
#     if isabs(directory):
#         return directory
# 
#     return realpath(normpath(join(config['games_root'], directory)))
# 
# def create_dir(directory):
#     """
#     Create the directory structure if necessary. return 'True' if it was created
#     successfully and can be written into, 'False' otherwise.
#     """
#     if directory.strip() == '':
#         return False
#     else:
#         absDir = get_absolute_path(directory)
#         try:
#             os.makedirs(absDir)
#         except OSError as e:
#             if e.errno != EEXIST:
#                 LOG.error('Failed creating meta dir: %s', str(e))
#                 return False
#         return os.access(absDir, os.W_OK)


from logging import getLogger
from pylons import config
from os import remove, listdir, rmdir
from os.path import join as join_path, exists as path_exists
from threading import Lock
from turbulenz_local.models.userlist import get_user
from turbulenz_local.tools import get_absolute_path, create_dir
import yaml

LOG = getLogger(__name__)


class GameProfileError(Exception):
    pass


class GameProfile(object):

    def __init__(self, user, game):
        self.lock = Lock()
        self.game = game
        self.user = user

        try:
            path = config['gameprofile_db']
        except KeyError:
            LOG.error('gameprofile_db path config variable not set')
            return

        # Create gameprofile folder and user folder on the game path
        path = join_path(path, game.slug)
        if not create_dir(path):
            error_msg = 'User GameProfile path \"%s\" could not be created.' % path
            LOG.error(error_msg)
            raise GameProfileError(error_msg)
        self.path = get_absolute_path(path)

        self.defaults = {}

# Path: turbulenz_local/models/userlist.py
# def get_user(self, username):
#     username_lower = username.lower()
#     with self.lock:
#         try:
#             return self.users[username_lower]
#         except KeyError:
#             LOG.info('No user with username "%s" adding user with defaults', username)
#             try:
#                 user = self._add_user(username_lower)
#                 self._write_users()
#                 return user
#             except ValueError as e:
#                 raise BadRequest(str(e))
# 
# Path: turbulenz_local/tools.py
# def get_absolute_path(directory):
#     """
#     Return absolute path by working out path relative to games root
#     """
#     if isabs(directory):
#         return directory
# 
#     return realpath(normpath(join(config['games_root'], directory)))
# 
# def create_dir(directory):
#     """
#     Create the directory structure if necessary. return 'True' if it was created
#     successfully and can be written into, 'False' otherwise.
#     """
#     if directory.strip() == '':
#         return False
#     else:
#         absDir = get_absolute_path(directory)
#         try:
#             os.makedirs(absDir)
#         except OSError as e:
#             if e.errno != EEXIST:
#                 LOG.error('Failed creating meta dir: %s', str(e))
#                 return False
#         return os.access(absDir, os.W_OK)


# Path: turbulenz_local/models/apiv1/gameprofile.py
from logging import getLogger
from pylons import config
from os import remove, listdir, rmdir
from os.path import join as join_path, exists as path_exists
from threading import Lock
from turbulenz_local.models.userlist import get_user
from turbulenz_local.tools import get_absolute_path, create_dir
import yaml


LOG = getLogger(__name__)


class GameProfileError(Exception):
    pass


class GameProfile(object):

    def __init__(self, user, game):
        self.lock = Lock()
        self.game = game
        self.user = user

        try:
            path = config['gameprofile_db']
        except KeyError:
            LOG.error('gameprofile_db path config variable not set')
            return

        # Create gameprofile folder and user folder on the game path
        path = join_path(path, game.slug)
        if not create_dir(path):
            error_msg = 'User GameProfile path \"%s\" could not be created.' % path
            LOG.error(error_msg)
            raise GameProfileError(error_msg)
        self.path = get_absolute_path(path)

        self.defaults = {}

        default_yaml_path = unicode(get_absolute_path(join_path(game.path, 'defaultgameprofiles.yaml')))