<commit_before># -*- coding: utf-8 -*-

# <NAME>
# Summer 2014
# Univerity of Colorado

import copy
import os
import uuid
import time
import mimetypes

import redis
import werkzeug

import environment
import tester_script

_ENCODING = 'utf-8'

_SUF_BASE = 'Base'

_BASE_SCHEMA           = ['created_time', 'modified_time']

_USER_SCHEMA           = ['username', 'first', 'last', 'type']
_GROUP_SCHEMA          = ['name', 'members']

_COL_PERMISSION_SCHEMA = ['create', 'list']
_OBJ_PERMISSION_SCHEMA = ['read', 'write', 'delete']

_ASSIGNMENTS_SCHEMA    = ['name', 'contact', 'permissions']
_TESTS_SCHEMA          = ['name', 'contact', 'type', 'maxscore']
_SUBMISSIONS_SCHEMA    = ['author']
_RUNS_SCHEMA           = ['test', 'status', 'score', 'output']
_FILES_SCHEMA          = ['key', 'name', 'type', 'encoding', 'path']

_FILES_DIR = "./files/"

_UUID_GLOB = '????????-????-????-????-????????????'

### Exceptions

class DatatypesError(Exception):
    """Base class for Datatypes Exceptions"""

    def __init__(self, *args, **kwargs):
        super(DatatypesError, self).__init__(*args, **kwargs)

class UUIDRedisFactoryError(DatatypesError):
    """Base class for UUID Redis Object Exceptions"""

    def __init__(self, *args, **kwargs):
        super(UUIDRedisFactoryError, self).__init__(*args, **kwargs)

class UUIDRedisObjectError(DatatypesError):
    """Base class for UUID Redis Object Exceptions"""

    def __init__(self, *args, **kwargs):
        super(UUIDRedisObjectError, self).__init__(*args, **kwargs)

class UUIDRedisObjectDNE(UUIDRedisObjectError):
    """UUID Redis Object Does Not Exist"""

    def __init__(self, obj):
        msg = "{:s} does not exist.".format(obj)
        super(UUIDRedisObjectDNE, self).__init__(msg)


### Objects

class UUIDObject(object):

    def __init__(self, uuid_obj):
        """Base Constructor"""
        super(UUIDObject, self).__init__()
        self.uuid = uuid_obj

    @classmethod
    def from_new(cls):
        """New Constructor"""

        # Create New Object
        uuid_obj = uuid.uuid4()
        obj = cls(uuid_obj)

        # Return Object
        return obj

    @classmethod
    def from_existing(cls, uuid_hex):
        """Existing Constructor"""

        # Create Existing Object
        uuid_obj = uuid.UUID(uuid_hex)
        obj = cls(uuid_obj)

        # Return Object
        return obj

    def __unicode__(self):
        u = u"{:s}_{:012x}".format(type(self).__name__, self.uuid.node)
        return u

    def __str__(self):
        s = unicode(self).encode(_ENCODING)
        return s

    def __repr__(self):
        r = "{:s}".format(self.uuid)
        return r

    def __hash__(self):
        return hash(repr(self))

    def __eq__(self, other):
        return (repr(self) == repr(other))


class UUIDRedisObjectBase(UUIDObject):

    def __init__(self, uuid_obj):
        """Base Constructor"""
        super(UUIDRedisObjectBase, self).__init__(uuid_obj)
        self.obj_key = "{:s}:{:s}".format(self.base_key, repr(self)).lower()

    @classmethod
    def from_existing(cls, uuid_hex):
        """Existing Constructor"""

        # Call Parent
        obj = super(UUIDRedisObjectBase, cls).from_existing(uuid_hex)

        # Verify Object ID in Set
        if not obj.db.exists(obj.obj_key):
            raise UUIDRedisObjectDNE(obj)

        # Return Object
        return obj

    def delete(self):
        """Delete Object"""

        # Delete Object Data from DB
        if not self.db.delete(self.obj_key):
            raise UUIDRedisObjectError("Delete Failed")


