Skip to content

Commit f618b7f

Browse files
committed
python-jsonschema#708 - conditional message code for old Python
For Python interpreters that don't treat multibyte Unicode characters as single characters, add some code to manually calculate the number of bar characters required to reach the desired line length. This code is confined to a conditional block and may be removed when support for older interpreters is no longer needed.
1 parent 55998e6 commit f618b7f

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

jsonschema/cli.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from jsonschema import __version__
1717
from jsonschema._reflect import namedAny
18-
from jsonschema.compat import PY3, JSONDecodeError
18+
from jsonschema.compat import JSONDecodeError
1919
from jsonschema.exceptions import SchemaError
2020
from jsonschema.validators import validator_for
2121

@@ -70,35 +70,44 @@ def validation_success(self, **kwargs):
7070

7171
@attr.s
7272
class _PrettyFormatter(object):
73-
74-
_WIDTH = 79
75-
_HEADER_LINE = '═'
73+
_MESSAGE_BAR_CHAR = '═'
74+
_MESSAGE_CORNER_CHARS = ('╒', '╕')
7675
_MESSAGE_FORMAT = '{}══[{}]═══({})'
76+
_MESSAGE_MAX_LENGTH = 79
7777

7878
@classmethod
7979
def _json_formatter(cls, x):
8080
return json.dumps(x, separators=(',\n', ': '), sort_keys=True)
8181

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
8698

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)
89101

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
93103

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
96106

97-
_simple_msg = _simple_msg_v3 if len(_HEADER_LINE) == 1 else _simple_msg_v2
98107

99108
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) + '┘'
102111

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

@@ -128,7 +137,7 @@ def validation_error(self, instance_path, error):
128137
)
129138

130139
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'
132141

133142

134143
@attr.s

0 commit comments

Comments
 (0)