<commit_before>from functools import wraps

from flask import current_app, render_template, Blueprint, abort, url_for, redirect, \
    g

import sys
from jinja2 import PackageLoader, ChoiceLoader


dbg = Blueprint('debug', __name__, template_folder='templates')


def load_plugins():
    # load plugins once
    if hasattr(dbg, '_flask_debug'):
        return

    dbg._flask_debug = {
        'extensions': {},
    }

    for name, mod in sys.modules.items():
        if (name.startswith('flask_debug_') and
                hasattr(mod, 'initialize_debug_ext')):
            mod.initialize_debug_ext(dbg)
            dbg._flask_debug['extensions'][name] = mod

    # collect loaders
    loaders = [dbg.jinja_loader]
    for name, mod in dbg._flask_debug['extensions'].items():
        template_folder = getattr(mod, 'template_folder', None)
        if template_folder:
            loaders.append(PackageLoader(name, template_folder))

    # replace blueprints loader with new loader that includes extensions
    dbg.jinja_loader = ChoiceLoader(loaders)


def requires_debug(view):
    @wraps(view)
    def _(*args, **kwargs):
        if not current_app.debug:
            abort(403, 'This function is only available if the application '
                       'has been started in debug mode.')
        return view(*args, **kwargs)
    return _


@dbg.route('/_debug/')  # the root
@requires_debug
def debug_root():
    return redirect(url_for('.debug_reflect'))


@dbg.route('/_reflect/')
@requires_debug
def debug_reflect():
    return render_template(
        'flask_debug/reflect.html',
        app=current_app,
    )


@dbg.route('/_config/')
@requires_debug
def debug_config():
    return render_template(
        'flask_debug/config.html',
        app=current_app,
    )


@dbg.before_request
def make_current_app_available():
    g.app = current_app


class Debug(object):
    def __init__(self, app=None):
        import flask_debug_extensions
        load_plugins()
        if app:
            self.init_app(app)

    def init_app(self, app):
        app.register_blueprint(dbg)
<commit_msg>Use special blueprint class; add menu-support.<commit_after>from collections import OrderedDict
from functools import wraps

from flask import current_app, render_template, Blueprint, abort, url_for, redirect, \
    g

import sys
from jinja2 import PackageLoader, ChoiceLoader



class DebugBlueprint(Blueprint):
    def __init__(self, *args, **kwargs):
        super(DebugBlueprint, self).__init__(*args, **kwargs)
        self.__menu = OrderedDict()
        self.__plugins = None

    def _debug_load_plugins(self):
        if self.__plugins is not None:
            # already loaded
            return

        dbg.__plugins = {}

        for name, mod in sys.modules.items():
            if (name.startswith('flask_debug_') and
                    hasattr(mod, 'initialize_debug_ext')):
                mod.initialize_debug_ext(dbg)
                dbg.__plugins[name] = mod

        # collect loaders
        loaders = [dbg.jinja_loader]
        for name, mod in dbg.__plugins.items():
            template_folder = getattr(mod, 'template_folder', None)
            if template_folder:
                loaders.append(PackageLoader(name, template_folder))

        # replace blueprints loader with new loader that includes extensions
        dbg.jinja_loader = ChoiceLoader(loaders)

    def route(self, rule, **options):
        on_menu = options.pop('on_menu', True)
        wrapper = super(DebugBlueprint, self).route(rule, **options)

        @wraps(wrapper)
        def _(f):
            endpoint = options.get('endpoint', f.__name__)
            wrapped = wrapper(f)
            self.__menu[endpoint] = wrapped
            return wrapped

        return _


dbg = DebugBlueprint('debug', __name__, template_folder='templates')


def requires_debug(view):
    @wraps(view)
    def _(*args, **kwargs):
        if not current_app.debug:
            abort(403, 'This function is only available if the application '
                       'has been started in debug mode.')
        return view(*args, **kwargs)
    return _


@dbg.route('/_debug/')  # the root
@requires_debug
def debug_root():
    return redirect(url_for('.debug_reflect'))


@dbg.route('/_reflect/')
@requires_debug
def debug_reflect():
    return render_template(
        'flask_debug/reflect.html',
        app=current_app,
    )


@dbg.route('/_config/')
@requires_debug
def debug_config():
    return render_template(
        'flask_debug/config.html',
        app=current_app,
    )


@dbg.before_request
def make_current_app_available():
    g.app = current_app


class Debug(object):
    def __init__(self, app=None):
        import flask_debug_extensions  # import plugin-list plugin
        dbg._debug_load_plugins()
        if app:
            self.init_app(app)

    def init_app(self, app):
        app.register_blueprint(dbg)

{'fraction_non_alphanumeric': 0.10335707019328585, 'fraction_numerical': 0.0016276703967446592, 'mean_word_length': 2.718608169440242, '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': '6576737', 'n_tokens_mistral': 1479, 'n_tokens_neox': 1418, 'n_words': 347}