<commit_before>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import os
import copy
import six
import requests
import json
import jsonschema
import datapackage_registry
from datapackage_registry.exceptions import DataPackageRegistryException
from .exceptions import (
    SchemaError, ValidationError
)


class Schema(object):
    '''Abstracts a JSON Schema and allows validation of data against it.

    Args:
        schema (str or dict): The JSON Schema itself as a dict, or a local path
            or URL to it.

    Raises:
        SchemaError: If unable to load schema or it was invalid.

    Warning:
        The schema objects created with this class are read-only. You should
        change any of its attributes after creation.
    '''
    def __init__(self, schema):
        self._registry = self._load_registry()
        self._schema = self._load_schema(schema, self._registry)
        self._validator = self._load_validator(self._schema, self._registry)
        self._check_schema()

    def to_dict(self):
        '''dict: Convert this :class:`.Schema` to dict.'''
        return copy.deepcopy(self._schema)

    def validate(self, data):
        '''Validates a data dict against this schema.

        Args:
            data (dict): The data to be validated.

        Raises:
            ValidationError: If the data is invalid.
        '''
        try:
            self._validator.validate(data)
        except jsonschema.ValidationError as e:
            six.raise_from(ValidationError(e), e)

    def _load_registry(self):
        try:
            return datapackage_registry.Registry()
        except DataPackageRegistryException as e:
            six.raise_from(SchemaError(e), e)

    def _load_schema(self, schema, registry):
        the_schema = schema

        if isinstance(schema, six.string_types):
            try:
                the_schema = registry.get(schema)
                if not the_schema:
                    if os.path.isfile(schema):
                        with open(schema, 'r') as f:
                            the_schema = json.load(f)
                    else:
                        req = requests.get(schema)<commit_msg>Improve `Schema.validate()` exception message<commit_after>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import os
import copy
import six
import requests
import json
import jsonschema
import datapackage_registry
from datapackage_registry.exceptions import DataPackageRegistryException
from .exceptions import (
    SchemaError, ValidationError
)


class Schema(object):
    '''Abstracts a JSON Schema and allows validation of data against it.

    Args:
        schema (str or dict): The JSON Schema itself as a dict, or a local path
            or URL to it.

    Raises:
        SchemaError: If unable to load schema or it was invalid.

    Warning:
        The schema objects created with this class are read-only. You should
        change any of its attributes after creation.
    '''
    def __init__(self, schema):
        self._registry = self._load_registry()
        self._schema = self._load_schema(schema, self._registry)
        self._validator = self._load_validator(self._schema, self._registry)
        self._check_schema()

    def to_dict(self):
        '''dict: Convert this :class:`.Schema` to dict.'''
        return copy.deepcopy(self._schema)

    def validate(self, data):
        '''Validates a data dict against this schema.

        Args:
            data (dict): The data to be validated.

        Raises:
            ValidationError: If the data is invalid.
        '''
        try:
            self._validator.validate(data)
        except jsonschema.ValidationError as e:
            six.raise_from(ValidationError.create_from(e), e)

    def _load_registry(self):
        try:
            return datapackage_registry.Registry()
        except DataPackageRegistryException as e:
            six.raise_from(SchemaError(e), e)

    def _load_schema(self, schema, registry):
        the_schema = schema

        if isinstance(schema, six.string_types):
            try:
                the_schema = registry.get(schema)
                if not the_schema:
                    if os.path.isfile(schema):
                        with open(schema, 'r') as f:
                            the_schema = json.load(f)
                    else:
                        req = requests.get(schema)
{'fraction_non_alphanumeric': 0.07145969498910676, 'fraction_numerical': 0.0, 'mean_word_length': 2.251416430594901, 'pattern_counts': {'":': 0, '<': 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': '7447567', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1163, 'n_words': 392}