# 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)))
# 
# 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/models/gamelist.py
# def get_game_by_slug(slug, reload_game=False):
#     return GameList.get_instance().get_by_slug(slug, reload_game)
# 
# Path: turbulenz_local/lib/tools.py
# def create_id():
#     #id needs to be of 12-bytes length
#     string_id = ''
#     for _ in range(12):
#         string_id += '%02x' % randint(0, 255)
#     return string_id
# 
# Path: turbulenz_local/lib/exceptions.py
# class InvalidGameSession(ApiException):
#     def __init__(self, value):
#         ApiException.__init__(self, value, '403 Forbidden', {'invalidGameSession': True})


import logging
import yaml
from os.path import exists
from turbulenz_local.tools import get_absolute_path
from turbulenz_local.models.userlist import get_user
from turbulenz_local.models.gamelist import get_game_by_slug
from turbulenz_local.lib.tools import create_id
from turbulenz_local.lib.exceptions import InvalidGameSession
from threading import Lock
from time import time
from pylons import config
            gamesession_id = create_id()
        self.gamesession_id = gamesession_id

        if created is None:
            created = int(time())
        self.created = created


    @classmethod
    def from_dict(cls, gamesession):
        game = get_game_by_slug(gamesession['game'])
        # remove any sessions pointing at old games / users
        if game:
            return GameSession(game,
                               get_user(gamesession['user']),
                               gamesession.get('gameSessionId', None),
                               gamesession.get('created', None))
        else:
            raise InvalidGameSession('No gamesession with that id')


    def to_dict(self):
        try:
            return {
                'gameSessionId': self.gamesession_id,
                'user': self.user.username,
                'game': str(self.game.slug),
                'created': self.created
            }
        except AttributeError:

# 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)))
# 
# 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/models/gamelist.py
# def get_game_by_slug(slug, reload_game=False):
#     return GameList.get_instance().get_by_slug(slug, reload_game)
# 
# Path: turbulenz_local/lib/tools.py
# def create_id():
#     #id needs to be of 12-bytes length
#     string_id = ''
#     for _ in range(12):
#         string_id += '%02x' % randint(0, 255)
#     return string_id
# 
# Path: turbulenz_local/lib/exceptions.py
# class InvalidGameSession(ApiException):
#     def __init__(self, value):
#         ApiException.__init__(self, value, '403 Forbidden', {'invalidGameSession': True})


# Path: turbulenz_local/models/gamesessionlist.py
import logging
import yaml
from os.path import exists
from turbulenz_local.tools import get_absolute_path
from turbulenz_local.models.userlist import get_user
from turbulenz_local.models.gamelist import get_game_by_slug
from turbulenz_local.lib.tools import create_id
from turbulenz_local.lib.exceptions import InvalidGameSession
from threading import Lock
from time import time
from pylons import config

            gamesession_id = create_id()
        self.gamesession_id = gamesession_id

        if created is None:
            created = int(time())
        self.created = created


    @classmethod
    def from_dict(cls, gamesession):
        game = get_game_by_slug(gamesession['game'])
        # remove any sessions pointing at old games / users
        if game:
            return GameSession(game,
                               get_user(gamesession['user']),
                               gamesession.get('gameSessionId', None),
                               gamesession.get('created', None))
        else:
            raise InvalidGameSession('No gamesession with that id')


    def to_dict(self):
        try:
            return {
                'gameSessionId': self.gamesession_id,
                'user': self.user.username,
                'game': str(self.game.slug),
                'created': self.created
            }
        except AttributeError:

            raise InvalidGameSession()