# Path: src/rest_toolkit/compat.py
# def add_metaclass(metaclass):
#     """Class decorator for creating a class with a metaclass."""
#     def wrapper(cls):
#         orig_vars = cls.__dict__.copy()
#         orig_vars.pop('__dict__', None)
#         orig_vars.pop('__weakref__', None)
#         slots = orig_vars.get('__slots__')
#         if slots is not None:
#             if isinstance(slots, str):
#                 slots = [slots]
#             for slots_var in slots:
#                 orig_vars.pop(slots_var)
#         return metaclass(cls.__name__, cls.__bases__, orig_vars)
#     return wrapper


import abc
import colander
from pyramid.httpexceptions import HTTPBadRequest
from ..compat import add_metaclass
from __future__ import absolute_import


def validate(data, schema):
    """Validate data against a Colander schema class.

    This is a helper function used by :py:class:`ColanderSchemaValidationMixin`
    to validate data against a Colander schema. If validation fails this function
    will raise a :py:class:`pyramid.httpexceptions.HTTPBadRequest` exception
    describing the validation error.

    :raises pyramid.httpexceptions.HTTPBadRequest: if validation fails this
        exception is raised to abort any further processing.
    """
    schema_instance = schema()
    try:
        schema_instance.deserialize(data)
    except colander.Invalid as e:
        raise HTTPBadRequest(e.msg)



# Path: src/rest_toolkit/compat.py
# def add_metaclass(metaclass):
#     """Class decorator for creating a class with a metaclass."""
#     def wrapper(cls):
#         orig_vars = cls.__dict__.copy()
#         orig_vars.pop('__dict__', None)
#         orig_vars.pop('__weakref__', None)
#         slots = orig_vars.get('__slots__')
#         if slots is not None:
#             if isinstance(slots, str):
#                 slots = [slots]
#             for slots_var in slots:
#                 orig_vars.pop(slots_var)
#         return metaclass(cls.__name__, cls.__bases__, orig_vars)
#     return wrapper


# Path: src/rest_toolkit/ext/colander.py
import abc
import colander
from pyramid.httpexceptions import HTTPBadRequest
from ..compat import add_metaclass

from __future__ import absolute_import


def validate(data, schema):
    """Validate data against a Colander schema class.

    This is a helper function used by :py:class:`ColanderSchemaValidationMixin`
    to validate data against a Colander schema. If validation fails this function
    will raise a :py:class:`pyramid.httpexceptions.HTTPBadRequest` exception
    describing the validation error.

    :raises pyramid.httpexceptions.HTTPBadRequest: if validation fails this
        exception is raised to abort any further processing.
    """
    schema_instance = schema()
    try:
        schema_instance.deserialize(data)
    except colander.Invalid as e:
        raise HTTPBadRequest(e.msg)



@add_metaclass(abc.ABCMeta)