class UUIDRedisHashBase(UUIDRedisObjectBase):
    """
    UUID Redis Hash Base Class

    """

    schema = _BASE_SCHEMA

    @classmethod
    def from_new(cls, d):
        """New Constructor"""

        # Call Parent
        obj = super(UUIDRedisHashBase, cls).from_new()

        # Set Times
        data = copy.deepcopy(d)
        data['created_time'] = str(time.time())
        data['modified_time'] = str(time.time())

        # Check dict
        if (set(data.keys()) != set(obj.schema)):
            raise KeyError("Keys {:s} do not match schema {:s}".format(data.keys(), obj.schema))

        # Add Object Data to DB
        if not obj.db.hmset(obj.obj_key, data):
            raise UUIDRedisObjectError("Create Failed")

        # Return Object
        return obj

    def __getitem__(self, k):
        if k in self.schema:
            return self.db.hget(self.obj_key, k)
        else:
            raise KeyError("Key {:s} not valid in {:s}".format(k, self))

    def __setitem__(self, k, v):
        if k in self.schema:
            return self.db.hset(self.obj_key, k, v)
        else:
            raise KeyError("Key {:s} not valid in {:s}".format(k, self))

    def copy(self):
        """ Copy Constructor """

        return self.from_new(self.get_dict)

    def __copy__(self):
        return self.copy()

    def __deepcopy__(self):
        return self.copy()

    def get_dict(self):
        """Get Dict"""
        data = self.db.hgetall(self.obj_key)
        return data

    def set_dict(self, d):
        """Set Dict"""

        # Create New Object
        data = copy.deepcopy(d)

        # Set Time
        data['modified_time'] = str(time.time())

        # Check dict
        if not set(data.keys()).issubset(set(self.schema)):
            raise KeyError("Keys {:s} do not match schema {:s}".format(data.keys(), self.schema))

        # Set dict
        self.db.hmset(self.obj_key, data)


class UUIDRedisSetBase(UUIDRedisObjectBase):
    """
    UUID Redis Set Base Class

    """

    @classmethod
    def from_new(cls, *vals):
        """New Constructor"""

        # Call Parent
        obj = super(UUIDRedisSetBase, cls).from_new()

        # Add lst to DB
        if not obj.db.sadd(obj.obj_key, *vals):
            raise UUIDRedisObjectError("Create Failed")

        # Return Object
        return obj

    def copy(self):
        """ Copy Constructor """

        return self.from_new(*list(self.get_vals))

    def __copy__(self):
        return self.copy()

    def __deepcopy__(self):
        return self.copy()

    def get_vals(self):
        """Get All Vals from Set"""
        return self.db.smembers(self.obj_key)

    def add_vals(self, *vals):
        """Add Vals to Set"""
        if not self.db.sadd(self.obj_key, *vals):
            raise UUIDRedisObjectError("Add Failed")

    def del_vals(self, *vals):
        """Remove Vals from Set"""
        if not self.db.srem(self.obj_key, *vals):
            raise UUIDRedisObjectError("Remove Failed")


