Configuration

This module is deprecated. webapp2.WSGIApplication now has a simple configuration dictionary used by default, stored in webapp2.WSGIApplication.config. See also webapp2.Config.

webapp2_extras.config.DEFAULT_VALUE = <object object at 0x107363580>

Value used for missing default values.

webapp2_extras.config.REQUIRED_VALUE = <object object at 0x107363790>

Value used for required values.

class webapp2_extras.config.Config(values=None, defaults=None)[source]

A simple configuration dictionary keyed by module name. This is a dictionary of dictionaries. It requires all values to be dictionaries and applies updates and default values to the inner dictionaries instead of the first level one.

The configuration object can be set as a config attribute of WSGIApplication:

import webapp2
from webapp2_extras import config as webapp2_config

my_config = {}

my_config['my.module'] = {
    'foo': 'bar',
}

app = webapp2.WSGIApplication(routes=[
    webapp2.Route('/', name='home', handler=MyHandler)
])
app.config = webapp2_config.Config(my_config)

Then to read configuration values, get them from the app:

class MyHandler(RequestHandler):
    def get(self):
        foo = self.app.config['my.module']['foo']

        # ...
__getitem__(module)[source]

Returns the configuration for a module. If it is not already set, loads a default_config variable from the given module and updates the configuration with those default values

Every module that allows some kind of configuration sets a default_config global variable that is loaded by this function, cached and used in case the requested configuration was not defined by the user.

Parameters:module – The module name.
Returns:A configuration value.
__init__(values=None, defaults=None)[source]

Initializes the configuration object.

Parameters:
  • values – A dictionary of configuration dictionaries for modules.
  • defaults – A dictionary of configuration dictionaries for initial default values. These modules are marked as loaded.
__setitem__(module, values)[source]

Sets a configuration for a module, requiring it to be a dictionary.

Parameters:
  • module – A module name for the configuration, e.g.: webapp2.ext.i18n.
  • values – A dictionary of configurations for the module.
get(module, default=<object object at 0x107363580>)[source]

Returns a configuration for a module. If default is not provided, returns an empty dict if the module is not configured.

Parameters:module – The module name.
Params default:Default value to return if the module is not configured. If not set, returns an empty dict.
Returns:A module configuration.
get_config(module, key=None, default=<object object at 0x107363790>)[source]

Returns a configuration value for a module and optionally a key. Will raise a KeyError if they the module is not configured or the key doesn’t exist and a default is not provided.

Parameters:
  • module – The module name.
  • default – Default value to return if the key doesn’t exist.
Params key:

The configuration key.

Returns:

A module configuration.

setdefault(module, values)[source]

Sets a default configuration dictionary for a module.

Parameters:
  • module – The module to set default configuration, e.g.: webapp2.ext.i18n.
  • values – A dictionary of configurations for the module.
Returns:

The module configuration dictionary.

update(module, values)[source]

Updates the configuration dictionary for a module.

Parameters:
  • module – The module to update the configuration, e.g.: webapp2.ext.i18n.
  • values – A dictionary of configurations for the module.

Previous topic

Auth

Next topic

i18n

This Page