atramhasis.load_app()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
import logging
2
import os
3
4
from pyramid.config import Configurator
5
from pyramid.config import PHASE3_CONFIG
6
from pyramid.interfaces import ISessionFactory
7
from pyramid.session import SignedCookieSessionFactory
8
from pyramid.settings import aslist
9
10
from atramhasis.renderers import json_renderer_verbose
11
12
LOG = logging.getLogger(__name__)
13
14
15
DEFAULT_SETTINGS = {
16
    'cache.tree.backend': 'dogpile.cache.memory',
17
    'cache.tree.arguments.cache_size': '5000',
18
    'cache.tree.expiration_time': '7000',
19
    'cache.list.backend': 'dogpile.cache.memory',
20
    'cache.list.arguments.cache_size': '5000',
21
    'cache.list.expiration_time': '7000',
22
    'jinja2.extensions': 'jinja2.ext.do',
23
    'jinja2.filters': 'label_sort = atramhasis.utils.label_sort',
24
    'dojo.mode': 'dist',
25
    'layout.focus_conceptschemes': [],
26
    'skosprovider.skosregistry_factory': 'atramhasis.skos.create_registry',
27
    'skosprovider.skosregistry_location': 'request',
28
}
29
30
31
def includeme(config):
32
    """this function adds some configuration for the application"""
33
    settings = config.registry.settings
34
    for key, value in DEFAULT_SETTINGS.items():
35
        if key not in settings:
36
            settings[key] = value
37
    # Regexes in path params clash with this validation.
38
    settings['pyramid_openapi3.enable_endpoint_validation'] = False
39
    # Better to return the response we give rather than a validation error.
40
    settings['pyramid_openapi3.enable_response_validation'] = False
41
    configure_session(config)
42
    config.include('pyramid_jinja2')
43
    config.include('pyramid_tm')
44
    config.add_static_view('static', 'static', cache_max_age=3600)
45
    config.add_renderer('csv', 'atramhasis.renderers.CSVRenderer')
46
    config.add_renderer('skosrenderer_verbose', json_renderer_verbose)
47
    # Rewrite urls with trailing slash
48
    config.include('pyramid_rewrite')
49
    config.include('pyramid_openapi3')
50
    config.include('atramhasis.routes')
51
    # pyramid_skosprovider must be included after the atramhasis routes
52
    # because it contains a regex in the path which consumes a lot of routes.
53
    config.include('pyramid_skosprovider')
54
    config.include('atramhasis.cache')
55
56
    config.add_translation_dirs('atramhasis:locale/')
57
58
    config.scan()
59
60
61
def configure_session(config):
62
    """
63
    Configure pyramid's session factory.
64
65
    People can configure their own session factory, but if no factory is registered
66
    atramhasis will try configuring its own.
67
    """
68
69
    def check_session_factory_set():
70
        session_factory = config.registry.queryUtility(ISessionFactory)
71
        if session_factory:
72
            return
73
74
        settings = config.registry.settings
75
        if 'atramhasis.session_factory.secret' not in settings:
76
            msg = (
77
                'No session factory is configured, and '
78
                'atramhasis.session_factory.secret setting is missing.'
79
            )
80
            raise ValueError(msg)
81
82
        LOG.info('Using default SignedCookieSessionFactory.')
83
        default_session_factory = SignedCookieSessionFactory(
84
            settings['atramhasis.session_factory.secret']
85
        )
86
        config.registry.registerUtility(default_session_factory, ISessionFactory)
87
88
    config.action(
89
        'check_session_factory_set', check_session_factory_set, order=PHASE3_CONFIG + 1
90
    )
91
92
93
def main(global_config, **settings):
94
    """This function returns a Pyramid WSGI application."""
95
    settings['layout.focus_conceptschemes'] = aslist(
96
        settings['layout.focus_conceptschemes'], flatten=False
97
    )
98
99
    dump_location = settings['atramhasis.dump_location']
100
    if not os.path.exists(dump_location):
101
        os.makedirs(dump_location)
102
103
    config = Configurator(settings=settings)
104
105
    return load_app(config)
106
107
108
def load_app(config):
109
    includeme(config)
110
111
    config.include('atramhasis.data:db')
112
113
    return config.make_wsgi_app()
114