class UUIDRedisFactory(object):

    def __init__(self, redis_db, base_cls, prefix=None):

        # Call Super
        super(UUIDRedisFactory, self).__init__()

        # Check Input
        if not issubclass(base_cls, UUIDRedisObjectBase):
            raise UUIDRedisFactoryError("cls must be subclass of UUIDRedisObjectBase")
        base_name = base_cls.__name__
        if not base_name.endswith(_SUF_BASE):
            raise UUIDRedisFactoryError("cls name must end with '{:s}'".format(_SUF_BASE))

        # Setup Class Name
        cls_name = base_name[0:base_name.rfind(_SUF_BASE)]

        # Setup DB
        self.db = redis_db

        # Setup Base Key
        if prefix == None:
            self.base_key = "{:s}".format(cls_name).lower()
        else:
            self.base_key = "{:s}:{:s}".format(prefix, cls_name).lower()

        # Setup Class
        class cls(base_cls):

            base_key = self.base_key
            db = self.db

        cls.__name__ = cls_name
        self.cls = cls

    def list_objs(self):
        """List Factory Objects"""
        obj_lst = self.db.keys("{:s}:{:s}".format(self.base_key, _UUID_GLOB))
        obj_uuids = []
        for obj_key in obj_lst:
            obj_uuid = obj_key.split(':')[-1]
            obj_uuids.append(obj_uuid)
        return set(obj_uuids)

    def get_objs(self):
        """Get Factory Objects"""
        obj_lst = self.db.keys("{:s}:{:s}".format(self.base_key, _UUID_GLOB))
        objs = []
        for obj_key in obj_lst:
            obj_uuid = obj_key.split(':')[-1]
            obj = cls.from_existing(obj_uuid)
            objs.append(obj)
        return objs

    def from_new(self, *args, **kwargs):
        return self.cls.from_new(*args, **kwargs)

    def from_existing(self, *args, **kwargs):
        return self.cls.from_existing(*args, **kwargs)


class Server(object):
    """
    COGS Server Class

    """

    # Override Constructor
    def __init__(self, redis_db):
        """Base Constructor"""

        # Call Parent Construtor
        super(Server, self).__init__()

        # Setup DB
        self.db = redis_db

        # Setup Factories
        self.AssignmentFactory = UUIDRedisFactory(self.db, AssignmentBase, None)<commit_msg>Reverted local DB setup removal<commit_after># -*- coding: utf-8 -*-

# <NAME>
# Summer 2014
# Univerity of Colorado

import copy
import os
import uuid
import time
import mimetypes

import redis
import werkzeug

import environment
import tester_script

_ENCODING = 'utf-8'

_SUF_BASE = 'Base'

_BASE_SCHEMA           = ['created_time', 'modified_time']
_USER_SCHEMA           = ['username', 'first', 'last', 'type']
_GROUP_SCHEMA          = ['name', 'members']
_COL_PERMISSION_SCHEMA = ['create', 'list']
_OBJ_PERMISSION_SCHEMA = ['read', 'write', 'delete']
_ASSIGNMENTS_SCHEMA    = ['name', 'contact', 'permissions']
_TESTS_SCHEMA          = ['name', 'contact', 'type', 'maxscore']
_SUBMISSIONS_SCHEMA    = ['author']
_RUNS_SCHEMA           = ['test', 'status', 'score', 'output']
_FILES_SCHEMA          = ['key', 'name', 'type', 'encoding', 'path']

_FILES_DIR = "./files/"

_UUID_GLOB = '????????-????-????-????-????????????'

_REDIS_CONF_DEFAULT = {'redis_host': "localhost",
                       'redis_port': 6379,
                       'redis_db': 3}

### Exceptions

class DatatypesError(Exception):
    """Base class for Datatypes Exceptions"""

    def __init__(self, *args, **kwargs):
        super(DatatypesError, self).__init__(*args, **kwargs)

class UUIDRedisFactoryError(DatatypesError):
    """Base class for UUID Redis Object Exceptions"""

    def __init__(self, *args, **kwargs):
        super(UUIDRedisFactoryError, self).__init__(*args, **kwargs)

class UUIDRedisObjectError(DatatypesError):
    """Base class for UUID Redis Object Exceptions"""

    def __init__(self, *args, **kwargs):
        super(UUIDRedisObjectError, self).__init__(*args, **kwargs)

class UUIDRedisObjectDNE(UUIDRedisObjectError):
    """UUID Redis Object Does Not Exist"""

    def __init__(self, obj):
        msg = "{:s} does not exist.".format(obj)
        super(UUIDRedisObjectDNE, self).__init__(msg)


### Objects

