# Path: cydra/util.py
# class SimpleCache(object):
#     """A simple in-memory cache
# 
#     This class does not do any locking. This means that keys should map to
#     relatively stable, immutable values"""
#     def __init__(self, lifetime=30, killtime=None, maxsize=100):
#         self.data = {}
#         self.lifetime = lifetime
#         self.maxsize = maxsize
# 
#         if killtime is None:
#             self.killtime = lifetime * 10
#         else:
#             self.killtime = killtime
# 
#     def set(self, key, value):
#         self.data[key] = SimpleCacheItem(value)
# 
#         if len(self.data) > self.maxsize:
#             self._remove_oldest()
# 
#     def cached(self, key, func):
#         self._remove_old()
#         if key in self.data:
#             return self.data[key].value
#         else:
#             res = func()
#             self.set(key, res)
#             return res
# 
#     def get(self, key, default=None):
#         self._remove_old()
#         item = self.data.get(key)
#         if item is not None:
#             return item.value
#         else:
#             return default
# 
#     def __contains__(self, key):
#         self._remove_old()
#         return key in self.data
# 
#     def _remove_old(self):
#         t = time.time()
# 
#         for key, item in self.data.items():
#             if t - item.creation > self.killtime or t - item.last_access > self.lifetime:
#                 del(self.data[key])
# 
#     def _remove_oldest(self):
#         mintime = time.time()
#         minkey = None
# 
#         for key, item in self.data.items():
#             if item.last_access < mintime:
#                 mintime = item.last_access
#                 minkey = key
# 
#         if minkey is not None:
#             del(self.data[minkey])


import urllib
import cydra
import logging
from cydra.util import SimpleCache

        Returns the permissions a user has on the object corresponding to the environment"""
        user = self.cydra.get_user(username=username)

        project, obj = self.environ_to_perm(environ)

        if isinstance(project, basestring):
            project = self.cydra.get_project(project)

            if project is None:
                return []

        return [perm.encode('utf-8') for (perm, value) in project.get_permissions(user, obj).items() if value == True]


def require_authorization(environ, start_response, realm='cydra', msg=''):
    start_response('401 Unauthorized', [('Content-Type', 'text/plain'), ('WWW-Authenticate', 'Basic realm="' + realm + '"')])
    return msg


def move_projectname_into_scriptname(environ, projectname):
    """Moves the project name from PATH_INFO to SCRIPT_NAME

    Example:

    /some/path/project/repository/more
     SCRIPT   | PATH INFO
    ->
     SCRIPT           |  PATH INFO"""


# Path: cydra/util.py
# class SimpleCache(object):
#     """A simple in-memory cache
# 
#     This class does not do any locking. This means that keys should map to
#     relatively stable, immutable values"""
#     def __init__(self, lifetime=30, killtime=None, maxsize=100):
#         self.data = {}
#         self.lifetime = lifetime
#         self.maxsize = maxsize
# 
#         if killtime is None:
#             self.killtime = lifetime * 10
#         else:
#             self.killtime = killtime
# 
#     def set(self, key, value):
#         self.data[key] = SimpleCacheItem(value)
# 
#         if len(self.data) > self.maxsize:
#             self._remove_oldest()
# 
#     def cached(self, key, func):
#         self._remove_old()
#         if key in self.data:
#             return self.data[key].value
#         else:
#             res = func()
#             self.set(key, res)
#             return res
# 
#     def get(self, key, default=None):
#         self._remove_old()
#         item = self.data.get(key)
#         if item is not None:
#             return item.value
#         else:
#             return default
# 
#     def __contains__(self, key):
#         self._remove_old()
#         return key in self.data
# 
#     def _remove_old(self):
#         t = time.time()
# 
#         for key, item in self.data.items():
#             if t - item.creation > self.killtime or t - item.last_access > self.lifetime:
#                 del(self.data[key])
# 
#     def _remove_oldest(self):
#         mintime = time.time()
#         minkey = None
# 
#         for key, item in self.data.items():
#             if item.last_access < mintime:
#                 mintime = item.last_access
#                 minkey = key
# 
#         if minkey is not None:
#             del(self.data[minkey])


# Path: cydra/web/wsgihelper.py
import urllib
import cydra
import logging
from cydra.util import SimpleCache


        Returns the permissions a user has on the object corresponding to the environment"""
        user = self.cydra.get_user(username=username)

        project, obj = self.environ_to_perm(environ)

        if isinstance(project, basestring):
            project = self.cydra.get_project(project)

            if project is None:
                return []

        return [perm.encode('utf-8') for (perm, value) in project.get_permissions(user, obj).items() if value == True]


def require_authorization(environ, start_response, realm='cydra', msg=''):
    start_response('401 Unauthorized', [('Content-Type', 'text/plain'), ('WWW-Authenticate', 'Basic realm="' + realm + '"')])
    return msg


def move_projectname_into_scriptname(environ, projectname):
    """Moves the project name from PATH_INFO to SCRIPT_NAME

    Example:

    /some/path/project/repository/more
     SCRIPT   | PATH INFO
    ->
     SCRIPT           |  PATH INFO"""


    environ['SCRIPT_NAME'] = environ.get('SCRIPT_NAME', '').rstrip('/') + '/' + projectname