# 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/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 BadRequest(ApiException):
#     def __init__(self, value, json_data=None):
#         ApiException.__init__(self, value, '400 Bad Request', json_data)
# 
# class NotFound(ApiException):
#     def __init__(self, value, json_data=None):
#         ApiException.__init__(self, value, '404 Not Found', json_data)
# 
# class Forbidden(ApiException):
#     def __init__(self, value, json_data=None):
#         ApiException.__init__(self, value, '403 Forbidden', json_data)


import logging
import yaml
from re import compile as regex_compile
from time import time as time
from os import listdir, remove as remove_file
from os.path import join as join_path, splitext, exists as path_exists
from threading import Lock
from pylons import config
from turbulenz_local.tools import get_absolute_path, create_dir
from turbulenz_local.lib.tools import create_id
from turbulenz_local.lib.exceptions import BadRequest, NotFound, Forbidden
            key = str(key)
            if not self.validate_key.match(key):
                raise BadRequest('Key can only contain alphanumeric characters hyphens and dots')

            if key in self.store:
                key_store = self.store[key]

                if key_store['access'] != self.read_only:
                    raise Forbidden('Forbidden: Key "%s" is read and write access'
                                    '(must use compare and set for read and write keys)' % key,
                                    {'reason': 'read_and_write'})
                owner = key_store['ownedBy']
                if owner != user.username:
                    raise Forbidden('Forbidden: Key "%s" is read only' % key, {'reason': 'read_only'})
            else:
                owner = user.username
            return self._set(key, value, owner, self.read_only)

    def compare_and_set(self, user, key, value, token):
        with self.lock:
            self.datashare_access(user)

            key = str(key)
            if not self.validate_key.match(key):
                raise BadRequest('Key can only contain alphanumeric characters hyphens and dots')

            if key in self.store:
                key_store = self.store[key]

                if key_store['access'] != self.read_and_write:

# 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/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 BadRequest(ApiException):
#     def __init__(self, value, json_data=None):
#         ApiException.__init__(self, value, '400 Bad Request', json_data)
# 
# class NotFound(ApiException):
#     def __init__(self, value, json_data=None):
#         ApiException.__init__(self, value, '404 Not Found', json_data)
# 
# class Forbidden(ApiException):
#     def __init__(self, value, json_data=None):
#         ApiException.__init__(self, value, '403 Forbidden', json_data)


# Path: turbulenz_local/models/apiv1/datashare.py
import logging
import yaml
from re import compile as regex_compile
from time import time as time
from os import listdir, remove as remove_file
from os.path import join as join_path, splitext, exists as path_exists
from threading import Lock
from pylons import config
from turbulenz_local.tools import get_absolute_path, create_dir
from turbulenz_local.lib.tools import create_id
from turbulenz_local.lib.exceptions import BadRequest, NotFound, Forbidden

            key = str(key)
            if not self.validate_key.match(key):
                raise BadRequest('Key can only contain alphanumeric characters hyphens and dots')

            if key in self.store:
                key_store = self.store[key]

                if key_store['access'] != self.read_only:
                    raise Forbidden('Forbidden: Key "%s" is read and write access'
                                    '(must use compare and set for read and write keys)' % key,
                                    {'reason': 'read_and_write'})
                owner = key_store['ownedBy']
                if owner != user.username:
                    raise Forbidden('Forbidden: Key "%s" is read only' % key, {'reason': 'read_only'})
            else:
                owner = user.username
            return self._set(key, value, owner, self.read_only)

    def compare_and_set(self, user, key, value, token):
        with self.lock:
            self.datashare_access(user)

            key = str(key)
            if not self.validate_key.match(key):
                raise BadRequest('Key can only contain alphanumeric characters hyphens and dots')

            if key in self.store:
                key_store = self.store[key]

                if key_store['access'] != self.read_and_write:

                    raise Forbidden('Forbidden: Key "%s" is read only access (must use set for read only keys)' % key,