<commit_before>#
# Quru Image Server
#
# Document:      flask_app.py
# Date started:  03 Oct 2011
# By:            <NAME>
# Purpose:       Flask app initializer
# Requires:
# Copyright:     Quru Ltd (www.quru.com)
# Licence:
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published
#   by the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see http://www.gnu.org/licenses/
#
# Last Changed:  $Date$ $Rev$ by $Author$
#
# Notable modifications:
# Date       By    Details
# =========  ====  ============================================================
#

import os
import flask
import __about__


def extend_app(app):
    """
    Applies custom extensions and middleware to the Flask app.
    """
    import csrf
    import flask_ext
    # Add proxy server support, if required
    if app.config['PROXY_SERVERS'] > 0:
        flask_ext.add_proxy_server_support(app, app.config['PROXY_SERVERS'])
    # Fix URLs with a badly encoded query string
    flask_ext.fix_bad_query_strings(app)
    # Add Markdown support to templates
    flask_ext.install_markdown_template_tag(app, app.config['DOCS_BASE_DIR'])
    # Enhanced JSON support for the API
    flask_ext.enhance_json_encoder(app)
    # We'll measure request times for our stats
    flask_ext.time_requests(app, True)
    # In development, allow any origin. In production this is set in the web server config.
    if app.config['DEBUG']:
        flask_ext.add_cors_headers(app, '*')
    # We need HTTP authentication for the API
    flask_ext.install_http_authentication(app, app.config['API_AUTHENTICATION_CLASS'])
    # And CSRF protection for the web pages (this needs to be after the http auth)
    csrf.install_csrf(app)


def configure_app(app):
    """
    Applies the configuration settings for the Flask app.
    This is the sum of the Flask defaults, overridden by conf.base_settings,
    overridden by the file specified in the QIS_SETTINGS environment variable.
    If QIS_SETTINGS is empty, the application tries ../../conf/local_settings.py
    """
    _ENV_VAR = 'QIS_SETTINGS'

    # Set Jinja template engine options
    app.jinja_options = app.jinja_options.copy()
    app.jinja_options['trim_blocks'] = True
    app.jinja_options['lstrip_blocks'] = True

    # Apply base configuration
    configs_used = ['base_settings']
    app.config.from_object('imageserver.conf.base_settings')

    # Examine QIS_SETTINGS
    env_settings = os.environ.get(_ENV_VAR)
    if not env_settings:
        # See if local_settings.py exists
        app_src_path = os.path.split(__file__)[0]
        local_settings_path = os.path.abspath(
            os.path.join(app_src_path, '..', '..', 'conf', 'local_settings.py')
        )
        if os.path.exists(local_settings_path):
            env_settings = os.environ[_ENV_VAR] = local_settings_path

    # Apply the user configuration
    if env_settings:
        if env_settings.endswith(".py"):
            # Full path to a Python settings file (the common case)
            app.config.from_envvar(_ENV_VAR)
            configs_used.append(os.path.split(env_settings)[1])
        else:
            # Name of a Python file in the imageserver.conf package
            app.config.from_object('imageserver.conf.' + env_settings)
            configs_used.append(env_settings)

    app.config['_SETTINGS_IN_USE'] = ' + '.join(configs_used)


# Create main web app
app = flask.Flask(__name__)
configure_app(app)
extend_app(app)

