Skip to content

Commit fb33196

Browse files
committed
python-jsonschema#708 - code review response; Py2 support removal
1 parent ef9c2b8 commit fb33196

File tree

2 files changed

+16
-54
lines changed

2 files changed

+16
-54
lines changed

jsonschema/cli.py

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -68,62 +68,27 @@ def validation_success(self, **kwargs):
6868

6969
@attr.s
7070
class _PrettyFormatter(object):
71-
_MESSAGE_BAR_CHAR = '═'
72-
_MESSAGE_CORNER_CHARS = ('╒', '╕')
73-
_MESSAGE_FORMAT = '{}══[{}]═══({})'
71+
_MESSAGE_BAR_CHAR = "═"
72+
_MESSAGE_CORNER_CHARS = "╒", "╕"
73+
_MESSAGE_FORMAT = "{}══[{}]═══({})"
7474
_MESSAGE_MAX_LENGTH = 79
7575

7676
@classmethod
7777
def _json_formatter(cls, x):
78-
return json.dumps(x, separators=(',\n', ': '), sort_keys=True)
78+
return json.dumps(x, indent=4, sort_keys=True)
7979

80-
def _message_end_chars(self, header=False):
81-
return self._MESSAGE_CORNER_CHARS if header is True \
80+
def _message_line(self, path, type, header=False):
81+
begin_char, end_char = self._MESSAGE_CORNER_CHARS if header \
8282
else [self._MESSAGE_BAR_CHAR] * 2
83-
84-
def _message_line_good_unicode(self, path, type, header=False):
85-
"""
86-
Message formatter for Python interpreters with good Unicode support.
87-
88-
TODO: Rename method when support for old Python no longer needed.
89-
"""
90-
begin_char, end_char = self._message_end_chars(header)
9183
return self._MESSAGE_FORMAT.format(begin_char, type, path) \
9284
.ljust(self._MESSAGE_MAX_LENGTH - 1, self._MESSAGE_BAR_CHAR) + \
9385
end_char
9486

95-
def _message_line_poor_unicode(self, path, type, header=False):
96-
"""
97-
Message formatter for Python interpreters with poor Unicode support.
98-
99-
TODO: Remove method when support for old Python no longer needed.
100-
"""
101-
begin_char, end_char = self._message_end_chars(header)
102-
103-
bar_length = self._MESSAGE_MAX_LENGTH - self._FORMAT_LENGTH - \
104-
len(type) - len(path)
105-
106-
return self._MESSAGE_FORMAT.format(begin_char, type, path) + \
107-
self._MESSAGE_BAR_CHAR * bar_length + end_char
108-
109-
if len(_MESSAGE_BAR_CHAR) != 1:
110-
# The code in this if-block is for Python interpreters that don't
111-
# treat multibyte Unicode characters as single characters.
112-
# E.g., most versions of Python 2.x.
113-
# TODO: Remove if-block when support for old Python no longer needed.
114-
115-
_message_line = _message_line_poor_unicode
116-
_FORMAT_LENGTH = len(
117-
_MESSAGE_FORMAT.replace(_MESSAGE_BAR_CHAR, '.')
118-
.format('.', '', '')) + 1
119-
else:
120-
_message_line = _message_line_good_unicode
121-
12287
def _error_msg(self, path, type, body):
12388
HEADER = self._message_line(path, type, header=True)
124-
FOOTER = '└' + '─' * (self._MESSAGE_MAX_LENGTH - 2) + '┘'
89+
FOOTER = "└" + "─" * (self._MESSAGE_MAX_LENGTH - 2) + "┘"
12590

126-
return '\n'.join((HEADER, str(body), FOOTER, '\n'))
91+
return "\n".join((HEADER, body, FOOTER, "\n"))
12792

12893
def filenotfound_error(self, path, exc_info):
12994
return self._error_msg(
@@ -151,7 +116,7 @@ def validation_error(self, instance_path, error):
151116
)
152117

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

156121

157122
@attr.s

jsonschema/exceptions.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
"""
44
from collections import defaultdict, deque
55
import itertools
6-
import pprint
76
import textwrap
7+
from functools import partial
8+
from pprint import pformat
89

910
import attr
1011

1112
from jsonschema import _utils
1213
from jsonschema.compat import PY3, iteritems
1314

14-
1515
WEAK_MATCHES = frozenset(["anyOf", "oneOf"])
1616
STRONG_MATCHES = frozenset()
1717

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

64-
def _formatted_message(self, formatter=None):
64+
def _formatted_message(self, formatter=partial(pformat, width=72)):
6565
essential_for_verbose = (
6666
self.validator, self.validator_value, self.instance, self.schema,
6767
)
6868
if any(m is _unset for m in essential_for_verbose):
6969
return self.message
7070

71-
if (callable(formatter)):
72-
pschema = formatter(self.schema)
73-
else:
74-
pschema = pprint.pformat(self.schema, width=72)
71+
pschema = formatter(self.schema)
7572

76-
pinstance = pprint.pformat(self.instance, width=72)
73+
pinstance = pformat(self.instance, width=72)
7774
return self.message + textwrap.dedent("""
7875
7976
Failed validating %r in %s%s:
@@ -207,8 +204,8 @@ def __init__(self, type, instance, schema):
207204
self.schema = schema
208205

209206
def __unicode__(self):
210-
pschema = pprint.pformat(self.schema, width=72)
211-
pinstance = pprint.pformat(self.instance, width=72)
207+
pschema = pformat(self.schema, width=72)
208+
pinstance = pformat(self.instance, width=72)
212209
return textwrap.dedent("""
213210
Unknown type %r for validator with schema:
214211
%s

0 commit comments

Comments
 (0)