Skip to content

Commit 96ee573

Browse files
committed
python-jsonschema#708 - improve pretty output format
Unicode box characters, true JSON output, (slightly) more space between each error, etc.
1 parent dd31840 commit 96ee573

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

jsonschema/cli.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,21 @@ def validation_success(self, **kwargs):
7070
@attr.s
7171
class _PrettyFormatter(object):
7272

73-
_ERROR_MSG = dedent(
74-
"""\
75-
===[{type}]===({path})===
73+
_WIDTH = 79
7674

77-
{body}
78-
-----------------------------
79-
""",
80-
)
81-
_SUCCESS_MSG = "===[SUCCESS]===({path})===\n"
75+
def _simple_msg(self, path, type, header=False):
76+
begin_end_chars = '╒╕' if header is True else '══'
77+
return '{}══[{}]═══({})'.format(begin_end_chars[0], type, path)\
78+
.ljust(self._WIDTH - 1, '═') + begin_end_chars[1]
79+
80+
def _error_msg(self, path, type, body):
81+
HEADER = self._simple_msg(path, type, header=True)
82+
FOOTER = '└' + '─' * (self._WIDTH - 2) + '┘'
83+
84+
return '\n'.join((HEADER, str(body), FOOTER, '\n'))
8285

8386
def filenotfound_error(self, path, exc_info):
84-
return self._ERROR_MSG.format(
87+
return self._error_msg(
8588
path=path,
8689
type="FileNotFoundError",
8790
body="{!r} does not exist.".format(path),
@@ -92,21 +95,21 @@ def parsing_error(self, path, exc_info):
9295
exc_lines = "".join(
9396
traceback.format_exception(exc_type, exc_value, exc_traceback),
9497
)
95-
return self._ERROR_MSG.format(
98+
return self._error_msg(
9699
path=path,
97100
type=exc_type.__name__,
98101
body=exc_lines,
99102
)
100103

101104
def validation_error(self, instance_path, error):
102-
return self._ERROR_MSG.format(
105+
return self._error_msg(
103106
path=instance_path,
104107
type=error.__class__.__name__,
105108
body=error,
106109
)
107110

108111
def validation_success(self, instance_path):
109-
return self._SUCCESS_MSG.format(path=instance_path)
112+
return self._simple_msg(path=instance_path, type='SUCCESS') + '\n\n'
110113

111114

112115
@attr.s

jsonschema/exceptions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Validation errors, and some surrounding helpers.
33
"""
4+
import json
45
from collections import defaultdict, deque
56
import itertools
67
import pprint
@@ -68,7 +69,7 @@ def __unicode__(self):
6869
if any(m is _unset for m in essential_for_verbose):
6970
return self.message
7071

71-
pschema = pprint.pformat(self.schema, width=72)
72+
pschema = json.dumps(self.schema, separators=(',\n', ': '), sort_keys=True)
7273
pinstance = pprint.pformat(self.instance, width=72)
7374
return self.message + textwrap.dedent("""
7475
@@ -200,7 +201,7 @@ def __init__(self, type, instance, schema):
200201
self.schema = schema
201202

202203
def __unicode__(self):
203-
pschema = pprint.pformat(self.schema, width=72)
204+
pschema = json.dumps(self.schema, separators=(',\n', ': '), sort_keys=True)
204205
pinstance = pprint.pformat(self.instance, width=72)
205206
return textwrap.dedent("""
206207
Unknown type %r for validator with schema:

0 commit comments

Comments
 (0)