with app.app_context():
    from cache_manager import CacheManager
    from data_manager import DataManager
    from image_manager import ImageManager
    from log_manager import LogManager
    from permissions_manager import PermissionsManager
    from stats_manager import StatsManager
    from task_manager import TaskManager

    logger = None
    try:
        # Create the logging client+server
        logger = LogManager(
            __about__.__tag__.lower() + '_' + str(os.getpid()),
            app.config['DEBUG'],
            app.config['LOGGING_SERVER'],
            app.config['LOGGING_SERVER_PORT']
        )
        LogManager.run_server(
            app.config['LOGGING_SERVER'],
            app.config['LOGGING_SERVER_PORT'],
            __about__.__tag__.lower() + '.log',
            app.config['DEBUG']
        )
        # Capture Flask's internal logging
        app.logger.addHandler(logger.logging_handler)

        # Announce startup
        logger.info(__about__.__title__ + ' v' + __about__.__version__ + ' engine startup')
        logger.info('Using settings ' + app.config['_SETTINGS_IN_USE'])
        if app.config['DEBUG']:
            logger.info('*** Debug mode ENABLED ***')
        if app.config['BENCHMARKING']:
            logger.info('*** Benchmarking mode ENABLED ***')

        # Create the stats recording client
        stats_engine = StatsManager(<commit_msg>Oops, app.log is used in tasks.py<commit_after>#
# Quru Image Server
#
# Document:      flask_app.py
# Date started:  03 Oct 2011
# By:            <NAME>
# Purpose:       Flask app initializer
# Requires:
# Copyright:     Quru Ltd (www.quru.com)
# Licence:
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published
#   by the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see http://www.gnu.org/licenses/
#
# Last Changed:  $Date$ $Rev$ by $Author$
#
# Notable modifications:
# Date       By    Details
# =========  ====  ============================================================
#

import os
import flask
import __about__


def extend_app(app):
    """
    Applies custom extensions and middleware to the Flask app.
    """
    import csrf
    import flask_ext
    # Add proxy server support, if required
    if app.config['PROXY_SERVERS'] > 0:
        flask_ext.add_proxy_server_support(app, app.config['PROXY_SERVERS'])
    # Fix URLs with a badly encoded query string
    flask_ext.fix_bad_query_strings(app)
    # Add Markdown support to templates
    flask_ext.install_markdown_template_tag(app, app.config['DOCS_BASE_DIR'])
    # Enhanced JSON support for the API
    flask_ext.enhance_json_encoder(app)
    # We'll measure request times for our stats
    flask_ext.time_requests(app, True)
    # In development, allow any origin. In production this is set in the web server config.
    if app.config['DEBUG']:
        flask_ext.add_cors_headers(app, '*')
    # We need HTTP authentication for the API
    flask_ext.install_http_authentication(app, app.config['API_AUTHENTICATION_CLASS'])
    # And CSRF protection for the web pages (this needs to be after the http auth)
    csrf.install_csrf(app)


def configure_app(app):
    """
    Applies the configuration settings for the Flask app.
    This is the sum of the Flask defaults, overridden by conf.base_settings,
    overridden by the file specified in the QIS_SETTINGS environment variable.
    If QIS_SETTINGS is empty, the application tries ../../conf/local_settings.py
    """
    _ENV_VAR = 'QIS_SETTINGS'

    # Set Jinja template engine options
    app.jinja_options = app.jinja_options.copy()
    app.jinja_options['trim_blocks'] = True
    app.jinja_options['lstrip_blocks'] = True

    # Apply base configuration
    configs_used = ['base_settings']
    app.config.from_object('imageserver.conf.base_settings')

    # Examine QIS_SETTINGS
    env_settings = os.environ.get(_ENV_VAR)
    if not env_settings:
        # See if local_settings.py exists
        app_src_path = os.path.split(__file__)[0]
        local_settings_path = os.path.abspath(
            os.path.join(app_src_path, '..', '..', 'conf', 'local_settings.py')
        )
        if os.path.exists(local_settings_path):
            env_settings = os.environ[_ENV_VAR] = local_settings_path

    # Apply the user configuration
    if env_settings:
        if env_settings.endswith(".py"):
            # Full path to a Python settings file (the common case)
            app.config.from_envvar(_ENV_VAR)
            configs_used.append(os.path.split(env_settings)[1])
        else:
            # Name of a Python file in the imageserver.conf package
            app.config.from_object('imageserver.conf.' + env_settings)
            configs_used.append(env_settings)

    app.config['_SETTINGS_IN_USE'] = ' + '.join(configs_used)


# Create main web app
app = flask.Flask(__name__)
configure_app(app)
extend_app(app)

with app.app_context():
    from cache_manager import CacheManager
    from data_manager import DataManager
    from image_manager import ImageManager
    from log_manager import LogManager
    from permissions_manager import PermissionsManager
    from stats_manager import StatsManager
    from task_manager import TaskManager

    logger = None
    try:
        # Create the logging client+server
        logger = LogManager(
            __about__.__tag__.lower() + '_' + str(os.getpid()),
            app.config['DEBUG'],
            app.config['LOGGING_SERVER'],
            app.config['LOGGING_SERVER_PORT']
        )
        app.log = logger
        LogManager.run_server(
            app.config['LOGGING_SERVER'],
            app.config['LOGGING_SERVER_PORT'],
            __about__.__tag__.lower() + '.log',
            app.config['DEBUG']
        )
        # Capture Flask's internal logging
        app.logger.addHandler(logger.logging_handler)

        # Announce startup
        logger.info(__about__.__title__ + ' v' + __about__.__version__ + ' engine startup')
        logger.info('Using settings ' + app.config['_SETTINGS_IN_USE'])
        if app.config['DEBUG']:
            logger.info('*** Debug mode ENABLED ***')
        if app.config['BENCHMARKING']:
            logger.info('*** Benchmarking mode ENABLED ***')

        # Create the stats recording client
        stats_engine = StatsManager(
{'fraction_non_alphanumeric': 0.10170280078161348, 'fraction_numerical': 0.0018609844607797525, 'mean_word_length': 3.0104477611940297, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3717629', 'n_tokens_mistral': 3181, 'n_tokens_neox': 3117, 'n_words': 908}