<commit_before>import logging
import pkg_resources
from pyramid.config import Configurator
from pyramid.exceptions import NotFound
from pyramid.wsgi import wsgiapp
from pyramid_beaker import session_factory_from_settings
from pyramid.settings import asbool

#from sqlalchemy import engine_from_config
#from aybu.core.model import init_model

from aybu.core.request import AybuRequest
from aybu.core.resources import Root
from aybu.core.dispatch import get_pylons_app
from captchalib.pyramid import CaptchaView

log = logging.getLogger(__name__)


def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    log.info("Starting application")
    config = Configurator(root_factory=Root,
                          request_factory=AybuRequest,
                          settings=settings)
    config.begin()
    # Configure beaker session
    session_factory = session_factory_from_settings(settings)
    config.set_session_factory(session_factory)

    # Add fallback to old pylons application
    pylons = get_pylons_app(global_config)
    fallback_view = wsgiapp(pylons)

    # Configure caching
    if not "disable_cache" in settings or not asbool(settings['disable_cache']):
        from aybu.core.cache.proxy import CacheProxy
        CacheProxy.cache_settings = {k.replace("cache.", "") : settings[k]
                                              for k in settings
                                              if k.startswith("cache.")}
    else:
        log.warn("Disabling cache globally")

    # Fallback on 404
    config.add_view(fallback_view, context=NotFound)
    # Fallback on "normal" pages in admin mode
    config.add_view(context='aybu.core.resources.ViewInfo',
                    view=fallback_view,
                    request_param='admin')
    config.add_view(context='aybu.core.resources.ContactsViewInfo',
                    view=fallback_view,
                    request_param='admin')
    config.add_view(context='aybu.core.resources.Admin',
                    view=fallback_view)

    # initialize babel
    config.add_translation_dirs('aybu.core:locale')

    setup_database(settings)
    config.include(setup_assets)
    config.include(add_subscribers)
    config.include(add_views)
    config.end()

    return config.make_wsgi_app()


def setup_database(settings):
    log.info("Setting up models")
    # FIXME: skip engine mapping as it's already been done by
    # the legacy pylons app
    #engine = engine_from_config(settings, "sqlalchemy.")
    #sess, mtd = init_model(engine)
    from aybu.cms.model.meta import dbsession, metadata
    AybuRequest.dbsession = dbsession
    AybuRequest.dbmetadata = metadata


def add_subscribers(config):
    config.set_renderer_globals_factory('aybu.core.views.add_renderer_globals')


def add_views(config):
    log.info("Adding views")
    config.add_view(context='aybu.core.resources.ViewInfo',
                    view='aybu.core.views.page.dynamic')
    config.add_view(context='aybu.core.resources.ContactsViewInfo',
                    view='aybu.core.views.page.contacts')

    # Configure Captchalib
    CaptchaView.text_length = 6
    CaptchaView.text_color = None
    CaptchaView.background_color = None
    CaptchaView.background_ext_list = None
    config.add_view(context='aybu.core.resources.Captcha', name='show',
                    view=CaptchaView)


