<commit_before># -*- coding: utf-8 -*-
"""
    weppy_sentry.ext
    ----------------

    Provides sentry extension for weppy

    :copyright: (c) 2016 by <NAME>
    :license: BSD, see LICENSE for more details.
"""

import raven
import sys
from weppy._compat import iteritems
from weppy.extensions import Extension, listen_signal
from weppy.globals import current
from weppy.pipeline import Pipeline, Pipe


class Sentry(Extension):
    client = None
    default_config = dict(
        dsn='',
        auto_load=True
    )

    _errmsg = "You need to configure Sentry extension before using its methods"

    def on_load(self):
        self.handler = SentryPipe(self)
        if not self.config.dsn:
            return
        self.client = raven.Client(self.config.dsn)

    @listen_signal('after_route')
    def inject_pipeline(self, route):
        if not self.config.auto_load:
            return
        injection_pipeline = [self.pipe]
        rewrap = Pipeline(
            injection_pipeline)(route.f)
        route.f = rewrap
        route.pipeline = injection_pipeline + route.pipeline

    def _build_ctx_data(self):
        ctx = {'extra': {}}
        try:
            ctx['request'] = Serializer.serialize_request(current.request)
            ctx['extra']['response'] = \
                Serializer.serialize_response(current.response)
            ctx['extra']['session'] = \
                Serializer.serialize_session(current.session)
            ctx['user'] = Serializer.serialize_user(current.session)
        except:
            pass
        return ctx

    def exception(self, **kwargs):
        assert self.client, self._errmsg
        self.client.captureException(
            sys.exc_info(), data=self._build_ctx_data(), **kwargs)<commit_msg>Renamed handler attribute to pipe<commit_after># -*- coding: utf-8 -*-
"""
    weppy_sentry.ext
    ----------------

    Provides sentry extension for weppy

    :copyright: (c) 2016 by <NAME>
    :license: BSD, see LICENSE for more details.
"""

import raven
import sys
from weppy._compat import iteritems
from weppy.extensions import Extension, listen_signal
from weppy.globals import current
from weppy.pipeline import Pipeline, Pipe


class Sentry(Extension):
    client = None
    default_config = dict(
        dsn='',
        auto_load=True
    )

    _errmsg = "You need to configure Sentry extension before using its methods"

    def on_load(self):
        self.pipe = SentryPipe(self)
        if not self.config.dsn:
            return
        self.client = raven.Client(self.config.dsn)

    @listen_signal('after_route')
    def inject_pipeline(self, route):
        if not self.config.auto_load:
            return
        injection_pipeline = [self.pipe]
        rewrap = Pipeline(
            injection_pipeline)(route.f)
        route.f = rewrap
        route.pipeline = injection_pipeline + route.pipeline

    def _build_ctx_data(self):
        ctx = {'extra': {}}
        try:
            ctx['request'] = Serializer.serialize_request(current.request)
            ctx['extra']['response'] = \
                Serializer.serialize_response(current.response)
            ctx['extra']['session'] = \
                Serializer.serialize_session(current.session)
            ctx['user'] = Serializer.serialize_user(current.session)
        except:
            pass
        return ctx

    def exception(self, **kwargs):
        assert self.client, self._errmsg
        self.client.captureException(
            sys.exc_info(), data=self._build_ctx_data(), **kwargs)
{'fraction_non_alphanumeric': 0.1093882153932901, 'fraction_numerical': 0.00281928390188892, 'mean_word_length': 2.325210871602624, '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': '7088470', 'n_tokens_mistral': 1015, 'n_tokens_neox': 982, 'n_words': 237}