source: main/waeup.kofa/trunk/src/waeup/kofa/startup.py @ 9847

Last change on this file since 9847 was 9217, checked in by uli, 12 years ago

Merge changes from uli-async-update back into trunk.

File size: 3.7 KB
RevLine 
[9217]1## $Id$
2##
3## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""WSGI application factories that additionally set environment vars.
19
20The `grokcore.startup` factories for creating WSGI applications
21currently do not support setting of arbitrary environment vars.
22
23The below factories add this feature.
24
25Environment vars can be set in any ``.ini`` file used at startup. In
26the ``[DEFAULT]`` section set the option ``env_vars`` with key/value
27pairs as value like so::
28
29  [DEFAULT]
30  zope_conf = <path-to-zope.conf>
31  env_vars = KEY1 value1
32             KEY2 value2
33
34This would set the env vars ``KEY1`` and ``KEY2`` to the respective
35values on startup on an instance.
36
37To activate these factories, in the ``setup.py`` of your project use::
38
39  [paste.app_factory]
40  main = waeup.kofa.startup:env_app_factory
41  debug = waeup.kofa.startup:env_debug_app_factory
42
43in the entry points section (replacing the references to respective
44`grokcore.startup` factories.
45
46Info for developers: paster on startup delivers the options from
47``[DEFAULT]`` section in `.ini` file as a dictionary in the
48`global_conf`.
49"""
50import os
51from ConfigParser import RawConfigParser
52from grokcore.startup import application_factory, debug_application_factory
53
54def _set_env_vars(global_conf):
55    """Set vars from `global_conf['env_vars']` in `os.environ`.
56    """
57    env_vars = global_conf.get('env_vars', None)
58    if not env_vars:
59        return
60    for line in env_vars.split('\n'):
61        key, val = [x.strip() for x in line.strip().split(' ', 1)]
62        os.environ[key] = val
63    return
64
65def env_app_factory(global_conf, **local_conf):
66    """A WSGI application factory that sets environment vars.
67
68    This app factory provides applications as expected by ``paster``
69    and useable as ``[paste.app_factory]`` plugin in setup.py.
70
71    It's a replacement for the stock app factory provided by
72    `grokcore.startup`.
73
74    Additionally it supports extrapolation of the DEFAULT var
75    ``env_vars`` in .ini files used to configure paster.
76
77    With this factory you can set enviroment vars (as in `os.environ`)
78    via the ``env_vars`` keyword set in some `.ini` file::
79
80      env_vars = MY_KEY some_value
81
82    would set the environment variable ``MY_KEY`` to the value
83    ``some_value`` before creating the actual app.
84
85    You can also set multiple keys/values at once like this::
86
87      env_vars = MY_KEY1  Some value
88                 Another_key Anoter_value
89
90    Note, that keys may not contain whitespaces while values
91    may. Both, keys and values, are stripped before being set.
92    """
93    _set_env_vars(global_conf)
94    return application_factory(global_conf, **local_conf)
95
96def env_debug_app_factory(global_conf, **local_conf):
97    """A debugger application factory.
98
99    This is a wrapper around the real factory from `grokcore.startup`
100    that does the same additional things as :func:`env_app_factory`:
101    it sets environment vars given in `env_vars` option of a
102    configuring .ini file for paster.
103    """
104    _set_env_vars(global_conf)
105    return debug_application_factory(global_conf, **local_conf)
Note: See TracBrowser for help on using the repository browser.