File: configuration.rst

package info (click to toggle)
python-bottle 0.12.19-1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,420 kB
  • sloc: python: 5,803; makefile: 77; sh: 63
file content (168 lines) | stat: -rw-r--r-- 6,255 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
=====================
Configuration (DRAFT)
=====================

.. currentmodule:: bottle

Bottle applications can store their configuration in :attr:`Bottle.config`, a dict-like object and central place for application specific settings. This dictionary controls many aspects of the framework, tells (newer) plugins what to do, and can be used to store your own configuration as well.

Configuration Basics
====================

The :attr:`Bottle.config` object behaves a lot like an ordinary dictionary. All the common dict methods work as expected. Let us start with some examples::

    import bottle
    app = bottle.default_app()             # or bottle.Bottle() if you prefer

    app.config['autojson']    = False      # Turns off the "autojson" feature
    app.config['sqlite.db']   = ':memory:' # Tells the sqlite plugin which db to use
    app.config['myapp.param'] = 'value'    # Example for a custom config value.

    # Change many values at once
    app.config.update({
        'autojson': False,
        'sqlite.db': ':memory:',
        'myapp.param': 'value'
    })

    # Add default values
    app.config.setdefault('myapp.param2', 'some default')

    # Receive values
    param  = app.config['myapp.param']
    param2 = app.config.get('myapp.param2', 'fallback value')

    # An example route using configuration values
    @app.route('/about', view='about.rst')
    def about():
        email = app.config.get('my.email', 'nomail@example.com')
        return {'email': email}

The app object is not always available, but as long as you are within a request context, you can use the `request` object to get the current application and its configuration::

    from bottle import request
    def is_admin(user):
        return user == request.app.config['myapp.admin_user']

Naming Convention
=================

To make life easier, plugins and applications should follow some simple rules when it comes to config parameter names:

- All keys should be lowercase strings and follow the rules for python identifiers (no special characters but the underscore).
- Namespaces are separated by dots (e.g. ``namespace.field`` or ``namespace.subnamespace.field``).
- Bottle uses the root namespace for its own configuration. Plugins should store all their variables in their own namespace (e.g. ``sqlite.db`` or ``werkzeug.use_debugger``).
- Your own application should use a separate namespace (e.g. ``myapp.*``).


Loading Configuration from a File
=================================

.. versionadded 0.12

Configuration files are useful if you want to enable non-programmers to configure your application,
or just don't want to hack python module files just to change the database port. A very common syntax for configuration files is shown here:

.. code-block:: ini

    [bottle]
    debug = True

    [sqlite]
    db = /tmp/test.db
    commit = auto

    [myapp]
    admin_user = defnull

With :meth:`ConfigDict.load_config` you can load these ``*.ini`` style configuration
files from disk and import their values into your existing configuration::

    app.config.load_config('/etc/myapp.conf')

Loading Configuration from a nested :class:`dict`
=================================================

.. versionadded 0.12

Another useful method is :meth:`ConfigDict.load_dict`. This method takes
an entire structure of nested dictionaries and turns it into a flat list of keys and values with namespaced keys::

    # Load an entire dict structure
    app.config.load_dict({
        'autojson': False,
        'sqlite': { 'db': ':memory:' },
        'myapp': {
            'param': 'value',
            'param2': 'value2'
        }
    })
    
    assert app.config['myapp.param'] == 'value'

    # Load configuration from a json file
    with open('/etc/myapp.json') as fp:
        app.config.load_dict(json.load(fp))


Listening to configuration changes
==================================

.. versionadded 0.12

The ``config`` hook on the application object is triggered each time a value in :attr:`Bottle.config` is changed. This hook can be used to react on configuration changes at runtime, for example reconnect to a new database, change the debug settings on a background service or resize worker thread pools. The hook callback receives two arguments (key, new_value) and is called before the value is actually changed in the dictionary. Raising an exception from a hook callback cancels the change and the old value is preserved.

::

  @app.hook('config')
  def on_config_change(key, value):
    if key == 'debug':
        switch_own_debug_mode_to(value)

The hook callbacks cannot *change* the value that is to be stored to the dictionary. That is what filters are for.


.. conf-meta:

Filters and other Meta Data
===========================

.. versionadded 0.12

:class:`ConfigDict` allows you to store meta data along with configuration keys. Two meta fields are currently defined:

help
    A help or description string. May be used by debugging, introspection or
    admin tools to help the site maintainer configuring their application.

filter
    A callable that accepts and returns a single value. If a filter is defined for a key, any new value stored to that key is first passed through the filter callback. The filter can be used to cast the value to a different type, check for invalid values (throw a ValueError) or trigger side effects.

This feature is most useful for plugins. They can validate their config parameters or trigger side effects using filters and document their configuration via ``help`` fields::

    class SomePlugin(object):
        def setup(app):
            app.config.meta_set('some.int', 'filter', int)
            app.config.meta_set('some.list', 'filter',
                lambda val: str(val).split(';'))
            app.config.meta_set('some.list', 'help',
                'A semicolon separated list.')

        def apply(self, callback, route):
            ...

    import bottle
    app = bottle.default_app()
    app.install(SomePlugin())

    app.config['some.list'] = 'a;b;c'     # Actually stores ['a', 'b', 'c']
    app.config['some.int'] = 'not an int' # raises ValueError


API Documentation
=================

.. versionadded 0.12

.. autoclass:: ConfigDict
   :members: