Configuration

Provides a simple woy to add settings to your application.

Management of loading of settings from different file types and merging into a simple easy to use settings object.

Usage:

>>> from pyapp.conf import settings
>>> # Configure default settings
>>> settings.configure('my_app.default_settings')

>>> settings.MY_CONFIG_VALUE
'foo'

The settings object also has helper methods to simplify your testing:

>>> from pyapp.conf import settings
>>> with settings.modify() as patch:
...     patch.MY_CONFIG_VALUE = 'bar'
...     settings.MY_CONFIG_VALUE
'bar'
>>> settings.MY_CONFIG_VALUE
'foo'

In addition to changing values new values can be added or existing values removed using the del keyword. Once the context has been exited all changes are reverted.

Note

All settings must be UPPER_CASE. If a setting is not upper case it will not be imported into the settings object.

Settings

class pyapp.conf.Settings(base_settings=<module 'pyapp.conf.default_settings' from '/home/docs/checkouts/readthedocs.org/user_builds/pyapp/checkouts/support-3.4/pyapp/conf/default_settings.py'>)[source]

Settings container

configure(application_settings, runtime_settings=None, additional_loaders=None, env_settings_key='PYAPP_SETTINGS')[source]

Configure the settings object

Parameters:
  • application_settings (str | unicode) – Your applications default settings file.
  • runtime_settings (str | unicode) – Settings defined for the current runtime (eg from the command line)
  • additional_loaders (list()) – Additional loaders to execute
  • env_settings_key (str | unicode) – Environment variable key used to override the runtime_settings.
is_configured

Settings have been configured.

load(loader, apply_method=<slot wrapper '__setitem__' of 'dict' objects>)[source]

Load settings from a loader instance. A loader is an iterator that yields key/value pairs.

See pyapp.conf.loaders.ModuleLoader as an example.

modify()[source]

Apply changes to settings file using a context manager that will roll back the changes on exit of a with block. Designed to simplify test cases.

This should be used with a context manager:

>>> settings = Settings()
>>> with settings.modify() as patch:
>>>     # Change a setting
>>>     patch.FOO = 'foo'
>>>     # Remove a setting
>>>     del patch.BAR
Return type:ModifySettingsContext

Loaders

Loaders are used to load settings from an external source, eg a Python module (using ModuleLoader).

A loader provides key/value pairs to the settings container to merge into the application settings.

ModuleLoader

class pyapp.conf.loaders.ModuleLoader(module)[source]

Load configuration from an importable module.

Loader will load all upper case attributes from the imported module.

Usage:

>>> loader = ModuleLoader("name.of.module")
>>> settings = dict(loader)

FileLoader

class pyapp.conf.loaders.file_loader.FileLoader(path, encoding='UTF8')[source]

Load settings from a file.

Currently only JSON is supported for settings.

Usage:

>>> loader = FileLoader('/path/to/settings.json')
>>> settings = dict(loader)