# Path: turbulenz_local/lib/exceptions.py
# class ApiException(BaseException):
#     def __init__(self, value, status='500 Internal Server Error', json_data=None):
#         super(ApiException, self).__init__()
#         self.value = value
#         self.status = status
#         self.json_data = json_data
# 
#     def __str__(self):
#         return self.value
# 
# 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)


import logging
import yaml
from collections import defaultdict
from os import access, R_OK, remove as remove_file, listdir
from os.path import join as join_path, normpath as norm_path
from threading import Lock
from time import time as get_time
from simplejson import JSONEncoder, JSONDecoder
from turbulenz_local.lib.exceptions import ApiException
from yaml import YAMLError
from pylons import config
from turbulenz_local.tools import get_absolute_path, create_dir
        return get_absolute_path(join_path(path, filename))
    else:
        return path



def _load_tasks(slug, recipient, notification_type):
    tasks = []
    num_tasks_per_sender = defaultdict(lambda: 0)
    task_ids = set()

    task_path = _get_task_path(slug, recipient, notification_type)
    for task_file in listdir(task_path):
        file_path = join_path(task_path, task_file)
        try:
            with open(file_path, 'rb') as f:
                json_dict = _json_decoder.decode(f.read())
                task = GameNotificationTask(**json_dict)
                task_ids.add(task.task_id)
                tasks.append(task)
                num_tasks_per_sender[task.sender] += 1
        except (IOError, OSError, TypeError) as e:
            LOG.error('Failed loading GameNotificationTask "%s": %s', file_path, str(e))

    tasks.sort(key=lambda task: task.time)

    return tasks, task_ids, num_tasks_per_sender




# Path: turbulenz_local/lib/exceptions.py
# class ApiException(BaseException):
#     def __init__(self, value, status='500 Internal Server Error', json_data=None):
#         super(ApiException, self).__init__()
#         self.value = value
#         self.status = status
#         self.json_data = json_data
# 
#     def __str__(self):
#         return self.value
# 
# 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/gamenotifications.py
import logging
import yaml
from collections import defaultdict
from os import access, R_OK, remove as remove_file, listdir
from os.path import join as join_path, normpath as norm_path
from threading import Lock
from time import time as get_time
from simplejson import JSONEncoder, JSONDecoder
from turbulenz_local.lib.exceptions import ApiException
from yaml import YAMLError
from pylons import config
from turbulenz_local.tools import get_absolute_path, create_dir

        return get_absolute_path(join_path(path, filename))
    else:
        return path



def _load_tasks(slug, recipient, notification_type):
    tasks = []
    num_tasks_per_sender = defaultdict(lambda: 0)
    task_ids = set()

    task_path = _get_task_path(slug, recipient, notification_type)
    for task_file in listdir(task_path):
        file_path = join_path(task_path, task_file)
        try:
            with open(file_path, 'rb') as f:
                json_dict = _json_decoder.decode(f.read())
                task = GameNotificationTask(**json_dict)
                task_ids.add(task.task_id)
                tasks.append(task)
                num_tasks_per_sender[task.sender] += 1
        except (IOError, OSError, TypeError) as e:
            LOG.error('Failed loading GameNotificationTask "%s": %s', file_path, str(e))

    tasks.sort(key=lambda task: task.time)

    return tasks, task_ids, num_tasks_per_sender




class GameNotificationTask(object):