<commit_before>

def remote_compile(client, fn):
    def outer(fn):
        from functools import wraps
        @wraps(fn)
        def inner(*args, **kwargs):
            class RemoteException(Exception):

                def __init__(self, remote_traceback, err):
                    self.err = err
                    self.remote_traceback = remote_traceback

            try:
                fn(*args, **kwargs)
            except Exception as err:
                import traceback
                remote_trace = traceback.format_exc()
                raise RemoteException(remote_trace.split('\n'), err)
        return inner
    return client.compile(outer)(client.compile(fn))<commit_msg>create a catches decorator to handle exceptions<commit_after>import logging
import sys
from functools import wraps


def remote_compile(client, fn):
    def outer(fn):
        from functools import wraps
        @wraps(fn)
        def inner(*args, **kwargs):
            class RemoteException(Exception):

                def __init__(self, remote_traceback, err):
                    self.err = err
                    self.remote_traceback = remote_traceback

            try:
                fn(*args, **kwargs)
            except Exception as err:
                import traceback
                remote_trace = traceback.format_exc()
                raise RemoteException(remote_trace.split('\n'), err)
        return inner
    return client.compile(outer)(client.compile(fn))


def catches(catch=None, handler=None, exit=True):
    """
    Very simple decorator that tries any of the exception(s) passed in as
    a single exception class or tuple (containing multiple ones) returning the
    exception message and optionally handling the problem if it rises with the
    handler if it is provided.

    So instead of douing something like this::

        def bar():
            try:
                some_call()
                print "Success!"
            except TypeError, exc:
                print "Error while handling some call: %s" % exc
                sys.exit(1)

    You would need to decorate it like this to have the same effect::

        @catches(TypeError)
        def bar():
            some_call()
            print "Success!"

    If multiple exceptions need to be caught they need to be provided as a
    tuple::

        @catches((TypeError, AttributeError))
        def bar():
            some_call()
            print "Success!"
    """
    catch = catch or Exception
    logger = logging.getLogger('ceph_deploy')

    def decorate(f):

        @wraps(f)
        def newfunc(*a, **kw):
            try:
                return f(*a, **kw)
            except catch as e:
                if handler:
                    return handler(e)
                else:
                    logger.error(make_exception_message(e))
                    if exit:
                        sys.exit(1)
        return newfunc

    return decorate

#
# Decorator helpers
#


def make_exception_message(exc):
    """
    An exception is passed in and this function
    returns the proper string depending on the result
    so it is readable enough.
    """
    if str(exc):
        return '%s: %s\n' % (exc.__class__.__name__, exc)
    else:
        return '%s\n' % (exc.__class__.__name__)

{'fraction_non_alphanumeric': 0.08206686930091185, 'fraction_numerical': 0.0006079027355623101, 'mean_word_length': 1.7678721614802355, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7033516', 'n_tokens_mistral': 870, 'n_tokens_neox': 823, 'n_words': 275}