<commit_before>from pyramid.tweens import EXCVIEW
from pyramid.httpexceptions import WSGIHTTPException
from raven import Client


def crow_tween_factory(handler, registry):
    def crow_tween(request):
        try:
            return handler(request)
        except WSGIHTTPException:
            raise
        except:
            request.raven.captureException()
            raise

    return crow_tween


def _request_to_http_context(request):
    return {
        'method': request.method,
        'url': request.path_url,
        'query_string': request.query_string,
        'data': (
            request.body
            if request.content_length < 2**16
            else 'Over-long request body of {} bytes omitted'.format(
                request.content_length
            )
        ),
        'headers': dict(request.headers),
        'env': dict(request.environ),
    }


def raven_client(request):
    """Configure a raven client from pyramid config file, add request context

    Uses keys from the pyramid config file beginning with raven. and turns each
    into a kwarg to raven.Client. Also produces http context from request and
    adds to client's context"""
    kwargs = {}

    for key in request.registry.settings:
        if key.startswith('raven.'):
            kwarg = key.partition('.')[2]
            kwargs[kwarg] = request.registry.settings[key]

    client = Client(**kwargs)
    client.http_context(_request_to_http_context(request))

    return client


def includeme(config):
    config.add_request_method(raven_client, 'raven', reify=True)
    config.add_tween('pyramid_crow.crow_tween_factory', under=EXCVIEW)<commit_msg>Always sanitize passwords, set generous timeout<commit_after>from pyramid.tweens import EXCVIEW
from pyramid.httpexceptions import WSGIHTTPException
from raven import Client


def crow_tween_factory(handler, registry):
    def crow_tween(request):
        try:
            return handler(request)
        except WSGIHTTPException:
            raise
        except:
            request.raven.captureException()
            raise

    return crow_tween


def _request_to_http_context(request):
    return {
        'method': request.method,
        'url': request.path_url,
        'query_string': request.query_string,
        'data': (
            request.body
            if request.content_length < 2**16
            else 'Over-long request body of {} bytes omitted'.format(
                request.content_length
            )
        ),
        'headers': dict(request.headers),
        'env': dict(request.environ),
    }


def raven_client(request):
    """Configure a raven client from pyramid config file, add request context

    Uses keys from the pyramid config file beginning with raven. and turns each
    into a kwarg to raven.Client. Also produces http context from request and
    adds to client's context"""
    kwargs = {
        'processors': (
            'raven.processors.SanitizePasswordsProcessor',
        ),
        'timeout': 4,
    }

    for key in request.registry.settings:
        if key.startswith('raven.'):
            kwarg = key.partition('.')[2]
            kwargs[kwarg] = request.registry.settings[key]

    client = Client(**kwargs)
    client.http_context(_request_to_http_context(request))

    return client


def includeme(config):
    config.add_request_method(raven_client, 'raven', reify=True)
    config.add_tween('pyramid_crow.crow_tween_factory', under=EXCVIEW)
{'fraction_non_alphanumeric': 0.08936292879792447, 'fraction_numerical': 0.0025944076102623233, 'mean_word_length': 2.5884177869700102, 'pattern_counts': {'":': 0, '<': 5, '<?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': '205574', 'n_tokens_mistral': 1010, 'n_tokens_neox': 959, 'n_words': 246}