<commit_before>import json

from globus_sdk.base import safe_stringify
from globus_cli.helpers import outformat_is_json
from globus_cli.safeio.write import safeprint


class PrintableErrorField(object):
    """
    A glorified tuple with a kwarg in its constructor.
    Coerces name and value fields to unicode for output consistency
    """
    TEXT_PREFIX = 'Globus CLI Error:'

    def __init__(self, name, value, multiline=False):
        self.name = safe_stringify(name)
        self.value = safe_stringify(value)
        self.multiline = multiline
        self._format_value()

    @property
    def _text_prefix_len(self):
        return len(self.TEXT_PREFIX)

    def _format_value(self):
        """
        formats self.value to be good for textmode printing
        self.value must be unicode before this is called, and will remain so
        """
        name = self.name + ':'
        if not self.multiline or '\n' not in self.value:
            self.value = u'{0} {1}'.format(name.ljust(self._text_prefix_len),
                                           self.value)
        else:
            spacer = '\n' + ' '*(self._text_prefix_len + 1)
            self.value = u'{0}{1}{2}'.format(
                name, spacer, spacer.join(self.value.split('\n')))


def write_error_info(error_name, fields, message=None):

    if outformat_is_json():
        # dictify joined tuple lists and dump to json string
        message = json.dumps(
            dict(
                [('error_name', error_name)] +
                [(f.name, f.value) for f in fields]),
            indent=2)
    if not message:
        message = u'A{0} {1} Occurred.\n{2}'.format(
            "n" if error_name[0] in "aeiouAEIOU" else "",
            error_name, '\n'.join(f.value for f in fields))
        message = u'{0} {1}'.format(PrintableErrorField.TEXT_PREFIX, message)

    safeprint(message, write_to_stderr=True)<commit_msg>Fix JSON rendering of error output<commit_after>import json

from globus_sdk.base import safe_stringify
from globus_cli.helpers import outformat_is_json
from globus_cli.safeio.write import safeprint


class PrintableErrorField(object):
    """
    A glorified tuple with a kwarg in its constructor.
    Coerces name and value fields to unicode for output consistency
    """
    TEXT_PREFIX = 'Globus CLI Error:'

    def __init__(self, name, value, multiline=False):
        self.name = safe_stringify(name)
        self.raw_value = safe_stringify(value)
        self.value = self.raw_value
        self.multiline = multiline
        self._format_value()

    @property
    def _text_prefix_len(self):
        return len(self.TEXT_PREFIX)

    def _format_value(self):
        """
        formats self.value to be good for textmode printing
        self.value must be unicode before this is called, and will remain so
        """
        name = self.name + ':'
        if not self.multiline or '\n' not in self.value:
            self.value = u'{0} {1}'.format(name.ljust(self._text_prefix_len),
                                           self.value)
        else:
            spacer = '\n' + ' '*(self._text_prefix_len + 1)
            self.value = u'{0}{1}{2}'.format(
                name, spacer, spacer.join(self.value.split('\n')))


def write_error_info(error_name, fields, message=None):

    if outformat_is_json():
        # dictify joined tuple lists and dump to json string
        message = json.dumps(
            dict(
                [('error_name', error_name)] +
                [(f.name, f.raw_value) for f in fields]),
            indent=2)
    if not message:
        message = u'A{0} {1} Occurred.\n{2}'.format(
            "n" if error_name[0] in "aeiouAEIOU" else "",
            error_name, '\n'.join(f.value for f in fields))
        message = u'{0} {1}'.format(PrintableErrorField.TEXT_PREFIX, message)

    safeprint(message, write_to_stderr=True)
{'fraction_non_alphanumeric': 0.1077952148186262, 'fraction_numerical': 0.006688963210702341, 'mean_word_length': 2.3145780051150897, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4198266', 'n_tokens_mistral': 1211, 'n_tokens_neox': 1161, 'n_words': 314}