<commit_before># -*- coding: utf-8 -*-
"""
    edacc.web
    ---------

    In this module the flask application instance is defined and configured
    according to the settings in config.py.

    :copyright: (c) 2010 by <NAME>.
    :license: MIT, see LICENSE for details.
"""

import uuid, datetime

from flask import Flask, Request, g
app = Flask(__name__)

from edacc import config, models
app.Debug = config.DEBUG

if config.LOGGING:
    # set up logging if configured
    import logging
    from logging.handlers import RotatingFileHandler
    file_handler = RotatingFileHandler(config.LOG_FILE)
    file_handler.setLevel(logging.WARNING)
    formatter = logging.Formatter("---------------------------\n" + \
                                  "%(asctime)s - %(name)s - " + \
                                  "%(levelname)s\n%(message)s")
    file_handler.setFormatter(formatter)
    app.logger.addHandler(file_handler)

# initialize configured database connections
for username, password, database, label in config.DEFAULT_DATABASES:
    models.add_database(username, password, database, label)


class LimitedRequest(Request):
    """ extending Flask's request class to limit form uploads to 100 MB """
    max_form_memory_size = 100 * 1024 * 1024

app.request_class = LimitedRequest
app.config.update(
    SECRET_KEY = config.SECRET_KEY,
    PERMANENT_SESSION_LIFETIME = datetime.timedelta(days=1)
)

# register view modules
from edacc.views.admin import admin
from edacc.views.accounts import accounts
from edacc.views.frontend import frontend
from edacc.views.analysis import analysis
from edacc.views.plot import plot

app.register_module(admin)
app.register_module(accounts)
app.register_module(frontend)
app.register_module(analysis)
app.register_module(plot)


if config.PIWIK:
    @app.before_request
    def register_piwik():
        """ Attach piwik URL to g """
        g.PIWIK_URL = config.PIWIK_URL


@app.before_request
def make_unique_id():
    """ Attach an unique ID to the request """
    g.unique_id = uuid.uuid4().hex


@app.after_request
def shutdown_session(response):
    """ remove SQLAlchemy session from thread after requests - might not even be needed for
    non-declarative SQLAlchemy usage according to the SQLAlchemy documentation.
    """
    for db in models.get_databases().itervalues():
        db.session.remove()
    return response
<commit_msg>filesystem caching of jinja2 template bytecode<commit_after># -*- coding: utf-8 -*-
"""
    edacc.web
    ---------

    In this module the flask application instance is defined and configured
    according to the settings in config.py.

    :copyright: (c) 2010 by <NAME>.
    :license: MIT, see LICENSE for details.
"""

import uuid, datetime

import jinja2
from werkzeug import ImmutableDict
from flask import Flask, Request, g
from edacc import config, models

Flask.jinja_options = ImmutableDict({
                            'extensions': ['jinja2.ext.autoescape', 'jinja2.ext.with_'],
                            'bytecode_cache': jinja2.FileSystemBytecodeCache(config.TEMP_DIR)
})
app = Flask(__name__)
app.Debug = config.DEBUG


if config.LOGGING:
    # set up logging if configured
    import logging
    from logging.handlers import RotatingFileHandler
    file_handler = RotatingFileHandler(config.LOG_FILE)
    file_handler.setLevel(logging.WARNING)
    formatter = logging.Formatter("---------------------------\n" + \
                                  "%(asctime)s - %(name)s - " + \
                                  "%(levelname)s\n%(message)s")
    file_handler.setFormatter(formatter)
    app.logger.addHandler(file_handler)

# initialize configured database connections
for username, password, database, label in config.DEFAULT_DATABASES:
    models.add_database(username, password, database, label)


class LimitedRequest(Request):
    """ extending Flask's request class to limit form uploads to 100 MB """
    max_form_memory_size = 100 * 1024 * 1024

app.request_class = LimitedRequest
app.config.update(
    SECRET_KEY = config.SECRET_KEY,
    PERMANENT_SESSION_LIFETIME = datetime.timedelta(days=1)
)

# register view modules
from edacc.views.admin import admin
from edacc.views.accounts import accounts
from edacc.views.frontend import frontend
from edacc.views.analysis import analysis
from edacc.views.plot import plot

app.register_module(admin)
app.register_module(accounts)
app.register_module(frontend)
app.register_module(analysis)
app.register_module(plot)


if config.PIWIK:
    @app.before_request
    def register_piwik():
        """ Attach piwik URL to g """
        g.PIWIK_URL = config.PIWIK_URL


@app.before_request
def make_unique_id():
    """ Attach an unique ID to the request """
    g.unique_id = uuid.uuid4().hex


@app.after_request
def shutdown_session(response):
    """ remove SQLAlchemy session from thread after requests - might not even be needed for
    non-declarative SQLAlchemy usage according to the SQLAlchemy documentation.
    """
    for db in models.get_databases().itervalues():
        db.session.remove()
    return response

{'fraction_non_alphanumeric': 0.10383480825958702, 'fraction_numerical': 0.009242871189773844, 'mean_word_length': 3.2560669456066944, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6760131', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1469, 'n_words': 408}