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

Last change on this file since 17898 was 17327, checked in by uli, 22 months ago

Upgrade grokcore.startup

while still keeping the possibility to use zdaemon. This change requires
rerunning buildout.

  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1## $Id: startup.py 17327 2023-01-31 16:15:54Z uli $
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
51import sys
52import zdaemon.zdctl
53from ConfigParser import RawConfigParser
54from grokcore.startup import (
55        application_factory, debug_application_factory,
56        interactive_debug_prompt)
57
58
59def _set_env_vars(global_conf):
60    """Set vars from `global_conf['env_vars']` in `os.environ`.
61    """
62    env_vars = global_conf.get('env_vars', None)
63    if not env_vars:
64        return
65    for line in env_vars.split('\n'):
66        key, val = [x.strip() for x in line.strip().split(' ', 1)]
67        os.environ[key] = val
68    return
69
70
71def env_app_factory(global_conf, **local_conf):
72    """A WSGI application factory that sets environment vars.
73
74    This app factory provides applications as expected by ``paster``
75    and useable as ``[paste.app_factory]`` plugin in setup.py.
76
77    It's a replacement for the stock app factory provided by
78    `grokcore.startup`.
79
80    Additionally it supports extrapolation of the DEFAULT var
81    ``env_vars`` in .ini files used to configure paster.
82
83    With this factory you can set enviroment vars (as in `os.environ`)
84    via the ``env_vars`` keyword set in some `.ini` file::
85
86      env_vars = MY_KEY some_value
87
88    would set the environment variable ``MY_KEY`` to the value
89    ``some_value`` before creating the actual app.
90
91    You can also set multiple keys/values at once like this::
92
93      env_vars = MY_KEY1  Some value
94                 Another_key Anoter_value
95
96    Note, that keys may not contain whitespaces while values
97    may. Both, keys and values, are stripped before being set.
98    """
99    _set_env_vars(global_conf)
100    return application_factory(global_conf, **local_conf)
101
102
103def env_debug_app_factory(global_conf, **local_conf):
104    """A debugger application factory.
105
106    This is a wrapper around the real factory from `grokcore.startup`
107    that does the same additional things as :func:`env_app_factory`:
108    it sets environment vars given in `env_vars` option of a
109    configuring .ini file for paster.
110    """
111    _set_env_vars(global_conf)
112    return debug_application_factory(global_conf, **local_conf)
113
114
115class ControllerCommands(zdaemon.zdctl.ZDCmd):
116
117    def do_debug(self, rest):
118        zope_conf = os.path.join('parts', 'etc', 'zope.conf')
119        del sys.argv[0]
120        interactive_debug_prompt(zope_conf=zope_conf)
121
122    def help_debug(self):
123        print("debug -- Initialize the application, providing a debugger")
124        print("         object at an interactive Python prompt.")
125
126
127def zdaemon_controller(zdaemon_conf=os.path.join('parts', 'etc',
128                                                 'zdaemon.conf')):
129    args = ['-C', zdaemon_conf] + sys.argv[1:]
130    zdaemon.zdctl.main(args, options=None, cmdclass=ControllerCommands)
Note: See TracBrowser for help on using the repository browser.