def setup_assets(config):
    """ Setup search paths for static files and for templates """
    from aybu.core.model.entities import Theme, Setting
    from aybu.cms.model.meta import dbsession

    theme_name = dbsession.query(Setting.value).\
            filter(Setting.name == 'theme_name').subquery()
    theme = Theme.query.filter(Theme.name.in_(theme_name)).one()

    log.info("Adding static view for aybu")
    config.add_static_view('static', 'aybu.core:static')

    log.info("Preparing static search path for %s", theme)

    themes_inheritance_chain = []
    themes_paths = [ pkg_resources.resource_filename('aybu.core', 'templates') ]
    while theme:
        themes_inheritance_chain.insert(0, theme)
        theme = theme.parent

    for theme in themes_inheritance_chain:
        theme_static_spec = 'aybu.themes:%s/public/' % theme.name
        log.info("Adding '%s' as override for static files", theme_static_spec)
        config.override_asset(<commit_msg>Use sqlsoup to access the database for theme setup during configuration. Fixes #550<commit_after>import logging
import pkg_resources
from pyramid.config import Configurator
from pyramid.exceptions import NotFound
from pyramid.wsgi import wsgiapp
from pyramid_beaker import session_factory_from_settings
from pyramid.settings import asbool

from sqlalchemy import engine_from_config
from sqlalchemy.ext.sqlsoup import SqlSoup

#from aybu.core.model import init_model

from aybu.core.request import AybuRequest
from aybu.core.resources import Root
from aybu.core.dispatch import get_pylons_app
from captchalib.pyramid import CaptchaView

log = logging.getLogger(__name__)

try:
    import uwsgi
except ImportError:
    log.info("AYBU Starting")
    uwsgi = None
else:
    log.info("AYBU Starting behind uWSGI")



def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    log.info("Starting application")
    config = Configurator(root_factory=Root,
                          request_factory=AybuRequest,
                          settings=settings)
    config.begin()
    # Configure beaker session
    session_factory = session_factory_from_settings(settings)
    config.set_session_factory(session_factory)

    # Add fallback to old pylons application
    pylons = get_pylons_app(global_config)
    fallback_view = wsgiapp(pylons)

    # Configure caching
    if not "disable_cache" in settings or not asbool(settings['disable_cache']):
        from aybu.core.cache.proxy import CacheProxy
        CacheProxy.cache_settings = {k.replace("cache.", "") : settings[k]
                                              for k in settings
                                              if k.startswith("cache.")}
    else:
        log.warn("Disabling cache globally")

    # Fallback on 404
    config.add_view(fallback_view, context=NotFound)
    # Fallback on "normal" pages in admin mode
    config.add_view(context='aybu.core.resources.ViewInfo',
                    view=fallback_view,
                    request_param='admin')
    config.add_view(context='aybu.core.resources.ContactsViewInfo',
                    view=fallback_view,
                    request_param='admin')
    config.add_view(context='aybu.core.resources.Admin',
                    view=fallback_view)

    # initialize babel
    config.add_translation_dirs('aybu.core:locale')

    setup_database(settings)
    config.include(setup_assets)
    config.include(add_subscribers)
    config.include(add_views)
    config.end()

    return config.make_wsgi_app()


def setup_database(settings):
    log.info("Setting up models")
    # FIXME: skip engine mapping as it's already been done by
    # the legacy pylons app
    #engine = engine_from_config(settings, "sqlalchemy.")
    #sess, mtd = init_model(engine)
    def _setup_model():
        from aybu.cms.model.meta import dbsession, metadata
        AybuRequest.dbsession = dbsession
        AybuRequest.dbmetadata = metadata

    if uwsgi and uwsgi.numproc > 1:
        # FIXME: when removing the fallback pylons app, we need to install the
        # postfork hook here
        #uwsgi.post_fork_hook = _setup_model
        _setup_model()
    else:
        _setup_model()


def add_subscribers(config):
    config.set_renderer_globals_factory('aybu.core.views.add_renderer_globals')


def add_views(config):
    log.info("Adding views")
    config.add_view(context='aybu.core.resources.ViewInfo',
                    view='aybu.core.views.page.dynamic')
    config.add_view(context='aybu.core.resources.ContactsViewInfo',
                    view='aybu.core.views.page.contacts')

    # Configure Captchalib
    CaptchaView.text_length = 6
    CaptchaView.text_color = None
    CaptchaView.background_color = None
    CaptchaView.background_ext_list = None
    config.add_view(context='aybu.core.resources.Captcha', name='show',
                    view=CaptchaView)


def setup_assets(config):
    """ Setup search paths for static files and for templates """

    # Use SqlSoup to access database to avoid requiring the entities to be
    # mapped, which is not the case when we are in a multiprocess environment.
    engine = engine_from_config(config.registry.settings, "sqlalchemy.")
    db = SqlSoup(engine)
    tname = db.settings.filter(db.settings.name == u'theme_name').one().value
    theme = db.theme.filter(db.theme.name == tname).one()

    log.info("Adding static view for aybu")
    config.add_static_view('static', 'aybu.core:static')

    log.info("Preparing static search path for %s", theme)

    themes_inheritance_chain = []
    themes_paths = [ pkg_resources.resource_filename('aybu.core', 'templates') ]
    while theme:
        themes_inheritance_chain.insert(0, theme)
        if theme.parent_name:
            theme = db.theme.filter(db.theme.name == theme.parent_name).one()
        else:
            theme = None

    for theme in themes_inheritance_chain:
        theme_static_spec = 'aybu.themes:%s/public/' % theme.name
        log.info("Adding '%s' as override for static files", theme_static_spec)
        config.override_asset(
{'fraction_non_alphanumeric': 0.09061250263102505, 'fraction_numerical': 0.0014733740265207324, 'mean_word_length': 3.082044673539519, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '472708', 'n_tokens_mistral': 2855, 'n_tokens_neox': 2728, 'n_words': 611}