class UUIDObject(object):

    def __init__(self, uuid_obj):
        """Base Constructor"""
        super(UUIDObject, self).__init__()
        self.uuid = uuid_obj

    @classmethod
    def from_new(cls):
        """New Constructor"""

        # Create New Object
        uuid_obj = uuid.uuid4()
        obj = cls(uuid_obj)

        # Return Object
        return obj

    @classmethod
    def from_existing(cls, uuid_hex):
        """Existing Constructor"""

        # Create Existing Object
        uuid_obj = uuid.UUID(uuid_hex)
        obj = cls(uuid_obj)

        # Return Object
        return obj

    def __unicode__(self):
        u = u"{:s}_{:012x}".format(type(self).__name__, self.uuid.node)
        return u

    def __str__(self):
        s = unicode(self).encode(_ENCODING)
        return s

    def __repr__(self):
        r = "{:s}".format(self.uuid)
        return r

    def __hash__(self):
        return hash(repr(self))

    def __eq__(self, other):
        return (repr(self) == repr(other))


class UUIDRedisObjectBase(UUIDObject):

    def __init__(self, uuid_obj):
        """Base Constructor"""
        super(UUIDRedisObjectBase, self).__init__(uuid_obj)
        self.obj_key = "{:s}:{:s}".format(self.base_key, repr(self)).lower()

    @classmethod
    def from_existing(cls, uuid_hex):
        """Existing Constructor"""

        # Call Parent
        obj = super(UUIDRedisObjectBase, cls).from_existing(uuid_hex)

        # Verify Object ID in Set
        if not obj.db.exists(obj.obj_key):
            raise UUIDRedisObjectDNE(obj)

        # Return Object
        return obj

    def delete(self):
        """Delete Object"""

        # Delete Object Data from DB
        if not self.db.delete(self.obj_key):
            raise UUIDRedisObjectError("Delete Failed")


class UUIDRedisHashBase(UUIDRedisObjectBase):
    """
    UUID Redis Hash Base Class

    """

    schema = _BASE_SCHEMA

    @classmethod
    def from_new(cls, d):
        """New Constructor"""

        # Call Parent
        obj = super(UUIDRedisHashBase, cls).from_new()

        # Set Times
        data = copy.deepcopy(d)
        data['created_time'] = str(time.time())
        data['modified_time'] = str(time.time())

        # Check dict
        if (set(data.keys()) != set(obj.schema)):
            raise KeyError("Keys {:s} do not match schema {:s}".format(data.keys(), obj.schema))

        # Add Object Data to DB
        if not obj.db.hmset(obj.obj_key, data):
            raise UUIDRedisObjectError("Create Failed")

        # Return Object
        return obj

    def __getitem__(self, k):
        if k in self.schema:
            return self.db.hget(self.obj_key, k)
        else:
            raise KeyError("Key {:s} not valid in {:s}".format(k, self))

    def __setitem__(self, k, v):
        if k in self.schema:
            return self.db.hset(self.obj_key, k, v)
        else:
            raise KeyError("Key {:s} not valid in {:s}".format(k, self))

    def copy(self):
        """ Copy Constructor """

        return self.from_new(self.get_dict)

    def __copy__(self):
        return self.copy()

    def __deepcopy__(self):
        return self.copy()

    def get_dict(self):
        """Get Dict"""
        data = self.db.hgetall(self.obj_key)
        return data

    def set_dict(self, d):
        """Set Dict"""

        # Create New Object
        data = copy.deepcopy(d)

        # Set Time
        data['modified_time'] = str(time.time())

        # Check dict
        if not set(data.keys()).issubset(set(self.schema)):
            raise KeyError("Keys {:s} do not match schema {:s}".format(data.keys(), self.schema))

        # Set dict
        self.db.hmset(self.obj_key, data)


