2018-11-30 19:16:14 +00:00
|
|
|
# coding: utf-8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
from jsonschema import Draft4Validator
|
💫 Replace ujson, msgpack and dill/pickle/cloudpickle with srsly (#3003)
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉
See here: https://github.com/explosion/srsly
Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.
At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.
srsly currently includes forks of the following packages:
ujson
msgpack
msgpack-numpy
cloudpickle
* WIP: replace json/ujson with srsly
* Replace ujson in examples
Use regular json instead of srsly to make code easier to read and follow
* Update requirements
* Fix imports
* Fix typos
* Replace msgpack with srsly
* Fix warning
2018-12-03 00:28:22 +00:00
|
|
|
import srsly
|
2018-11-30 19:16:14 +00:00
|
|
|
|
|
|
|
from ...errors import Errors
|
|
|
|
|
|
|
|
|
|
|
|
SCHEMAS = {}
|
|
|
|
|
|
|
|
|
|
|
|
def get_schema(name):
|
|
|
|
"""Get the JSON schema for a given name. Looks for a .json file in
|
|
|
|
spacy.cli.schemas, validates the schema and raises ValueError if not found.
|
|
|
|
|
|
|
|
EXAMPLE:
|
|
|
|
>>> schema = get_schema('training')
|
|
|
|
|
|
|
|
name (unicode): The name of the schema.
|
|
|
|
RETURNS (dict): The JSON schema.
|
|
|
|
"""
|
|
|
|
if name not in SCHEMAS:
|
|
|
|
schema_path = Path(__file__).parent / "{}.json".format(name)
|
|
|
|
if not schema_path.exists():
|
|
|
|
raise ValueError(Errors.E104.format(name=name))
|
💫 Replace ujson, msgpack and dill/pickle/cloudpickle with srsly (#3003)
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉
See here: https://github.com/explosion/srsly
Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.
At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.
srsly currently includes forks of the following packages:
ujson
msgpack
msgpack-numpy
cloudpickle
* WIP: replace json/ujson with srsly
* Replace ujson in examples
Use regular json instead of srsly to make code easier to read and follow
* Update requirements
* Fix imports
* Fix typos
* Replace msgpack with srsly
* Fix warning
2018-12-03 00:28:22 +00:00
|
|
|
schema = srsly.read_json(schema_path)
|
2018-11-30 19:16:14 +00:00
|
|
|
# TODO: replace with (stable) Draft6Validator, if available
|
|
|
|
validator = Draft4Validator(schema)
|
|
|
|
validator.check_schema(schema)
|
|
|
|
SCHEMAS[name] = schema
|
|
|
|
return SCHEMAS[name]
|
|
|
|
|
|
|
|
|
|
|
|
def validate_json(data, schema):
|
|
|
|
"""Validate data against a given JSON schema (see https://json-schema.org).
|
|
|
|
|
|
|
|
data: JSON-serializable data to validate.
|
|
|
|
schema (dict): The JSON schema.
|
|
|
|
RETURNS (list): A list of error messages, if available.
|
|
|
|
"""
|
|
|
|
validator = Draft4Validator(schema)
|
|
|
|
errors = []
|
|
|
|
for err in sorted(validator.iter_errors(data), key=lambda e: e.path):
|
|
|
|
if err.path:
|
|
|
|
err_path = "[{}]".format(" -> ".join([str(p) for p in err.path]))
|
|
|
|
else:
|
|
|
|
err_path = ""
|
|
|
|
errors.append(err.message + " " + err_path)
|
|
|
|
return errors
|