cubicweb/pyramid/__init__.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 09 Nov 2016 11:42:33 +0100
branch3.24
changeset 11811 f09efeead7f9
parent 11702 be23c3813bbf
child 11958 950ce7d9f642
permissions -rw-r--r--
Fix broken flake8 configuration and flake8 errors which were hidden by this breakage. flake8 --filename options doesn't work as expected: * it's expected to be a shell pattern, using stdlib's fnmatch.fnmatch function internally. This funciton thinks that 'cubicweb/x.py' doesn't match 'cubicweb/x.py' (there must be a reason but that's not the point), hence no file was actually checked ; * as this is a list of pattern, each encountered file is checked against each pattern, leading to run time explosion. So maintain list of files to check in a separated file and give this list to flake8 using unix's xarg command.
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
11563
f9473eb6a8a9 Make debug mode usable without pyramid_debugtoolbar
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11550
diff changeset
     2
from warnings import warn
11511
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
     3
import wsgicors
11503
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
     4
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
     5
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
     6
from pyramid.config import Configurator
11534
ceb1a5baca4f [config] Read pyramid settings in a 'pyramid.ini' file
Christophe de Vienne <christophe@unlish.com>
parents: 11532
diff changeset
     7
from pyramid.settings import asbool, aslist
ceb1a5baca4f [config] Read pyramid settings in a 'pyramid.ini' file
Christophe de Vienne <christophe@unlish.com>
parents: 11532
diff changeset
     8
ceb1a5baca4f [config] Read pyramid settings in a 'pyramid.ini' file
Christophe de Vienne <christophe@unlish.com>
parents: 11532
diff changeset
     9
try:
ceb1a5baca4f [config] Read pyramid settings in a 'pyramid.ini' file
Christophe de Vienne <christophe@unlish.com>
parents: 11532
diff changeset
    10
    from configparser import SafeConfigParser
ceb1a5baca4f [config] Read pyramid settings in a 'pyramid.ini' file
Christophe de Vienne <christophe@unlish.com>
parents: 11532
diff changeset
    11
except ImportError:
ceb1a5baca4f [config] Read pyramid settings in a 'pyramid.ini' file
Christophe de Vienne <christophe@unlish.com>
parents: 11532
diff changeset
    12
    from ConfigParser import SafeConfigParser
11501
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    13
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    14
11564
a6547ff97ce0 Allow tests to override pyramid_settings
Christophe de Vienne <christophe@unlish.com>
parents: 11563
diff changeset
    15
def make_cubicweb_application(cwconfig, settings=None):
11501
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    16
    """
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    17
    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
    18
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    19
    It is initialy meant to be used by the 'pyramid' command of cubicweb-ctl.
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    20
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    21
    :param cwconfig: A CubicWeb configuration
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    22
    :returns: A Pyramid config object
11501
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    23
    """
11620
2497bcf18030 split collecting setting before using them, so the function can be reused when inserting WSGI middlewares
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 11601
diff changeset
    24
    settings = dict(settings) if settings else {}
2497bcf18030 split collecting setting before using them, so the function can be reused when inserting WSGI middlewares
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 11601
diff changeset
    25
    settings.update(settings_from_cwconfig(cwconfig))
2497bcf18030 split collecting setting before using them, so the function can be reused when inserting WSGI middlewares
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 11601
diff changeset
    26
    config = Configurator(settings=settings)
2497bcf18030 split collecting setting before using them, so the function can be reused when inserting WSGI middlewares
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 11601
diff changeset
    27
    config.registry['cubicweb.config'] = cwconfig
11631
faf279e33298 Merge with pyramid-cubicweb
Yann Voté <yann.vote@logilab.fr>
parents: 11623
diff changeset
    28
    config.include('cubicweb.pyramid')
11620
2497bcf18030 split collecting setting before using them, so the function can be reused when inserting WSGI middlewares
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 11601
diff changeset
    29
    return config
11534
ceb1a5baca4f [config] Read pyramid settings in a 'pyramid.ini' file
Christophe de Vienne <christophe@unlish.com>
parents: 11532
diff changeset
    30
11811
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11702
diff changeset
    31
11620
2497bcf18030 split collecting setting before using them, so the function can be reused when inserting WSGI middlewares
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 11601
diff changeset
    32
def settings_from_cwconfig(cwconfig):
2497bcf18030 split collecting setting before using them, so the function can be reused when inserting WSGI middlewares
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 11601
diff changeset
    33
    '''
2497bcf18030 split collecting setting before using them, so the function can be reused when inserting WSGI middlewares
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 11601
diff changeset
    34
    Extract settings from pyramid.ini and pyramid-debug.ini (if in debug)
2497bcf18030 split collecting setting before using them, so the function can be reused when inserting WSGI middlewares
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 11601
diff changeset
    35
2497bcf18030 split collecting setting before using them, so the function can be reused when inserting WSGI middlewares
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 11601
diff changeset
    36
    Can be used to configure middleware WSGI with settings from pyramid.ini files
11534
ceb1a5baca4f [config] Read pyramid settings in a 'pyramid.ini' file
Christophe de Vienne <christophe@unlish.com>
parents: 11532
diff changeset
    37
11620
2497bcf18030 split collecting setting before using them, so the function can be reused when inserting WSGI middlewares
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 11601
diff changeset
    38
    :param cwconfig: A CubicWeb configuration
2497bcf18030 split collecting setting before using them, so the function can be reused when inserting WSGI middlewares
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 11601
diff changeset
    39
    :returns: A settings dictionnary
2497bcf18030 split collecting setting before using them, so the function can be reused when inserting WSGI middlewares
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 11601
diff changeset
    40
    '''
2497bcf18030 split collecting setting before using them, so the function can be reused when inserting WSGI middlewares
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 11601
diff changeset
    41
    settings_filenames = [os.path.join(cwconfig.apphome, 'pyramid.ini')]
2497bcf18030 split collecting setting before using them, so the function can be reused when inserting WSGI middlewares
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 11601
diff changeset
    42
    settings = {}
11501
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    43
    if cwconfig.debugmode:
11534
ceb1a5baca4f [config] Read pyramid settings in a 'pyramid.ini' file
Christophe de Vienne <christophe@unlish.com>
parents: 11532
diff changeset
    44
        settings_filenames.insert(
ceb1a5baca4f [config] Read pyramid settings in a 'pyramid.ini' file
Christophe de Vienne <christophe@unlish.com>
parents: 11532
diff changeset
    45
            0, os.path.join(cwconfig.apphome, 'pyramid-debug.ini'))
11811
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11702
diff changeset
    46
11501
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    47
        settings.update({
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    48
            'pyramid.debug_authorization': True,
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    49
            'pyramid.debug_notfound': True,
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    50
            'pyramid.debug_routematch': True,
11567
4f8aa5fcd5da [debug] The debug mode now set pyramid.reload_templates
Christophe de Vienne <christophe@unlish.com>
parents: 11564
diff changeset
    51
            'pyramid.reload_templates': True,
11501
fcf7f99fad4a Add a make_cubicweb_application function
Christophe de Vienne <christophe@unlish.com>
parents: 11492
diff changeset
    52
        })
11811
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11702
diff changeset
    53
11534
ceb1a5baca4f [config] Read pyramid settings in a 'pyramid.ini' file
Christophe de Vienne <christophe@unlish.com>
parents: 11532
diff changeset
    54
    for fname in settings_filenames:
ceb1a5baca4f [config] Read pyramid settings in a 'pyramid.ini' file
Christophe de Vienne <christophe@unlish.com>
parents: 11532
diff changeset
    55
        if os.path.exists(fname):
ceb1a5baca4f [config] Read pyramid settings in a 'pyramid.ini' file
Christophe de Vienne <christophe@unlish.com>
parents: 11532
diff changeset
    56
            cp = SafeConfigParser()
ceb1a5baca4f [config] Read pyramid settings in a 'pyramid.ini' file
Christophe de Vienne <christophe@unlish.com>
parents: 11532
diff changeset
    57
            cp.read(fname)
ceb1a5baca4f [config] Read pyramid settings in a 'pyramid.ini' file
Christophe de Vienne <christophe@unlish.com>
parents: 11532
diff changeset
    58
            settings.update(cp.items('main'))
ceb1a5baca4f [config] Read pyramid settings in a 'pyramid.ini' file
Christophe de Vienne <christophe@unlish.com>
parents: 11532
diff changeset
    59
            break
11811
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11702
diff changeset
    60
11620
2497bcf18030 split collecting setting before using them, so the function can be reused when inserting WSGI middlewares
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 11601
diff changeset
    61
    return settings
11503
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    62
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    63
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents: 11534
diff changeset
    64
def wsgi_application_from_cwconfig(
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents: 11534
diff changeset
    65
        cwconfig,
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents: 11534
diff changeset
    66
        profile=False, profile_output=None, profile_dump_every=None):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    67
    """ Build a WSGI application from a cubicweb configuration
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    68
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    69
    :param cwconfig: A CubicWeb configuration
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    70
    :param profile: Enable profiling. See :ref:`profiling`.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    71
    :param profile_output: Profiling output filename. See :ref:`profiling`.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    72
    :param profile_dump_every: Profiling number of requests before dumping the
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    73
                               stats. See :ref:`profiling`.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    74
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    75
    :returns: A fully operationnal WSGI application
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
    76
    """
11503
ddf61aa73384 Add a wsgi application factory suitable for wsgi servers.
Christophe de Vienne <christophe@unlish.com>
parents: 11501
diff changeset
    77
    config = make_cubicweb_application(cwconfig)
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents: 11534
diff changeset
    78
    profile = profile or asbool(config.registry.settings.get(
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents: 11534
diff changeset
    79
        'cubicweb.profile.enable', False))
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents: 11534
diff changeset
    80
    if profile:
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents: 11534
diff changeset
    81
        config.add_route('profile_ping', '_profile/ping')
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents: 11534
diff changeset
    82
        config.add_route('profile_cnx', '_profile/cnx')
11631
faf279e33298 Merge with pyramid-cubicweb
Yann Voté <yann.vote@logilab.fr>
parents: 11623
diff changeset
    83
        config.scan('cubicweb.pyramid.profile')
11511
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    84
    app = config.make_wsgi_app()
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    85
    # This replaces completely web/cors.py, which is not used by
11631
faf279e33298 Merge with pyramid-cubicweb
Yann Voté <yann.vote@logilab.fr>
parents: 11623
diff changeset
    86
    # cubicweb.pyramid anymore
11511
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    87
    app = wsgicors.CORS(
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    88
        app,
11532
6a1d0aa3ac85 Fix cors 'origin' parameter passing
Christophe de Vienne <christophe@unlish.com>
parents: 11522
diff changeset
    89
        origin=' '.join(cwconfig['access-control-allow-origin']),
11547
fd7d2033cd80 [cors] Fix 'headers' and 'methods' parameters
Christophe de Vienne <christophe@unlish.com>
parents: 11540
diff changeset
    90
        headers=', '.join(cwconfig['access-control-allow-headers']),
fd7d2033cd80 [cors] Fix 'headers' and 'methods' parameters
Christophe de Vienne <christophe@unlish.com>
parents: 11540
diff changeset
    91
        methods=', '.join(cwconfig['access-control-allow-methods']),
11511
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
    92
        credentials='true')
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents: 11534
diff changeset
    93
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents: 11534
diff changeset
    94
    if profile:
11631
faf279e33298 Merge with pyramid-cubicweb
Yann Voté <yann.vote@logilab.fr>
parents: 11623
diff changeset
    95
        from cubicweb.pyramid.profile import wsgi_profile
11535
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents: 11534
diff changeset
    96
        filename = profile_output or config.registry.settings.get(
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents: 11534
diff changeset
    97
            'cubicweb.profile.output', 'program.prof')
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents: 11534
diff changeset
    98
        dump_every = profile_dump_every or config.registry.settings.get(
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents: 11534
diff changeset
    99
            'cubicweb.profile.dump_every', 100)
dd875009cc47 [profile] Add a profiling tool
Christophe de Vienne <christophe@unlish.com>
parents: 11534
diff changeset
   100
        app = wsgi_profile(app, filename=filename, dump_every=dump_every)
11511
13e0f569684c Use 'wsgicors' for CORS handling.
Christophe de Vienne <christophe@unlish.com>
parents: 11503
diff changeset
   101
    return app
11522
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
   102
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
   103
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
   104
def wsgi_application(instance_name=None, debug=None):
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   105
    """ Build a WSGI application from a cubicweb instance name
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   106
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   107
    :param instance_name: Name of the cubicweb instance (optional). If not
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   108
                          provided, :envvar:`CW_INSTANCE` must exists.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   109
    :param debug: Enable/disable the debug mode. If defined to True or False,
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   110
                  overrides :envvar:`CW_DEBUG`.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   111
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   112
    The following environment variables are used if they exist:
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   113
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   114
    .. envvar:: CW_INSTANCE
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   115
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   116
        A CubicWeb instance name.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   117
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   118
    .. envvar:: CW_DEBUG
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   119
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   120
        If defined, the debugmode is enabled.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   121
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   122
    The function can be used as an entry-point for third-party wsgi containers.
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   123
    Below is a sample uswgi configuration file:
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   124
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   125
    .. code-block:: ini
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   126
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   127
        [uwsgi]
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   128
        http = 127.0.1.1:8080
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   129
        env = CW_INSTANCE=myinstance
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   130
        env = CW_DEBUG=1
11631
faf279e33298 Merge with pyramid-cubicweb
Yann Voté <yann.vote@logilab.fr>
parents: 11623
diff changeset
   131
        module = cubicweb.pyramid:wsgi_application()
11537
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   132
        virtualenv = /home/user/.virtualenvs/myvirtualenv
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   133
        processes = 1
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   134
        threads = 8
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   135
        stats = 127.0.0.1:9191
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   136
        plugins = http,python
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   137
caf268942436 Initial documentation.
Christophe de Vienne <christophe@unlish.com>
parents: 11535
diff changeset
   138
    """
11522
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
   139
    if instance_name is None:
11623
b6f6737d4823 wsgi: clearer exception when CW_INSTANCE is missing
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents: 11620
diff changeset
   140
        instance_name = os.environ['CW_INSTANCE']
11522
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
   141
    if debug is None:
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
   142
        debug = 'CW_DEBUG' in os.environ
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
   143
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
   144
    cwconfig = cwcfg.config_for(instance_name, debugmode=debug)
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
   145
568204930c85 Provides a full wsgi cubicweb application builder
Christophe de Vienne <christophe@unlish.com>
parents: 11511
diff changeset
   146
    return wsgi_application_from_cwconfig(cwconfig)
11586
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   147
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   148
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   149
def includeme(config):
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   150
    """Set-up a CubicWeb instance.
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   151
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   152
    The CubicWeb instance can be set in several ways:
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   153
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   154
    -   Provide an already loaded CubicWeb config instance in the registry:
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   155
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   156
        .. code-block:: python
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   157
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   158
            config.registry['cubicweb.config'] = your_config_instance
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   159
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   160
    -   Provide an instance name in the pyramid settings with
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   161
        :confval:`cubicweb.instance`.
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   162
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   163
    """
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   164
    cwconfig = config.registry.get('cubicweb.config')
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   165
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   166
    if cwconfig is None:
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   167
        debugmode = asbool(
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   168
            config.registry.settings.get('cubicweb.debug', False))
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   169
        cwconfig = cwcfg.config_for(
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   170
            config.registry.settings['cubicweb.instance'], debugmode=debugmode)
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   171
        config.registry['cubicweb.config'] = cwconfig
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   172
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   173
    if cwconfig.debugmode:
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   174
        try:
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   175
            config.include('pyramid_debugtoolbar')
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   176
        except ImportError:
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   177
            warn('pyramid_debugtoolbar package not available, install it to '
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   178
                 'get UI debug features', RuntimeWarning)
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   179
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   180
    config.registry['cubicweb.repository'] = repo = cwconfig.repository()
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   181
    config.registry['cubicweb.registry'] = repo.vreg
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   182
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   183
    if asbool(config.registry.settings.get('cubicweb.defaults', True)):
11631
faf279e33298 Merge with pyramid-cubicweb
Yann Voté <yann.vote@logilab.fr>
parents: 11623
diff changeset
   184
        config.include('cubicweb.pyramid.defaults')
11586
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   185
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   186
    for name in aslist(config.registry.settings.get('cubicweb.includes', [])):
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   187
        config.include(name)
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   188
11631
faf279e33298 Merge with pyramid-cubicweb
Yann Voté <yann.vote@logilab.fr>
parents: 11623
diff changeset
   189
    config.include('cubicweb.pyramid.tools')
faf279e33298 Merge with pyramid-cubicweb
Yann Voté <yann.vote@logilab.fr>
parents: 11623
diff changeset
   190
    config.include('cubicweb.pyramid.predicates')
faf279e33298 Merge with pyramid-cubicweb
Yann Voté <yann.vote@logilab.fr>
parents: 11623
diff changeset
   191
    config.include('cubicweb.pyramid.core')
11702
be23c3813bbf [pyramid] Override cubicweb.hooks.syncsession.get_user_sessions() for Pyramid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11631
diff changeset
   192
    config.include('cubicweb.pyramid.syncsession')
11586
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   193
c7a25122af4d [config] Move most config code to a includeme()
Christophe de Vienne <christophe@unlish.com>
parents: 11567
diff changeset
   194
    if asbool(config.registry.settings.get('cubicweb.bwcompat', True)):
11631
faf279e33298 Merge with pyramid-cubicweb
Yann Voté <yann.vote@logilab.fr>
parents: 11623
diff changeset
   195
        config.include('cubicweb.pyramid.bwcompat')