|
15 | 15 |
|
16 | 16 | from jsonschema import __version__
|
17 | 17 | from jsonschema._reflect import namedAny
|
18 |
| -from jsonschema.compat import PY3, JSONDecodeError |
| 18 | +from jsonschema.compat import JSONDecodeError |
19 | 19 | from jsonschema.exceptions import SchemaError
|
20 | 20 | from jsonschema.validators import validator_for
|
21 | 21 |
|
@@ -70,35 +70,44 @@ def validation_success(self, **kwargs):
|
70 | 70 |
|
71 | 71 | @attr.s
|
72 | 72 | class _PrettyFormatter(object):
|
73 |
| - |
74 |
| - _WIDTH = 79 |
75 |
| - _HEADER_LINE = '═' |
| 73 | + _MESSAGE_BAR_CHAR = '═' |
| 74 | + _MESSAGE_CORNER_CHARS = ('╒', '╕') |
76 | 75 | _MESSAGE_FORMAT = '{}══[{}]═══({})'
|
| 76 | + _MESSAGE_MAX_LENGTH = 79 |
77 | 77 |
|
78 | 78 | @classmethod
|
79 | 79 | def _json_formatter(cls, x):
|
80 | 80 | return json.dumps(x, separators=(',\n', ': '), sort_keys=True)
|
81 | 81 |
|
82 |
| - def _simple_msg_v3(self, path, type, header=False): |
83 |
| - begin_end_chars = ('╒', '╕') if header is True else ('═', '═') |
84 |
| - return '{}══[{}]═══({})'.format(begin_end_chars[0], type, path) \ |
85 |
| - .ljust(self._WIDTH - 1, '═') + begin_end_chars[1] |
| 82 | + def _message_end_chars(self, header=False): |
| 83 | + return self._MESSAGE_CORNER_CHARS if header is True else [self._MESSAGE_BAR_CHAR] * 2 |
| 84 | + |
| 85 | + def _message_line(self, path, type, header=False): |
| 86 | + begin_char, end_char = self._message_end_chars(header) |
| 87 | + return self._MESSAGE_FORMAT.format(begin_char, type, path) \ |
| 88 | + .ljust(self._MESSAGE_MAX_LENGTH - 1, self._MESSAGE_BAR_CHAR) + end_char |
| 89 | + |
| 90 | + if len(_MESSAGE_BAR_CHAR) != 1: |
| 91 | + # The code in this if-block is for Python interpreters that don't |
| 92 | + # treat multibyte Unicode characters as single characters. |
| 93 | + # E.g., some versions of Python 2.x. This block may be removed |
| 94 | + # when support for those interpreters is no longer needed. |
| 95 | + |
| 96 | + _FORMAT_LENGTH = len( |
| 97 | + _MESSAGE_FORMAT.replace(_MESSAGE_BAR_CHAR, '.').format('.', '', '')) + 1 |
86 | 98 |
|
87 |
| - def _simple_msg_v2(self, path, type, header=False): |
88 |
| - begin_end_chars = ('╒', '╕') if header is True else ('═', '═') |
| 99 | + def _message_line(self, path, type, header=False): |
| 100 | + begin_char, end_char = self._message_end_chars(header) |
89 | 101 |
|
90 |
| - # printed length of the static charaters: left end, brackets, bar characters |
91 |
| - format_length = 11 # TODO: calculate fixed chars printed length |
92 |
| - desired_length = self._WIDTH - len(type) - len(path) - format_length |
| 102 | + bar_length = self._MESSAGE_MAX_LENGTH - len(type) - len(path) - self._FORMAT_LENGTH |
93 | 103 |
|
94 |
| - return self._MESSAGE_FORMAT.format(begin_end_chars[0], type, path) + \ |
95 |
| - self._HEADER_LINE * desired_length + begin_end_chars[1] |
| 104 | + return self._MESSAGE_FORMAT.format(begin_char, type, path) + \ |
| 105 | + self._MESSAGE_BAR_CHAR * bar_length + end_char |
96 | 106 |
|
97 |
| - _simple_msg = _simple_msg_v3 if len(_HEADER_LINE) == 1 else _simple_msg_v2 |
98 | 107 |
|
99 | 108 | def _error_msg(self, path, type, body):
|
100 |
| - HEADER = self._simple_msg(path, type, header=True) |
101 |
| - FOOTER = '└' + '─' * (self._WIDTH - 2) + '┘' |
| 109 | + HEADER = self._message_line(path, type, header=True) |
| 110 | + FOOTER = '└' + '─' * (self._MESSAGE_MAX_LENGTH - 2) + '┘' |
102 | 111 |
|
103 | 112 | return '\n'.join((HEADER, str(body), FOOTER, '\n'))
|
104 | 113 |
|
@@ -128,7 +137,7 @@ def validation_error(self, instance_path, error):
|
128 | 137 | )
|
129 | 138 |
|
130 | 139 | def validation_success(self, instance_path):
|
131 |
| - return self._simple_msg(path=instance_path, type='SUCCESS') + '\n\n' |
| 140 | + return self._message_line(path=instance_path, type='SUCCESS') + '\n\n' |
132 | 141 |
|
133 | 142 |
|
134 | 143 | @attr.s
|
|
0 commit comments