class UUIDRedisSetBase(UUIDRedisObjectBase):
    """
    UUID Redis Set Base Class

    """

    @classmethod
    def from_new(cls, *vals):
        """New Constructor"""

        # Call Parent
        obj = super(UUIDRedisSetBase, cls).from_new()

        # Add lst to DB
        if not obj.db.sadd(obj.obj_key, *vals):
            raise UUIDRedisObjectError("Create Failed")

        # Return Object
        return obj

    def copy(self):
        """ Copy Constructor """

        return self.from_new(*list(self.get_vals))

    def __copy__(self):
        return self.copy()

    def __deepcopy__(self):
        return self.copy()

    def get_vals(self):
        """Get All Vals from Set"""
        return self.db.smembers(self.obj_key)

    def add_vals(self, *vals):
        """Add Vals to Set"""
        if not self.db.sadd(self.obj_key, *vals):
            raise UUIDRedisObjectError("Add Failed")

    def del_vals(self, *vals):
        """Remove Vals from Set"""
        if not self.db.srem(self.obj_key, *vals):
            raise UUIDRedisObjectError("Remove Failed")


class UUIDRedisFactory(object):

    def __init__(self, redis_db, base_cls, prefix=None):

        # Call Super
        super(UUIDRedisFactory, self).__init__()

        # Check Input
        if not issubclass(base_cls, UUIDRedisObjectBase):
            raise UUIDRedisFactoryError("cls must be subclass of UUIDRedisObjectBase")
        base_name = base_cls.__name__
        if not base_name.endswith(_SUF_BASE):
            raise UUIDRedisFactoryError("cls name must end with '{:s}'".format(_SUF_BASE))

        # Setup Class Name
        cls_name = base_name[0:base_name.rfind(_SUF_BASE)]

        # Setup DB
        self.db = redis_db

        # Setup Base Key
        if prefix == None:
            self.base_key = "{:s}".format(cls_name).lower()
        else:
            self.base_key = "{:s}:{:s}".format(prefix, cls_name).lower()

        # Setup Class
        class cls(base_cls):

            base_key = self.base_key
            db = self.db

        cls.__name__ = cls_name
        self.cls = cls

    def list_objs(self):
        """List Factory Objects"""
        obj_lst = self.db.keys("{:s}:{:s}".format(self.base_key, _UUID_GLOB))
        obj_uuids = []
        for obj_key in obj_lst:
            obj_uuid = obj_key.split(':')[-1]
            obj_uuids.append(obj_uuid)
        return set(obj_uuids)

    def get_objs(self):
        """Get Factory Objects"""
        obj_lst = self.db.keys("{:s}:{:s}".format(self.base_key, _UUID_GLOB))
        objs = []
        for obj_key in obj_lst:
            obj_uuid = obj_key.split(':')[-1]
            obj = cls.from_existing(obj_uuid)
            objs.append(obj)
        return objs

    def from_new(self, *args, **kwargs):
        return self.cls.from_new(*args, **kwargs)

    def from_existing(self, *args, **kwargs):
        return self.cls.from_existing(*args, **kwargs)


class Server(object):
    """
    COGS Server Class

    """

    # Override Constructor
    def __init__(self, redis_db=None):
        """Base Constructor"""

        # Call Parent Construtor
        super(Server, self).__init__()

        # Setup DB
        if not redis_db:
            redis_conf = _REDIS_CONF_DEFAULT
            self.db = redis.StrictRedis(host=redis_conf['redis_host'],
                                        port=redis_conf['redis_port'],
                                        db=redis_conf['redis_db'])
        else:
            self.db = redis_db

        # Setup Factories
        self.AssignmentFactory = UUIDRedisFactory(self.db, AssignmentBase, None)
{'fraction_non_alphanumeric': 0.13152204047685057, 'fraction_numerical': 0.0017188799556418076, 'mean_word_length': 2.376895712413406, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6026131', 'n_tokens_mistral': 5915, 'n_tokens_neox': 5745, 'n_words': 1318}