Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigigraph / vigigraph / config / middleware.py @ 696cffc9

History | View | Annotate | Download (3.79 KB)

1
# -*- coding: utf-8 -*-
2
# vim: set fileencoding=utf-8 sw=4 ts=4 et :
3
# Copyright (C) 2006-2020 CS GROUP - France
4
# License: GNU GPL v2 <http://www.gnu.org/licenses/gpl-2.0.html>
5

    
6
"""WSGI middleware initialization for the vigigraph application."""
7

    
8
import imp
9
import os.path
10
from pkg_resources import resource_filename, working_set
11
from paste.cascade import Cascade
12
from paste.urlparser import StaticURLParser
13
from logging import getLogger
14

    
15
__all__ = ['make_app']
16

    
17

    
18
def make_app(global_conf, full_stack=True, **app_conf):
19
    """
20
    Set vigigraph up with the settings found in the PasteDeploy configuration
21
    file used.
22

23
    This is the PasteDeploy factory for the vigigraph application.
24

25
    C{app_conf} contains all the application-specific settings (those defined
26
    under ``[app:main]``).
27

28
    @param global_conf: The global settings for vigigraph (those
29
        defined under the ``[DEFAULT]`` section).
30
    @type global_conf: C{dict}
31
    @param full_stack: Should the whole TG2 stack be set up?
32
    @type full_stack: C{str} or C{bool}
33
    @return: The vigigraph application with all the relevant middleware
34
        loaded.
35
    """
36
    # Charge le fichier "app_cfg.py" se trouvant aux côtés de "settings.ini".
37
    mod_info = imp.find_module('app_cfg', [ global_conf['here'] ])
38
    app_cfg = imp.load_module('vigigraph.config.app_cfg', *mod_info)
39
    base_config = app_cfg.base_config
40

    
41
    # Initialisation de l'application et de son environnement d'exécution.
42
    load_environment = base_config.make_load_environment()
43
    make_base_app = base_config.setup_tg_wsgi_app(load_environment)
44
    app = make_base_app(global_conf, full_stack=True, **app_conf)
45

    
46
    max_age = app_conf.get("cache_max_age")
47
    try:
48
        max_age = int(max_age)
49
    except (ValueError, TypeError):
50
        max_age = None
51

    
52
    # Personalisation des fichiers statiques via un dossier public/
53
    # dans le répertoire contenant le fichier settings.ini chargé.
54
    custom_static = StaticURLParser(os.path.join(global_conf['here'], 'public'),
55
                                    cache_max_age=max_age)
56

    
57
    # On définit 2 middlewares pour fichiers statiques qui cherchent
58
    # les fichiers dans le thème actuellement chargé.
59
    # Le premier va les chercher dans le dossier des fichiers spécifiques
60
    # à l'application, le second cherche dans les fichiers communs.
61
    app_static = StaticURLParser(
62
        resource_filename('vigilo.themes.public', 'vigigraph'),
63
        cache_max_age=max_age)
64
    common_static = StaticURLParser(
65
        resource_filename('vigilo.themes.public', 'common'),
66
        cache_max_age=max_age)
67
    local_static = StaticURLParser(
68
        resource_filename('vigigraph', 'public'),
69
        cache_max_age=max_age)
70
    cascade_list = [custom_static, app_static, common_static, local_static, app]
71

    
72
    LOGGER = getLogger("vigigraph")
73
    ## Mise en place du répertoire d'extensions
74
    #setup_plugins_path(base_config.get("plugins_dir",
75
    #                   "/etc/vigilo/vigigraph/plugins"))
76

    
77
    # Spécifique projets
78
    for module in ["turbogears", "vigigraph"]:
79
        for entry in working_set.iter_entry_points(
80
                                "vigilo.%s.public" % module):
81
            if (entry.name != "enterprise" and
82
                    entry.name not in base_config.get("extensions", [])):
83
                # les points d'entrée "enterprise" sont automatiquement
84
                # chargés, il faut lister les autres dans la conf
85
                continue
86
            new_public_dir = resource_filename(entry.module_name, "public")
87
            LOGGER.debug("Adding static files directory for ext %s: %s",
88
                         (entry.name, new_public_dir))
89
            cascade_list.insert(0, StaticURLParser(new_public_dir,
90
                                                   cache_max_age=max_age))
91

    
92
    app = Cascade(cascade_list)
93
    return app