<commit_before>"""
byceps.application
~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2019 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from importlib import import_module
from pathlib import Path
from typing import Any, Dict, Iterator, Optional, Tuple, Union

from flask import Flask, redirect
import jinja2

from .blueprints.snippet.init import add_routes_for_snippets
from . import config, config_defaults
from .database import db
from . import email
from .redis import redis
from .util.framework.blueprint import register_blueprint
from .util.l10n import set_locale
from .util import templatefilters
from .util.templating import SiteTemplateOverridesLoader


BlueprintReg = Tuple[str, Optional[str]]


def create_app(
    config_filename: Union[Path, str],
    config_overrides: Optional[Dict[str, Any]] = None,
) -> Flask:
    """Create the actual Flask application."""
    app = Flask(__name__)

    app.config.from_object(config_defaults)
    app.config.from_pyfile(str(config_filename))
    if config_overrides is not None:
        app.config.from_mapping(config_overrides)

    # Throw an exception when an undefined name is referenced in a template.
    app.jinja_env.undefined = jinja2.StrictUndefined

    # Set the locale.
    set_locale(app.config['LOCALE'])  # Fail if not configured.

    # Initialize database.
    db.init_app(app)

    # Initialize Redis connection.
    redis.init_app(app)

    email.init_app(app)

    config.init_app(app)

    _register_blueprints(app)

    templatefilters.register(app)

    _add_static_file_url_rules(app)

    return app


def _register_blueprints(app: Flask) -> None:
    """Register blueprints depending on the configuration."""
    for name, url_prefix in _get_blueprints(app):
        register_blueprint(app, name, url_prefix)


def _get_blueprints(app: Flask) -> Iterator[BlueprintReg]:
    """Yield blueprints to register on the application."""
    yield from _get_blueprints_common()

    current_mode = config.get_site_mode(app)
    if current_mode.is_public():
        yield from _get_blueprints_site()
    elif current_mode.is_admin():
        yield from _get_blueprints_admin()

    yield from _get_blueprints_api()

    yield from _get_blueprints_health()

    if app.config['METRICS_ENABLED']:
        yield from _get_blueprints_metrics()

    if app.debug:
        yield from _get_blueprints_debug()


def _get_blueprints_common() -> Iterator[BlueprintReg]:
    yield from [
        ('authentication',              '/authentication'           ),
        ('authorization',               None                        ),
        ('core',                        '/core'                     ),
        ('user',                        None                        ),
        ('user.avatar',                 '/users'                    ),
        ('user.creation',               '/users'                    ),
        ('user.current',                '/users'                    ),
        ('user.email_address',          '/users/email_address'      ),
    ]


def _get_blueprints_site() -> Iterator[BlueprintReg]:
    yield from [
        ('attendance',                  '/attendance'               ),
        ('board',                       '/board'                    ),
        ('consent',                     '/consent'                  ),
        ('news',                        '/news'                     ),
        ('newsletter',                  '/newsletter'               ),
        ('orga_team',                   '/orgas'                    ),
        ('party',                       None                        ),
        ('seating',                     '/seating'                  ),
        ('shop.order',                  '/shop'                     ),
        ('shop.orders',                 '/shop/orders'              ),
        ('snippet',                     '/snippets'                 ),
        ('terms',                       '/terms'                    ),
        ('ticketing',                   '/tickets'                  ),
        ('user.profile',                '/users'                    ),
        ('user_badge',                  '/user_badges'              ),
        ('user_group',                  '/user_groups'              ),
        ('user_message',                '/user_messages'            ),
    ]


def _get_blueprints_admin() -> Iterator[BlueprintReg]:
    yield from [
        ('admin.attendance',            '/admin/attendance'         ),
        ('admin.authorization',         '/admin/authorization'      ),
        ('admin.board',                 '/admin/board'              ),
        ('admin.brand',                 '/admin/brands'             ),
        ('admin.consent',               '/admin/consent'            ),
        ('admin.core',                  None                        ),
        ('admin.dashboard',             '/admin/dashboard'          ),
        ('admin.email',                 '/admin/email'              ),
        ('admin.news',                  '/admin/news'               ),
        ('admin.newsletter',            '/admin/newsletter'         ),
        ('admin.jobs',                  '/admin/jobs'               ),
        ('admin.orga',                  '/admin/orgas'              ),
        ('admin.orga_presence',         '/admin/presence'           ),
        ('admin.orga_team',             '/admin/orga_teams'         ),
        ('admin.party',                 '/admin/parties'            ),
        ('admin.seating',               '/admin/seating'            ),
        ('admin.shop',                  None                        ),
        ('admin.shop.article',          '/admin/shop/articles'      ),
        ('admin.shop.email',            '/admin/shop/email'         ),<commit_msg>Remove URL prefix for snippets blueprint as it exposes no endpoints<commit_after>"""
