Skip to content

Commit

Permalink
python-jsonschema#708 - code review response; Py2 support removal
Browse files Browse the repository at this point in the history
  • Loading branch information
sloanlance committed Aug 4, 2020
1 parent ef9c2b8 commit fb33196
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 54 deletions.
53 changes: 9 additions & 44 deletions jsonschema/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,62 +68,27 @@ def validation_success(self, **kwargs):

@attr.s
class _PrettyFormatter(object):
_MESSAGE_BAR_CHAR = '═'
_MESSAGE_CORNER_CHARS = ('╒', '╕')
_MESSAGE_FORMAT = '{}══[{}]═══({})'
_MESSAGE_BAR_CHAR = "═"
_MESSAGE_CORNER_CHARS = "╒", "╕"
_MESSAGE_FORMAT = "{}══[{}]═══({})"
_MESSAGE_MAX_LENGTH = 79

@classmethod
def _json_formatter(cls, x):
return json.dumps(x, separators=(',\n', ': '), sort_keys=True)
return json.dumps(x, indent=4, sort_keys=True)

def _message_end_chars(self, header=False):
return self._MESSAGE_CORNER_CHARS if header is True \
def _message_line(self, path, type, header=False):
begin_char, end_char = self._MESSAGE_CORNER_CHARS if header \
else [self._MESSAGE_BAR_CHAR] * 2

def _message_line_good_unicode(self, path, type, header=False):
"""
Message formatter for Python interpreters with good Unicode support.
TODO: Rename method when support for old Python no longer needed.
"""
begin_char, end_char = self._message_end_chars(header)
return self._MESSAGE_FORMAT.format(begin_char, type, path) \
.ljust(self._MESSAGE_MAX_LENGTH - 1, self._MESSAGE_BAR_CHAR) + \
end_char

def _message_line_poor_unicode(self, path, type, header=False):
"""
Message formatter for Python interpreters with poor Unicode support.
TODO: Remove method when support for old Python no longer needed.
"""
begin_char, end_char = self._message_end_chars(header)

bar_length = self._MESSAGE_MAX_LENGTH - self._FORMAT_LENGTH - \
len(type) - len(path)

return self._MESSAGE_FORMAT.format(begin_char, type, path) + \
self._MESSAGE_BAR_CHAR * bar_length + end_char

if len(_MESSAGE_BAR_CHAR) != 1:
# The code in this if-block is for Python interpreters that don't
# treat multibyte Unicode characters as single characters.
# E.g., most versions of Python 2.x.
# TODO: Remove if-block when support for old Python no longer needed.

_message_line = _message_line_poor_unicode
_FORMAT_LENGTH = len(
_MESSAGE_FORMAT.replace(_MESSAGE_BAR_CHAR, '.')
.format('.', '', '')) + 1
else:
_message_line = _message_line_good_unicode

def _error_msg(self, path, type, body):
HEADER = self._message_line(path, type, header=True)
FOOTER = '└' + '─' * (self._MESSAGE_MAX_LENGTH - 2) + '┘'
FOOTER = "└" + "─" * (self._MESSAGE_MAX_LENGTH - 2) + "┘"

return '\n'.join((HEADER, str(body), FOOTER, '\n'))
return "\n".join((HEADER, body, FOOTER, "\n"))

def filenotfound_error(self, path, exc_info):
return self._error_msg(
Expand Down Expand Up @@ -151,7 +116,7 @@ def validation_error(self, instance_path, error):
)

def validation_success(self, instance_path):
return self._message_line(path=instance_path, type='SUCCESS') + '\n\n'
return self._message_line(path=instance_path, type="SUCCESS") + "\n\n"


@attr.s
Expand Down
17 changes: 7 additions & 10 deletions jsonschema/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"""
from collections import defaultdict, deque
import itertools
import pprint
import textwrap
from functools import partial
from pprint import pformat

import attr

from jsonschema import _utils
from jsonschema.compat import PY3, iteritems


WEAK_MATCHES = frozenset(["anyOf", "oneOf"])
STRONG_MATCHES = frozenset()

Expand Down Expand Up @@ -61,19 +61,16 @@ def __init__(
def __repr__(self):
return "<%s: %r>" % (self.__class__.__name__, self.message)

def _formatted_message(self, formatter=None):
def _formatted_message(self, formatter=partial(pformat, width=72)):
essential_for_verbose = (
self.validator, self.validator_value, self.instance, self.schema,
)
if any(m is _unset for m in essential_for_verbose):
return self.message

if (callable(formatter)):
pschema = formatter(self.schema)
else:
pschema = pprint.pformat(self.schema, width=72)
pschema = formatter(self.schema)

pinstance = pprint.pformat(self.instance, width=72)
pinstance = pformat(self.instance, width=72)
return self.message + textwrap.dedent("""
Failed validating %r in %s%s:
Expand Down Expand Up @@ -207,8 +204,8 @@ def __init__(self, type, instance, schema):
self.schema = schema

def __unicode__(self):
pschema = pprint.pformat(self.schema, width=72)
pinstance = pprint.pformat(self.instance, width=72)
pschema = pformat(self.schema, width=72)
pinstance = pformat(self.instance, width=72)
return textwrap.dedent("""
Unknown type %r for validator with schema:
%s
Expand Down

0 comments on commit fb33196

Please sign in to comment.