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