<commit_before>from six import iteritems
from collections import defaultdict
from cornice import Service
from pyramid.security import Everyone

from daybed.acl import (
    get_model_acls, invert_acls_matrix, dict_list2set, dict_set2list,
    PERMISSIONS_SET
)
from daybed.backends.exceptions import ModelNotFound
from daybed.views.errors import forbidden_view
from daybed.schemas.validators import model_validator, acls_validator


models = Service(name='models', path='/models', description='Models')


model = Service(name='model',
                path='/models/{model_id}',
                description='Model',
                renderer="jsonp",
                cors_origins=('*',))


definition = Service(name='model-definition',
                     path='/models/{model_id}/definition',
                     description='Model Definitions',
                     renderer="jsonp",
                     cors_origins=('*',))


acls = Service(name='model-acls',
               path='/models/{model_id}/acls',
               description='Model ACLs',
               renderer="jsonp",
               cors_origins=('*',))


@definition.get(permission='get_definition')
def get_definition(request):
    """Retrieves a model definition."""
    model_id = request.matchdict['model_id']
    try:
        return request.db.get_model_definition(model_id)
    except ModelNotFound:
        request.response.status = "404 Not Found"
        return {
            "error": "404 Not Found",
            "msg": "%s: model not found" % model_id
        }


@acls.get(permission='get_acls')
def get_acls(request):
    """Retrieves a model acls."""
    model_id = request.matchdict['model_id']
    try:
        return invert_acls_matrix(request.db.get_model_acls(model_id))
    except ModelNotFound:
        request.response.status = "404 Not Found"
        return {
            "error": "404 Not Found",
            "msg": "%s: model not found" % model_id
        }


@acls.patch(permission='put_acls', validators=(acls_validator,))
def patch_acls(request):
    """Update a model acls."""
    model_id = request.matchdict['model_id']
    definition = request.db.get_model_definition(model_id)
    acls = dict_list2set(request.db.get_model_acls(model_id))

    for token, perms in iteritems(request.validated['acls']):
        # Handle remove all
        if '-all' in [perm.lower() for perm in perms]:
            for p in PERMISSIONS_SET:
                acls[p].discard(token)
        # Handle add all
        elif 'all' in [perm.lstrip('+').lower() for perm in perms]:
            for p in PERMISSIONS_SET:
                acls[p].add(token)
        # Handle add/remove perms list
        else:
            for perm in perms:
                perm = perm.lower()
                if perm.startswith('-'):
                    acls[perm.lstrip('-')].discard(token)
                else:
                    acls[perm.lstrip('+')].add(token)

    request.db.put_model(definition, dict_set2list(acls), model_id)
    return invert_acls_matrix(acls)


@acls.put(permission='put_acls', validators=(acls_validator,))
def put_acls(request):
    """Update a model acls."""
    model_id = request.matchdict['model_id']
    definition = request.db.get_model_definition(model_id)
    acls = defaultdict(set)
    for token, perms in iteritems(request.validated['acls']):
        for perm in perms:
            if not perm.startswith('-'):
                acls[perm.lstrip('+')].add(token)
    request.db.put_model(definition, dict_set2list(acls))
    return invert_acls_matrix(acls)


@models.post(permission='post_model', validators=(model_validator,))
def post_models(request):
    """Creates a model with the given definition and records, if any."""
    if request.token:
        token = request.token<commit_msg>Let PUT /acls handle ALL.<commit_after>from six import iteritems
from collections import defaultdict
from cornice import Service
from pyramid.security import Everyone

from daybed.acl import (
    get_model_acls, invert_acls_matrix, dict_list2set, dict_set2list,
    PERMISSIONS_SET
)
from daybed.backends.exceptions import ModelNotFound
from daybed.views.errors import forbidden_view
from daybed.schemas.validators import model_validator, acls_validator


models = Service(name='models', path='/models', description='Models')


model = Service(name='model',
                path='/models/{model_id}',
                description='Model',
                renderer="jsonp",
                cors_origins=('*',))


definition = Service(name='model-definition',
                     path='/models/{model_id}/definition',
                     description='Model Definitions',
                     renderer="jsonp",
                     cors_origins=('*',))


acls = Service(name='model-acls',
               path='/models/{model_id}/acls',
               description='Model ACLs',
               renderer="jsonp",
               cors_origins=('*',))


@definition.get(permission='get_definition')
def get_definition(request):
    """Retrieves a model definition."""
    model_id = request.matchdict['model_id']
    try:
        return request.db.get_model_definition(model_id)
    except ModelNotFound:
        request.response.status = "404 Not Found"
        return {
            "error": "404 Not Found",
            "msg": "%s: model not found" % model_id
        }


@acls.get(permission='get_acls')
def get_acls(request):
    """Retrieves a model acls."""
    model_id = request.matchdict['model_id']
    try:
        return invert_acls_matrix(request.db.get_model_acls(model_id))
    except ModelNotFound:
        request.response.status = "404 Not Found"
        return {
            "error": "404 Not Found",
            "msg": "%s: model not found" % model_id
        }


@acls.patch(permission='put_acls', validators=(acls_validator,))
def patch_acls(request):
    """Update a model acls."""
    model_id = request.matchdict['model_id']
    definition = request.db.get_model_definition(model_id)
    acls = dict_list2set(request.db.get_model_acls(model_id))

    for token, perms in iteritems(request.validated['acls']):
        # Handle remove all
        if '-all' in [perm.lower() for perm in perms]:
            for p in PERMISSIONS_SET:
                acls[p].discard(token)
        # Handle add all
        elif 'all' in [perm.lstrip('+').lower() for perm in perms]:
            for p in PERMISSIONS_SET:
                acls[p].add(token)
        # Handle add/remove perms list
        else:
            for perm in perms:
                perm = perm.lower()
                if perm.startswith('-'):
                    acls[perm.lstrip('-')].discard(token)
                else:
                    acls[perm.lstrip('+')].add(token)

    request.db.put_model(definition, dict_set2list(acls), model_id)
    return invert_acls_matrix(acls)


@acls.put(permission='put_acls', validators=(acls_validator,))
def put_acls(request):
    """Update a model acls."""
    model_id = request.matchdict['model_id']
    definition = request.db.get_model_definition(model_id)
    acls = defaultdict(set)
    for token, perms in iteritems(request.validated['acls']):
        perms = [p.lstrip('+').lower() for p in perms]
        if 'all' in perms:
            perms = PERMISSIONS_SET
        for perm in perms:
            if not perm.startswith('-'):
                acls[perm].add(token)
    acls = dict_set2list(acls)
    request.db.put_model(definition, acls, model_id)
    return invert_acls_matrix(acls)


@models.post(permission='post_model', validators=(model_validator,))
def post_models(request):
    """Creates a model with the given definition and records, if any."""
    if request.token:
        token = request.token
{'fraction_non_alphanumeric': 0.12179321067634102, 'fraction_numerical': 0.004405286343612335, 'mean_word_length': 2.6307619943555975, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4516019', 'n_tokens_mistral': 2388, 'n_tokens_neox': 2304, 'n_words': 500}