diff options
author | Julian Berman <Julian@GrayVines.com> | 2013-05-20 18:34:32 -0400 |
---|---|---|
committer | Julian Berman <Julian@GrayVines.com> | 2013-05-20 18:34:32 -0400 |
commit | 25fb92cd2cdb1ea1b370072df4a44ec78fbeca9d (patch) | |
tree | 49eaa2a72a194d9a2ab071ecb18248d17687a6b1 | |
parent | 0fe509c9c0ce61331c7c438bae983635cdf86076 (diff) | |
download | jsonschema-25fb92cd2cdb1ea1b370072df4a44ec78fbeca9d.tar.gz |
Oh fine I'll have an exceptions module.
I generally hate this, but there's enough ugliness and eventually it'd be nice
to have these in a public module for when we might want them out of __init__.py
-rw-r--r-- | jsonschema/__init__.py | 9 | ||||
-rw-r--r-- | jsonschema/_format.py | 18 | ||||
-rw-r--r-- | jsonschema/_validators.py | 82 | ||||
-rw-r--r-- | jsonschema/exceptions.py | 112 | ||||
-rw-r--r-- | jsonschema/validators.py | 6 |
5 files changed, 121 insertions, 106 deletions
diff --git a/jsonschema/__init__.py b/jsonschema/__init__.py index d2e1b6f..ec4fcec 100644 --- a/jsonschema/__init__.py +++ b/jsonschema/__init__.py @@ -9,13 +9,14 @@ instance under a schema, and will create a validator for you. """ +from jsonschema.exceptions import ( + FormatError, RefResolutionError, SchemaError, UnknownType, ValidationError +) from jsonschema._format import ( - FormatChecker, FormatError, draft3_format_checker, draft4_format_checker, + FormatChecker, draft3_format_checker, draft4_format_checker, ) -from jsonschema._validators import ValidationError from jsonschema.validators import ( - RefResolutionError, SchemaError, UnknownType, ErrorTree, - Draft3Validator, Draft4Validator, RefResolver, ValidatorMixin, + ErrorTree, Draft3Validator, Draft4Validator, RefResolver, ValidatorMixin, validate, validates, ) diff --git a/jsonschema/_format.py b/jsonschema/_format.py index 238e7117..f5b0849 100644 --- a/jsonschema/_format.py +++ b/jsonschema/_format.py @@ -2,23 +2,7 @@ import datetime import re import socket -from jsonschema.compat import PY3 - - -class FormatError(Exception): - def __init__(self, message, cause=None): - super(FormatError, self).__init__(message, cause) - self.message = message - self.cause = self.__cause__ = cause - - def __str__(self): - return self.message.encode("utf-8") - - def __unicode__(self): - return self.message - - if PY3: - __str__ = __unicode__ +from jsonschema.exceptions import FormatError class FormatChecker(object): diff --git a/jsonschema/_validators.py b/jsonschema/_validators.py index 89d0086..5246dfa 100644 --- a/jsonschema/_validators.py +++ b/jsonschema/_validators.py @@ -1,89 +1,11 @@ -import collections -import pprint import re -import textwrap from jsonschema import _utils -from jsonschema._format import FormatError -from jsonschema.compat import PY3, iteritems +from jsonschema.exceptions import FormatError, ValidationError +from jsonschema.compat import iteritems FLOAT_TOLERANCE = 10 ** -15 -unset = _utils.Unset() - - -class _Error(Exception): - def __init__( - self, message, validator=unset, path=(), cause=None, context=(), - validator_value=unset, instance=unset, schema=unset, schema_path=(), - ): - self.message = message - self.path = collections.deque(path) - self.schema_path = collections.deque(schema_path) - self.context = list(context) - self.cause = self.__cause__ = cause - self.validator = validator - self.validator_value = validator_value - self.instance = instance - self.schema = schema - - @classmethod - def create_from(cls, other): - return cls( - message=other.message, - cause=other.cause, - context=other.context, - path=other.path, - schema_path=other.schema_path, - validator=other.validator, - validator_value=other.validator_value, - instance=other.instance, - schema=other.schema, - ) - - def _set(self, **kwargs): - for k, v in iteritems(kwargs): - if getattr(self, k) is unset: - setattr(self, k, v) - - def __repr__(self): - return "<%s: %r>" % (self.__class__.__name__, self.message) - - def __str__(self): - return unicode(self).encode("utf-8") - - def __unicode__(self): - if unset in ( - self.validator, self.validator_value, self.instance, self.schema, - ): - return self.message - - path = _utils.format_as_index(self.path) - schema_path = _utils.format_as_index(list(self.schema_path)[:-1]) - - pschema = pprint.pformat(self.schema, width=72) - pinstance = pprint.pformat(self.instance, width=72) - return self.message + textwrap.dedent(""" - - Failed validating %r in schema%s: - %s - - On instance%s: - %s - """.rstrip() - ) % ( - self.validator, - schema_path, - _utils.indent(pschema), - path, - _utils.indent(pinstance), - ) - - if PY3: - __str__ = __unicode__ - - -class ValidationError(_Error): pass def patternProperties(validator, patternProperties, instance, schema): diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py new file mode 100644 index 0000000..f1d3e42 --- /dev/null +++ b/jsonschema/exceptions.py @@ -0,0 +1,112 @@ +import collections +import pprint +import textwrap + +from jsonschema import _utils +from jsonschema.compat import PY3, iteritems + + +_unset = _utils.Unset() + + +class _Error(Exception): + def __init__( + self, message, validator=_unset, path=(), cause=None, context=(), + validator_value=_unset, instance=_unset, schema=_unset, schema_path=(), + ): + self.message = message + self.path = collections.deque(path) + self.schema_path = collections.deque(schema_path) + self.context = list(context) + self.cause = self.__cause__ = cause + self.validator = validator + self.validator_value = validator_value + self.instance = instance + self.schema = schema + + @classmethod + def create_from(cls, other): + return cls( + message=other.message, + cause=other.cause, + context=other.context, + path=other.path, + schema_path=other.schema_path, + validator=other.validator, + validator_value=other.validator_value, + instance=other.instance, + schema=other.schema, + ) + + def _set(self, **kwargs): + for k, v in iteritems(kwargs): + if getattr(self, k) is _unset: + setattr(self, k, v) + + def __repr__(self): + return "<%s: %r>" % (self.__class__.__name__, self.message) + + def __str__(self): + return unicode(self).encode("utf-8") + + def __unicode__(self): + if _unset in ( + self.validator, self.validator_value, self.instance, self.schema, + ): + return self.message + + path = _utils.format_as_index(self.path) + schema_path = _utils.format_as_index(list(self.schema_path)[:-1]) + + pschema = pprint.pformat(self.schema, width=72) + pinstance = pprint.pformat(self.instance, width=72) + return self.message + textwrap.dedent(""" + + Failed validating %r in schema%s: + %s + + On instance%s: + %s + """.rstrip() + ) % ( + self.validator, + schema_path, + _utils.indent(pschema), + path, + _utils.indent(pinstance), + ) + + if PY3: + __str__ = __unicode__ + + +class ValidationError(_Error): + pass + + +class SchemaError(_Error): + pass + + +class RefResolutionError(Exception): + pass + + +class UnknownType(Exception): + pass + + +class FormatError(Exception): + def __init__(self, message, cause=None): + super(FormatError, self).__init__(message, cause) + self.message = message + self.cause = self.__cause__ = cause + + def __str__(self): + return self.message.encode("utf-8") + + def __unicode__(self): + return self.message + + if PY3: + __str__ = __unicode__ diff --git a/jsonschema/validators.py b/jsonschema/validators.py index 43ed517..3db7ce9 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -15,11 +15,7 @@ from jsonschema.compat import ( PY3, Sequence, urljoin, urlsplit, urldefrag, unquote, urlopen, str_types, int_types, iteritems, ) - - -class RefResolutionError(Exception): pass -class SchemaError(_validators._Error): pass -class UnknownType(Exception): pass +from jsonschema.exceptions import RefResolutionError, SchemaError, UnknownType _unset = _utils.Unset() |