<commit_before>#TODO, figure out how to load gevent monkey patch only in production
# try:
#     from gevent.monkey import patch_all
#     patch_all()
# except ImportError:
#     if not DEBUG:
#         print "unable to apply gevent monkey.patch_all"

import logging

from flask import Flask, g, request, session
from flask.ext.assets import Bundle

from utils import json_markup, OrderedDictYAMLLoader
import yaml

from .config import DefaultConfig

from .site import site
from .admin import admin
from .user import User, user
from .call import call
from .campaign import campaign
from .api import api

from extensions import cache, db, babel, assets, login_manager, csrf, mail, store

DEFAULT_BLUEPRINTS = (
    site,
    admin,
    user,
    call,
    campaign,
    api
)


def create_app(config=None, app_name=None, blueprints=None):
    """Create the main Flask app."""

    if app_name is None:
        app_name = DefaultConfig.APP_NAME
    if blueprints is None:
        blueprints = DEFAULT_BLUEPRINTS

    app = Flask(app_name)
    # configure app from object or environment
    configure_app(app, config)
    # init extensions once we have app context
    init_extensions(app)
    # then blueprints, for url/view routing
    register_blueprints(app, blueprints)

    configure_logging(app)

    # then extension specific configurations
    configure_babel(app)
    configure_login(app)
    configure_assets(app)

    # finally instance specific configurations
    context_processors(app)
    instance_defaults(app)

    app.logger.info('Call Power started')
    app.logger.info('db at %s' % db.engine.url)
    return app


def configure_app(app, config=None):
    """Configure app by object, instance folders or environment variables"""

    # http://flask.pocoo.org/docs/api/#configuration
    app.config.from_object(DefaultConfig)
    if config:
        app.config.from_object(config)

    # http://flask.pocoo.org/docs/config/#instance-folders
    # app.config.from_pyfile('instance/app.cfg')

    # app.config.from_envvar('%s_APP_CONFIG' % DefaultConfig.PROJECT.upper(), silent=True)


def init_extensions(app):
    db.init_app(app)
    db.app = app

    assets.init_app(app)
    babel.init_app(app)
    cache.init_app(app)
    csrf.init_app(app)
    mail.init_app(app)
    login_manager.init_app(app)
    store.init_app(app)

    if app.config.get('DEBUG'):
        from flask_debugtoolbar import DebugToolbarExtension
        DebugToolbarExtension(app)


def register_blueprints(app, blueprints):
    for blueprint in blueprints:
        app.register_blueprint(blueprint)


def configure_babel(app):
    @babel.localeselector
    def get_locale():
        #TODO, first check user config?
        g.accept_languages = app.config.get('ACCEPT_LANGUAGES')
        accept_languages = g.accept_languages.keys()
        browser_default = request.accept_languages.best_match(accept_languages)
        if 'language' in session:
            language = session['language']
            #current_app.logger.debug('lang from session: %s' % language)
            if not language in accept_languages:
                #clear it
                #current_app.logger.debug('invalid %s, clearing' % language)
                session['language'] = None
                language = browser_default
        else:
            language = browser_default
            #current_app.logger.debug('lang from browser: %s' % language)
        session['language'] = language  # save it to session

        #and to user model?
        return language


def configure_login(app):
    login_manager.login_view = 'user.login'
    login_manager.refresh_view = 'user.reauth'
    login_manager.session_protection = 'basic'
<commit_msg>pep flake<commit_after># TODO, figure out how to load gevent monkey patch only in production
# try:
#     from gevent.monkey import patch_all
#     patch_all()
# except ImportError:
#     if not DEBUG:
#         print "unable to apply gevent monkey.patch_all"

import logging

from flask import Flask, g, request, session
from flask.ext.assets import Bundle

from utils import json_markup, OrderedDictYAMLLoader
import yaml

from .config import DefaultConfig

from .site import site
from .admin import admin
from .user import User, user
from .call import call
from .campaign import campaign
from .api import api

from extensions import cache, db, babel, assets, login_manager, csrf, mail, store

DEFAULT_BLUEPRINTS = (
    site,
    admin,
    user,
    call,
    campaign,
    api
)


def create_app(config=None, app_name=None, blueprints=None):
    """Create the main Flask app."""

    if app_name is None:
        app_name = DefaultConfig.APP_NAME
    if blueprints is None:
        blueprints = DEFAULT_BLUEPRINTS

    app = Flask(app_name)
    # configure app from object or environment
    configure_app(app, config)
    # init extensions once we have app context
    init_extensions(app)
    # then blueprints, for url/view routing
    register_blueprints(app, blueprints)

    configure_logging(app)

    # then extension specific configurations
    configure_babel(app)
    configure_login(app)
    configure_assets(app)

    # finally instance specific configurations
    context_processors(app)
    instance_defaults(app)

    app.logger.info('Call Power started')
    app.logger.info('db at %s' % db.engine.url)
    return app


def configure_app(app, config=None):
    """Configure app by object, instance folders or environment variables"""

    # http://flask.pocoo.org/docs/api/#configuration
    app.config.from_object(DefaultConfig)
    if config:
        app.config.from_object(config)

    # http://flask.pocoo.org/docs/config/#instance-folders
    # app.config.from_pyfile('instance/app.cfg')

    # app.config.from_envvar('%s_APP_CONFIG' % DefaultConfig.PROJECT.upper(), silent=True)


def init_extensions(app):
    db.init_app(app)
    db.app = app

    assets.init_app(app)
    babel.init_app(app)
    cache.init_app(app)
    csrf.init_app(app)
    mail.init_app(app)
    login_manager.init_app(app)
    store.init_app(app)

    if app.config.get('DEBUG'):
        from flask_debugtoolbar import DebugToolbarExtension
        DebugToolbarExtension(app)


def register_blueprints(app, blueprints):
    for blueprint in blueprints:
        app.register_blueprint(blueprint)


def configure_babel(app):
    @babel.localeselector
    def get_locale():
        # TODO, first check user config?
        g.accept_languages = app.config.get('ACCEPT_LANGUAGES')
        accept_languages = g.accept_languages.keys()
        browser_default = request.accept_languages.best_match(accept_languages)
        if 'language' in session:
            language = session['language']
            # current_app.logger.debug('lang from session: %s' % language)
            if language not in accept_languages:
                # clear it
                # current_app.logger.debug('invalid %s, clearing' % language)
                session['language'] = None
                language = browser_default
        else:
            language = browser_default
            # current_app.logger.debug('lang from browser: %s' % language)
        session['language'] = language  # save it to session

        # and to user model?
        return language


def configure_login(app):
    login_manager.login_view = 'user.login'
    login_manager.refresh_view = 'user.reauth'
    login_manager.session_protection = 'basic'

{'fraction_non_alphanumeric': 0.08866729551273413, 'fraction_numerical': 0.0, 'mean_word_length': 3.1164725457570714, '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': '4779177', 'n_tokens_mistral': 2242, 'n_tokens_neox': 2179, 'n_words': 570}