byceps.application
~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2019 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from importlib import import_module
from pathlib import Path
from typing import Any, Dict, Iterator, Optional, Tuple, Union

from flask import Flask, redirect
import jinja2

from .blueprints.snippet.init import add_routes_for_snippets
from . import config, config_defaults
from .database import db
from . import email
from .redis import redis
from .util.framework.blueprint import register_blueprint
from .util.l10n import set_locale
from .util import templatefilters
from .util.templating import SiteTemplateOverridesLoader


BlueprintReg = Tuple[str, Optional[str]]


def create_app(
    config_filename: Union[Path, str],
    config_overrides: Optional[Dict[str, Any]] = None,
) -> Flask:
    """Create the actual Flask application."""
    app = Flask(__name__)

    app.config.from_object(config_defaults)
    app.config.from_pyfile(str(config_filename))
    if config_overrides is not None:
        app.config.from_mapping(config_overrides)

    # Throw an exception when an undefined name is referenced in a template.
    app.jinja_env.undefined = jinja2.StrictUndefined

    # Set the locale.
    set_locale(app.config['LOCALE'])  # Fail if not configured.

    # Initialize database.
    db.init_app(app)

    # Initialize Redis connection.
    redis.init_app(app)

    email.init_app(app)

    config.init_app(app)

    _register_blueprints(app)

    templatefilters.register(app)

    _add_static_file_url_rules(app)

    return app


def _register_blueprints(app: Flask) -> None:
    """Register blueprints depending on the configuration."""
    for name, url_prefix in _get_blueprints(app):
        register_blueprint(app, name, url_prefix)


def _get_blueprints(app: Flask) -> Iterator[BlueprintReg]:
    """Yield blueprints to register on the application."""
    yield from _get_blueprints_common()

    current_mode = config.get_site_mode(app)
    if current_mode.is_public():
        yield from _get_blueprints_site()
    elif current_mode.is_admin():
        yield from _get_blueprints_admin()

    yield from _get_blueprints_api()

    yield from _get_blueprints_health()

    if app.config['METRICS_ENABLED']:
        yield from _get_blueprints_metrics()

    if app.debug:
        yield from _get_blueprints_debug()


def _get_blueprints_common() -> Iterator[BlueprintReg]:
    yield from [
        ('authentication',              '/authentication'           ),
        ('authorization',               None                        ),
        ('core',                        '/core'                     ),
        ('user',                        None                        ),
        ('user.avatar',                 '/users'                    ),
        ('user.creation',               '/users'                    ),
        ('user.current',                '/users'                    ),
        ('user.email_address',          '/users/email_address'      ),
    ]


def _get_blueprints_site() -> Iterator[BlueprintReg]:
    yield from [
        ('attendance',                  '/attendance'               ),
        ('board',                       '/board'                    ),
        ('consent',                     '/consent'                  ),
        ('news',                        '/news'                     ),
        ('newsletter',                  '/newsletter'               ),
        ('orga_team',                   '/orgas'                    ),
        ('party',                       None                        ),
        ('seating',                     '/seating'                  ),
        ('shop.order',                  '/shop'                     ),
        ('shop.orders',                 '/shop/orders'              ),
        ('snippet',                     None                        ),
        ('terms',                       '/terms'                    ),
        ('ticketing',                   '/tickets'                  ),
        ('user.profile',                '/users'                    ),
        ('user_badge',                  '/user_badges'              ),
        ('user_group',                  '/user_groups'              ),
        ('user_message',                '/user_messages'            ),
    ]


def _get_blueprints_admin() -> Iterator[BlueprintReg]:
    yield from [
        ('admin.attendance',            '/admin/attendance'         ),
        ('admin.authorization',         '/admin/authorization'      ),
        ('admin.board',                 '/admin/board'              ),
        ('admin.brand',                 '/admin/brands'             ),
        ('admin.consent',               '/admin/consent'            ),
        ('admin.core',                  None                        ),
        ('admin.dashboard',             '/admin/dashboard'          ),
        ('admin.email',                 '/admin/email'              ),
        ('admin.news',                  '/admin/news'               ),
        ('admin.newsletter',            '/admin/newsletter'         ),
        ('admin.jobs',                  '/admin/jobs'               ),
        ('admin.orga',                  '/admin/orgas'              ),
        ('admin.orga_presence',         '/admin/presence'           ),
        ('admin.orga_team',             '/admin/orga_teams'         ),
        ('admin.party',                 '/admin/parties'            ),
        ('admin.seating',               '/admin/seating'            ),
        ('admin.shop',                  None                        ),
        ('admin.shop.article',          '/admin/shop/articles'      ),
        ('admin.shop.email',            '/admin/shop/email'         ),
{'fraction_non_alphanumeric': 0.11649055950112593, 'fraction_numerical': 0.0020786419539234368, 'mean_word_length': 1.3647347941839034, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7110314', 'n_tokens_mistral': 3041, 'n_tokens_neox': 2889, 'n_words': 606}