<commit_before># -*- coding: utf-8 -*-

import six
from flask import g
from flask import request

from nplusone.core import signals
from nplusone.core import listeners
from nplusone.core import notifiers
import nplusone.ext.sqlalchemy  # noqa


def get_worker():
    try:
        return request._get_current_object()
    except RuntimeError:
        return None


def setup_state():
    signals.get_worker = get_worker
setup_state()


class NPlusOne(object):

    def __init__(self, app):
        self.app = app
        self.init_app()

    def load_config(self):
        self.notifiers = notifiers.init(self.app.config)
        self.whitelist = [
            listeners.Rule(**item)
            for item in self.app.config.get('NPLUSONE_WHITELIST', [])
        ]

    def init_app(self):
        @self.app.before_request
        def connect():
            self.load_config()
            g.listeners = getattr(g, 'listeners', {})
            for name, listener_type in six.iteritems(listeners.listeners):
                g.listeners[name] = listener_type(self)
                g.listeners[name].setup()

        @self.app.after_request
        def disconnect(response):
            for name, listener_type in six.iteritems(listeners.listeners):
                listener = g.listeners.pop(name)
                listener.teardown()
            return response

    def notify(self, message):
        if not message.match(self.whitelist):
            for notifier in self.notifiers:
                notifier.notify(message)
<commit_msg>Improve Flask APIs<commit_after># -*- coding: utf-8 -*-
import six

from flask import g
from flask import request

import nplusone.ext.sqlalchemy  # noqa

from nplusone.core import signals
from nplusone.core import listeners
from nplusone.core import notifiers


def get_worker():
    try:
        return request._get_current_object()
    except RuntimeError:
        return None


def setup_state():
    signals.get_worker = get_worker


setup_state()


class NPlusOne(object):
    app = None

    def __init__(self, app=None):
        if app:
            self.init_app(app)

    def load_config(self):
        self.notifiers = notifiers.init(self.app.config)
        self.whitelist = [
            listeners.Rule(**item)
            for item in self.app.config.get('NPLUSONE_WHITELIST', [])
        ]

    def init_app(self, app):
        self.app = app

        @app.before_request
        def connect():
            self.load_config()
            g.listeners = getattr(g, 'listeners', {})
            for name, listener_type in six.iteritems(listeners.listeners):
                g.listeners[name] = listener_type(self)
                g.listeners[name].setup()

        @app.after_request
        def disconnect(response):
            for name, listener_type in six.iteritems(listeners.listeners):
                listener = g.listeners.pop(name, None)
                if listener:
                    listener.teardown()
            return response

    def notify(self, message):
        if not message.match(self.whitelist):
            for notifier in self.notifiers:
                notifier.notify(message)

    def ignore(self, signal):
        return signals.ignore(getattr(signals, signal))

{'fraction_non_alphanumeric': 0.09576768612913192, 'fraction_numerical': 0.0006178560395427865, 'mean_word_length': 2.193293885601578, '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': '2864876', 'n_tokens_mistral': 966, 'n_tokens_neox': 929, 'n_words': 218}