pyramid_cubicweb/__init__.py
author Christophe de Vienne <christophe@unlish.com>
Thu, 28 Aug 2014 15:42:51 +0200
changeset 11503 ddf61aa73384
parent 11501 fcf7f99fad4a
child 11511 13e0f569684c
permissions -rw-r--r--
Add a wsgi application factory suitable for wsgi servers. This factory can generate a wsgi application for a cubicweb instance. It reads the instance name from the CW_INSTANCE environment variable, and activates the debugmode if CW_DEBUG is defined in environment. It is usable by uwsgi as the 'module' parameter : CW_INSTANCE=test uwsgi --plugins python,http --http 0.0.0.0:8080 --module pyramid_cubicweb:wsgi_application()
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11503
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
     1
import os
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
     2
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
     3
from cubicweb.cwconfig import CubicWebConfiguration as cwcfg
11501
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
     4
from pyramid.config import Configurator
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
     5
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
     6
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
     7
def make_cubicweb_application(cwconfig):
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
     8
    """
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
     9
    Create a pyramid-based CubicWeb instance from a cubicweb configuration.
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    10
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    11
    It is initialy meant to be used by the 'pyramid' command of cubicweb-ctl.
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    12
    """
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    13
    settings = {
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    14
        'session.secret': '11',  # XXX
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    15
    }
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    16
    if cwconfig.debugmode:
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    17
        settings.update({
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    18
            'pyramid.debug_authorization': True,
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    19
            'pyramid.debug_notfound': True,
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    20
            'pyramid.debug_routematch': True,
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    21
        })
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    22
    config = Configurator(settings=settings)
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    23
    if cwconfig.debugmode:
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    24
        config.include('pyramid_debugtoolbar')
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    25
    config.registry['cubicweb.config'] = cwconfig
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    26
    config.registry['cubicweb.repository'] = repo = cwconfig.repository()
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    27
    config.registry['cubicweb.registry'] = repo.vreg
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    28
    config.include('pyramid_cubicweb.defaults')
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    29
    config.include('pyramid_cubicweb.core')
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    30
    config.include('pyramid_cubicweb.bwcompat')
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    31
    return config
11503
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    32
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    33
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    34
def wsgi_application(instance_name=None, debug=None):
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    35
    if instance_name is None:
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    36
        instance_name = os.environ.get('CW_INSTANCE')
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    37
    if debug is None:
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    38
        debug = 'CW_DEBUG' in os.environ
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    39
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    40
    cwconfig = cwcfg.config_for(instance_name, debugmode=debug)
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    41
    config = make_cubicweb_application(cwconfig)
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    42
    return config.make_wsgi_app()