<commit_before># -*- coding: utf-8 -*-
"""
    flaskbb.app
    ~~~~~~~~~~~~~~~~~~~~

    manages the app creation and configuration process

    :copyright: (c) 2013 by the FlaskBB Team.
    :license: BSD, see LICENSE for more details.
"""
import os
import logging

from flask import Flask, render_template
from flask.ext.login import current_user
from flask_debugtoolbar import DebugToolbarExtension

# Import the user blueprint
from flaskbb.user.views import user
from flaskbb.user.models import User, Guest
# Import the auth blueprint
from flaskbb.auth.views import auth
# Import the admin blueprint
from flaskbb.admin.views import admin
# Import the PM blueprint
from flaskbb.pms.views import pms
# Import the forum blueprint
from flaskbb.forum.views import forum
from flaskbb.forum.models import *

from flaskbb.extensions import db, login_manager, mail, cache #toolbar
from flaskbb.helpers import time_delta_format, last_seen, can_moderate


DEFAULT_BLUEPRINTS = (
    (forum, ""),
    (auth, ""),
    (user, "/u"),
    (pms, "/pm"),
    (admin, "/admin")
)


def create_app(config=None, blueprints=None):
    """
    Creates the app.
    """

    if blueprints is None:
        blueprints = DEFAULT_BLUEPRINTS

    # Initialize the app
    app = Flask("flaskbb")

    configure_app(app, config)
    configure_extensions(app)
    configure_blueprints(app, blueprints)
    configure_template_filters(app)
    configure_before_handlers(app)
    configure_errorhandlers(app)
    configure_logging(app)

    return app


def configure_app(app, config):
    """
    Configures the app. If no configuration file is choosen,
    the app will use the example configuration.
    """

    # Get the configuration file
    if config is None:
        from flaskbb.configs.development import DevelopmentConfig
        app.config.from_object(DevelopmentConfig)
        app.logger.info("No configuration specified. Using Development config")
    else:
        app.config.from_object(config)


def configure_extensions(app):
    """
    Configures the extensions
    """

    # Flask-SQLAlchemy
    db.init_app(app)

    # Flask-Mail
    mail.init_app(app)

    # Flask-Cache
    cache.init_app(app)

    # Flask-Debugtoolbar
    toolbar = DebugToolbarExtension(app)

    # Flask-Login
    login_manager.login_view = app.config["LOGIN_VIEW"]
    login_manager.refresh_view = app.config["REAUTH_VIEW"]
    login_manager.anonymous_user = Guest

    @login_manager.user_loader
    def load_user(id):
        """
        Loads the user. Required by the `login` extension
        """
        return User.query.get(id)

    login_manager.init_app(app)


def configure_blueprints(app, blueprints):
    """
    Configures the blueprints
    """
<commit_msg>A note how to reduce the queries later.. <commit_after># -*- coding: utf-8 -*-
"""
    flaskbb.app
    ~~~~~~~~~~~~~~~~~~~~

    manages the app creation and configuration process

    :copyright: (c) 2013 by the FlaskBB Team.
    :license: BSD, see LICENSE for more details.
"""
import os
import logging

from flask import Flask, render_template
from flask.ext.login import current_user
from flask_debugtoolbar import DebugToolbarExtension

# Import the user blueprint
from flaskbb.user.views import user
from flaskbb.user.models import User, Guest
# Import the auth blueprint
from flaskbb.auth.views import auth
# Import the admin blueprint
from flaskbb.admin.views import admin
# Import the PM blueprint
from flaskbb.pms.views import pms
# Import the forum blueprint
from flaskbb.forum.views import forum
from flaskbb.forum.models import *

from flaskbb.extensions import db, login_manager, mail, cache #toolbar
from flaskbb.helpers import time_delta_format, last_seen, can_moderate


DEFAULT_BLUEPRINTS = (
    (forum, ""),
    (auth, ""),
    (user, "/u"),
    (pms, "/pm"),
    (admin, "/admin")
)


def create_app(config=None, blueprints=None):
    """
    Creates the app.
    """

    if blueprints is None:
        blueprints = DEFAULT_BLUEPRINTS

    # Initialize the app
    app = Flask("flaskbb")

    configure_app(app, config)
    configure_extensions(app)
    configure_blueprints(app, blueprints)
    configure_template_filters(app)
    configure_before_handlers(app)
    configure_errorhandlers(app)
    configure_logging(app)

    return app


def configure_app(app, config):
    """
    Configures the app. If no configuration file is choosen,
    the app will use the example configuration.
    """

    # Get the configuration file
    if config is None:
        from flaskbb.configs.development import DevelopmentConfig
        app.config.from_object(DevelopmentConfig)
        app.logger.info("No configuration specified. Using Development config")
    else:
        app.config.from_object(config)


def configure_extensions(app):
    """
    Configures the extensions
    """

    # Flask-SQLAlchemy
    db.init_app(app)

    # Flask-Mail
    mail.init_app(app)

    # Flask-Cache
    cache.init_app(app)

    # Flask-Debugtoolbar
    toolbar = DebugToolbarExtension(app)

    # Flask-Login
    login_manager.login_view = app.config["LOGIN_VIEW"]
    login_manager.refresh_view = app.config["REAUTH_VIEW"]
    login_manager.anonymous_user = Guest

    @login_manager.user_loader
    def load_user(id):
        """
        Loads the user. Required by the `login` extension
        """
        # TODO: Also load the permissions and
        # the unread pm count with 1 big query
        return User.query.get(id)

    login_manager.init_app(app)


def configure_blueprints(app, blueprints):
    """
    Configures the blueprints
    """

{'fraction_non_alphanumeric': 0.09483836399357028, 'fraction_numerical': 0.0019646365422396855, 'mean_word_length': 3.317656129529684, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4351479', 'n_tokens_mistral': 1726, 'n_tokens_neox': 1636, 'n_words': 466}