<commit_before>"""
Internal subroutines for e.g. aborting execution with an error message,
or performing indenting on multiline output.
"""
import os
import sys
import textwrap
from traceback import format_exc


def _encode(msg, stream):
    if isinstance(msg, unicode) and hasattr(stream, 'encoding') and not stream.encoding is None:
        return msg.encode(stream.encoding)
    else:
        return str(msg)


def isatty(stream):
    """Check if a stream is a tty.

    Not all file-like objects implement the `isatty` method.
    """
    fn = getattr(stream, 'isatty', None)
    if fn is None:
        return False
    return fn()


def abort(msg):
    """
    Abort execution, print ``msg`` to stderr and exit with error status (1.)

    This function currently makes use of `sys.exit`_, which raises
    `SystemExit`_. Therefore, it's possible to detect and recover from inner
    calls to `abort` by using ``except SystemExit`` or similar.

    .. _sys.exit: http://docs.python.org/library/sys.html#sys.exit
    .. _SystemExit: http://docs.python.org/library/exceptions.html#exceptions.SystemExit
    """
    from fabric.state import output, env
    if not env.colorize_errors:
        red  = lambda x: x
    else:
        from colors import red

    if output.aborts:
        sys.stderr.write(red("\nFatal error: %s\n" % _encode(msg, sys.stderr)))
        sys.stderr.write(red("\nAborting.\n"))

    if env.abort_exception:
        raise env.abort_exception(msg)
    else:
        sys.exit(msg)


def warn(msg):
    """
    Print warning message, but do not abort execution.

    This function honors Fabric's :doc:`output controls
    <../../usage/output_controls>` and will print the given ``msg`` to stderr,
    provided that the ``warnings`` output level (which is active by default) is
    turned on.
    """
    from fabric.state import output, env

    if not env.colorize_errors:
        magenta = lambda x: x
    else:
        from colors import magenta

    if output.warnings:
        msg = _encode(msg, sys.stderr)
        sys.stderr.write(magenta("\nWarning: %s\n\n" % msg))


def indent(text, spaces=4, strip=False):
    """
    Return ``text`` indented by the given number of spaces.<commit_msg>Fix #1318<commit_after>"""
Internal subroutines for e.g. aborting execution with an error message,
or performing indenting on multiline output.
"""
import os
import sys
import textwrap
from traceback import format_exc


def _encode(msg, stream):
    if isinstance(msg, unicode) and hasattr(stream, 'encoding') and not stream.encoding is None:
        return msg.encode(stream.encoding)
    else:
        return str(msg)


def isatty(stream):
    """Check if a stream is a tty.

    Not all file-like objects implement the `isatty` method.
    """
    fn = getattr(stream, 'isatty', None)
    if fn is None:
        return False
    return fn()


def abort(msg):
    """
    Abort execution, print ``msg`` to stderr and exit with error status (1.)

    This function currently makes use of `SystemExit`_ in a manner that is
    similar to `sys.exit`_ (but which skips the automatic printing to stderr,
    allowing us to more tightly control it via settings).

    Therefore, it's possible to detect and recover from inner calls to `abort`
    by using ``except SystemExit`` or similar.

    .. _sys.exit: http://docs.python.org/library/sys.html#sys.exit
    .. _SystemExit: http://docs.python.org/library/exceptions.html#exceptions.SystemExit
    """
    from fabric.state import output, env
    if not env.colorize_errors:
        red  = lambda x: x
    else:
        from colors import red

    if output.aborts:
        sys.stderr.write(red("\nFatal error: %s\n" % _encode(msg, sys.stderr)))
        sys.stderr.write(red("\nAborting.\n"))

    if env.abort_exception:
        raise env.abort_exception(msg)
    else:
        # See issue #1318 for details on the below; it lets us construct a
        # valid, useful SystemExit while sidestepping the automatic stderr
        # print (which would otherwise duplicate with the above in a
        # non-controllable fashion).
        e = SystemExit(1)
        e.message = msg
        raise e


def warn(msg):
    """
    Print warning message, but do not abort execution.

    This function honors Fabric's :doc:`output controls
    <../../usage/output_controls>` and will print the given ``msg`` to stderr,
    provided that the ``warnings`` output level (which is active by default) is
    turned on.
    """
    from fabric.state import output, env

    if not env.colorize_errors:
        magenta = lambda x: x
    else:
        from colors import magenta

    if output.warnings:
        msg = _encode(msg, sys.stderr)
        sys.stderr.write(magenta("\nWarning: %s\n\n" % msg))


def indent(text, spaces=4, strip=False):
    """
    Return ``text`` indented by the given number of spaces.
{'fraction_non_alphanumeric': 0.10361842105263158, 'fraction_numerical': 0.0026726973684210527, 'mean_word_length': 2.8097102584181677, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6150978', 'n_tokens_mistral': 1460, 'n_tokens_neox': 1421, 'n_words': 518}