Simple module to load and use configuration in a clean, 'pythonic' way.
Project description
confidence.py :+1:
Confidence makes it easy to load one or multiple sources of configuration values and exposes them as a simple to use Python object. Given the following YAML file:
foo:
bar: 42
foo.baz: '21 is only half the answer'
foobar: the answer is ${foo.bar}…
Use it with confidence:
# load configuration from a YAML file
configuration = confidence.loadf('path/to/configuration.yaml')
# a Configuration object is like a dict, but better
value = configuration.get('foo.bar')
value = configuration.get('foo.bar', default=42)
# or even kwargs, should you want to
# (passing bar=42 and foo='21 is only half the answer')
function(**configuration.foo)
# namespaces are one honking great idea -- let's do more of those!
value = configuration.foo.bar
# they're even safe when values might be missing
value = configuration.foo.whoopsie
if value is NotConfigured:
value = 42
# or, similar
value = configuration.foo.whoopsie or 42
# even references to other configured values will work
value = configuration.foobar # 'the answer is 42…'
Often, combining multiple sources of configuration can be useful when defining defaults or reading from multiple files:
configuration = confidence.loadf('/etc/system-wide-defaults.yaml',
'./local-overrides.yaml')
# confidence provides a convenient way of using this kind of precedence,
# letting 'more local' files take precedence over system-wide sources
# load_name will attempt to load multiple files, skipping ones that
# don't exist (using typical *nix paths, XDG-specified locations, some
# Windows environment variables and typical OSX paths):
# - /etc/xdg/app.yaml
# - /etc/app.yaml
# - /Library/Preferences/app.yaml
# - C:/ProgramData/app.yaml
# - ~/.config/app.yaml
# - ~/Library/Preferences/app.yaml
# - ~/AppData/Roaming/app.yaml
# - ~/.app.yaml
# - ./app.yaml
configuration = confidence.load_name('app')
# if set, load_name will take a look at environment variables like
# APP_FOO_BAR and APP_FOO_BAZ, mixing those in as foo.bar and foo.baz
# the default load order can be overridden if necessary:
configuration = confidence.load_name('app', load_order=confidence.loaders(
# loading system after user makes system locations take precedence
confidence.Locality.user, confidence.Locality.system
))
While powerful, no set of convenience functions will ever satisfy everyone’s use case. To be able to serve as wide an audience as possible, confidence doesn’t hide away its flexible internal API.
# let's say application defaults are available as a dict in source
app_defaults = {'foo': {'bar': 42},
'foo.baz': '21 is only half the answer'}
# and we've already created a way to read a dict from somewhere
def read_from_source(name):
...
return read_values
# all of this can be combined to turn it into a single glorious Configuration instance
# precedence rules apply here, values from read_from_source will overwrite both
# app_defaults and values read from file
configuration = confidence.Configuration(app_defaults,
# yeah, this would be a Configuration instance
# remember it's just like a dict?
confidence.loadf('path/to/app.yaml'),
read_from_source('app'))
# make it so, no. 1
run_app(configuration)
Changes
0.6.1 (2019-04-12)
Fix resolving references during loading when sources passed to Configuration are Configuration instances themselves.
0.6 (2019-04-05)
Add Missing policy to control what to do with unconfigured keys on attribute access
Split single-file module into multi-module package (user-facing names importable from confidence package)
Raise errors when merging / splitting non-str type keys, avoiding issues with confusing and broken access patterns
0.5 (2019-02-01)
Enable referencing keys from values
Enable customizing load order for load_name through loaders and Locality (default behaviour remains unchanged)
0.4.1 (2018-11-26)
Warn about attribute access to configuration keys that collide with Configuration members.
0.4 (2018-07-09)
Enable escaping underscores in environment variables (NAME_FOO__BAR results in config.foo_bar).
Use yaml.safe_load to avoid security issues with yaml.load.
Raise AttributeError when attempting to set a non-protected attribute on a Configuration instance.
0.3 (2018-05-24)
Enable ignoring missing files in loadf.
Fix crashes when reading empty or comment-only yaml files.
0.2 (2018-03-06)
Read files from XDG-specified directories.
Read files form system-wide and user-local directories specified in environment variables PROGRAMDATA, APPDATA and LOCALAPPDATA (in that order).
Read files from /Library/Preferences and ~/Library/Preferences.
0.1.1 (2018-01-12)
Expand user dirs for arguments to loadf, including values for EXAMPLE_CONFIG_FILE environment variables.
0.1 (2017-12-18)
Initial release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for confidence-0.6.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75ad9bb4fff5c4b39653df045cb0a11e86094a2359801f1e45255a78113c599a |
|
MD5 | 1855047c9189c6262434e320c6558720 |
|
BLAKE2b-256 | a5a49c3ee541be8e4f1fcb6cea5a3b80abc242c32e017d4a7aca24dfe1c5c94d |