|
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
|
|
|
configure_session(config) |
|
40
|
|
|
config.include('pyramid_jinja2') |
|
41
|
|
|
config.include('pyramid_tm') |
|
42
|
|
|
config.add_static_view('static', 'static', cache_max_age=3600) |
|
43
|
|
|
config.add_renderer('csv', 'atramhasis.renderers.CSVRenderer') |
|
44
|
|
|
config.add_renderer('skosrenderer_verbose', json_renderer_verbose) |
|
45
|
|
|
# Rewrite urls with trailing slash |
|
46
|
|
|
config.include('pyramid_rewrite') |
|
47
|
|
|
config.include("pyramid_openapi3") |
|
48
|
|
|
config.include('atramhasis.routes') |
|
49
|
|
|
# pyramid_skosprovider must be included after the atramhasis routes |
|
50
|
|
|
# because it contains a regex in the path which consumes a lot of routes. |
|
51
|
|
|
config.include('pyramid_skosprovider') |
|
52
|
|
|
config.include('atramhasis.cache') |
|
53
|
|
|
|
|
54
|
|
|
config.add_translation_dirs('atramhasis:locale/') |
|
55
|
|
|
|
|
56
|
|
|
config.scan() |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
def configure_session(config): |
|
60
|
|
|
""" |
|
61
|
|
|
Configure pyramid's session factory. |
|
62
|
|
|
|
|
63
|
|
|
People can configure their own session factory, but if no factory is registered |
|
64
|
|
|
atramhasis will try configuring its own. |
|
65
|
|
|
""" |
|
66
|
|
|
|
|
67
|
|
|
def check_session_factory_set(): |
|
68
|
|
|
session_factory = config.registry.queryUtility(ISessionFactory) |
|
69
|
|
|
if session_factory: |
|
70
|
|
|
return |
|
71
|
|
|
|
|
72
|
|
|
settings = config.registry.settings |
|
73
|
|
|
if 'atramhasis.session_factory.secret' not in settings: |
|
74
|
|
|
msg = ( |
|
75
|
|
|
'No session factory is configured, and ' |
|
76
|
|
|
'atramhasis.session_factory.secret setting is missing.' |
|
77
|
|
|
) |
|
78
|
|
|
raise ValueError(msg) |
|
79
|
|
|
|
|
80
|
|
|
LOG.info('Using default SignedCookieSessionFactory.') |
|
81
|
|
|
default_session_factory = SignedCookieSessionFactory( |
|
82
|
|
|
settings['atramhasis.session_factory.secret'] |
|
83
|
|
|
) |
|
84
|
|
|
config.registry.registerUtility(default_session_factory, ISessionFactory) |
|
85
|
|
|
|
|
86
|
|
|
config.action( |
|
87
|
|
|
'check_session_factory_set', check_session_factory_set, order=PHASE3_CONFIG + 1 |
|
88
|
|
|
) |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
def main(global_config, **settings): |
|
92
|
|
|
""" This function returns a Pyramid WSGI application. |
|
93
|
|
|
""" |
|
94
|
|
|
settings['layout.focus_conceptschemes'] = aslist(settings['layout.focus_conceptschemes'], flatten=False) |
|
95
|
|
|
|
|
96
|
|
|
dump_location = settings['atramhasis.dump_location'] |
|
97
|
|
|
if not os.path.exists(dump_location): |
|
98
|
|
|
os.makedirs(dump_location) |
|
99
|
|
|
|
|
100
|
|
|
config = Configurator(settings=settings) |
|
101
|
|
|
|
|
102
|
|
|
return load_app(config) |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
def load_app(config): |
|
106
|
|
|
includeme(config) |
|
107
|
|
|
|
|
108
|
|
|
config.include('atramhasis.data:db') |
|
109
|
|
|
|
|
110
|
|
|
return config.make_wsgi_app() |
|
111
|
|
|
|