confidence


Nameconfidence JSON
Version 0.15 PyPI version JSON
download
home_pagehttps://github.com/NetherlandsForensicInstitute/confidence/
SummarySimple module to load and use configuration in a clean, 'pythonic' way.
upload_time2023-06-26 12:31:06
maintainer
docs_urlNone
authorNetherlands Forensic Institute
requires_python
licenseApache Software License 2.0
keywords configuration
VCS
bugtrack_url
requirements pyyaml
Travis-CI No Travis.
coveralls test coverage No coveralls.
            confidence :+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:

~~~~ yaml
foo:
  bar: 42

foo.baz: '21 is only half the answer'

foobar: the answer is ${foo.bar}…
~~~~

Use it with confidence:

~~~~ python
# load configuration from a YAML file
configuration = confidence.loadf('path/to/configuration.yaml')

# a Configuration object is like a read-only 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:

~~~~ python
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.

~~~~ python
# 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.15 (2023-06-26)
-----------------

- Add `unwrap` function to the public API, unwrapping a `Configuration` object into a plain `dict` (note that references are not resolved and will remain references in the result).
- Change string-representations (result of `repr()`) of `Configuration` and `ConfigurationSequence` to be more like builtin types.

0.14 (2023-02-28)
-----------------

- Add system-wide `.../name/name.yaml` paths to the default load order, aiding in the use configuration *directories* (e.g. in containerized setups).
- Ensure non-confidence values can be dumped, enabling dumping of arbitrary bits of configuration.

0.13 (2023-01-02)
-----------------

- Avoid checking for existence of files, try to open them instead.
- Fix dumping / serialization issues by unwrapping complex wrapper types to their simple counterparts during initialization of `Configuration`.

0.12 (2022-03-01)
-----------------

- Use named loggers, default `confidence.*` library loggers to silence as [described in the docs](https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-library).
- Resolve references in sequences.

0.11 (2021-11-25)
-----------------

- Parse values of environment variables as YAML values (e.g. `NAME_KEY=yes` will result in `key` being `True`).
- Add INFO-level logging of files and environment variables being used to load configuration.

0.10 (2021-08-04)
-----------------

- Remove configurable key separator, hardcode the default.
- Rename enumeration values (like `Locality.USER`) to be upper case.
- Add `dump`, `dumpf` and `dumps` functions to dump `Configuration` instances to YAML format.

0.9 (2021-02-01)
----------------

- Add type hints to confidence.

0.8 (2020-12-14)
----------------

- Add human-readable `repr`s to `Configuration` and `ConfigurationSequence`.
- Make `ConfigurationSequence` more list-like by enabling addition operator (`configured_sequence + [1, 2, 3]` or `(1, 2, 3) + configured_sequence`).

0.7 (2020-07-10)
----------------

- Auto-wrap configured sequences to enable 'list-of-dicts' style configuration while retaining `Configuration` functionality.

0.6.3 (2020-01-14)
------------------

- Restrict reference pattern to make a nested pattern work.

0.6.2 (2019-11-25)
------------------

- Make `Configuration` instances picklable.

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](https://specifications.freedesktop.org/basedir-spec/latest/) 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.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/NetherlandsForensicInstitute/confidence/",
    "name": "confidence",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "configuration",
    "author": "Netherlands Forensic Institute",
    "author_email": "holmesnl@users.noreply.github.com",
    "download_url": "",
    "platform": null,
    "description": "confidence :+1:\n===============\n\nConfidence makes it easy to load one or multiple sources of\nconfiguration values and exposes them as a simple to use Python object.\nGiven the following YAML file:\n\n~~~~ yaml\nfoo:\n  bar: 42\n\nfoo.baz: '21 is only half the answer'\n\nfoobar: the answer is ${foo.bar}\u2026\n~~~~\n\nUse it with confidence:\n\n~~~~ python\n# load configuration from a YAML file\nconfiguration = confidence.loadf('path/to/configuration.yaml')\n\n# a Configuration object is like a read-only dict, but better\nvalue = configuration.get('foo.bar')\nvalue = configuration.get('foo.bar', default=42)\n# or even kwargs, should you want to\n# (passing bar=42 and foo='21 is only half the answer')\nfunction(**configuration.foo)\n\n# namespaces are one honking great idea -- let's do more of those!\nvalue = configuration.foo.bar\n# they're even safe when values might be missing\nvalue = configuration.foo.whoopsie\nif value is NotConfigured:\n    value = 42\n# or, similar\nvalue = configuration.foo.whoopsie or 42\n\n# even references to other configured values will work\nvalue = configuration.foobar  # 'the answer is 42\u2026'\n~~~~\n\nOften, combining multiple sources of configuration can be useful when\ndefining defaults or reading from multiple files:\n\n~~~~ python\nconfiguration = confidence.loadf('/etc/system-wide-defaults.yaml',\n                                 './local-overrides.yaml')\n\n# confidence provides a convenient way of using this kind of precedence,\n# letting 'more local' files take precedence over system-wide sources\n# load_name will attempt to load multiple files, skipping ones that\n# don't exist (using typical *nix paths, XDG-specified locations, some\n# Windows environment variables and typical OSX paths):\n# - /etc/xdg/app.yaml\n# - /etc/app.yaml\n# - /Library/Preferences/app.yaml\n# - C:/ProgramData/app.yaml\n# - ~/.config/app.yaml\n# - ~/Library/Preferences/app.yaml\n# - ~/AppData/Roaming/app.yaml\n# - ~/.app.yaml\n# - ./app.yaml\n\nconfiguration = confidence.load_name('app')\n\n# if set, load_name will take a look at environment variables like\n# APP_FOO_BAR and APP_FOO_BAZ, mixing those in as foo.bar and foo.baz\n\n# the default load order can be overridden if necessary:\n\nconfiguration = confidence.load_name('app', load_order=confidence.loaders(\n    # loading system after user makes system locations take precedence\n    confidence.Locality.USER, confidence.Locality.SYSTEM\n))\n~~~~\n\nWhile powerful, no set of convenience functions will ever satisfy\neveryone's use case. To be able to serve as wide an audience as\npossible, confidence doesn't hide away its flexible internal API.\n\n~~~~ python\n# let's say application defaults are available as a dict in source\napp_defaults = {'foo': {'bar': 42},\n                'foo.baz': '21 is only half the answer'}\n\n# and we've already created a way to read a dict from somewhere\ndef read_from_source(name):\n    ...\n    return read_values\n\n# all of this can be combined to turn it into a single glorious Configuration instance\n# precedence rules apply here, values from read_from_source will overwrite both\n# app_defaults and values read from file\nconfiguration = confidence.Configuration(app_defaults,\n                                         # yeah, this would be a Configuration instance\n                                         # remember it's just like a dict?\n                                         confidence.loadf('path/to/app.yaml'),\n                                         read_from_source('app'))\n# make it so, no. 1\nrun_app(configuration)\n~~~~\n\n\n\nChanges\n=======\n\n0.15 (2023-06-26)\n-----------------\n\n- Add `unwrap` function to the public API, unwrapping a `Configuration` object into a plain `dict` (note that references are not resolved and will remain references in the result).\n- Change string-representations (result of `repr()`) of `Configuration` and `ConfigurationSequence` to be more like builtin types.\n\n0.14 (2023-02-28)\n-----------------\n\n- Add system-wide `.../name/name.yaml` paths to the default load order, aiding in the use configuration *directories* (e.g. in containerized setups).\n- Ensure non-confidence values can be dumped, enabling dumping of arbitrary bits of configuration.\n\n0.13 (2023-01-02)\n-----------------\n\n- Avoid checking for existence of files, try to open them instead.\n- Fix dumping / serialization issues by unwrapping complex wrapper types to their simple counterparts during initialization of `Configuration`.\n\n0.12 (2022-03-01)\n-----------------\n\n- Use named loggers, default `confidence.*` library loggers to silence as [described in the docs](https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-library).\n- Resolve references in sequences.\n\n0.11 (2021-11-25)\n-----------------\n\n- Parse values of environment variables as YAML values (e.g. `NAME_KEY=yes` will result in `key` being `True`).\n- Add INFO-level logging of files and environment variables being used to load configuration.\n\n0.10 (2021-08-04)\n-----------------\n\n- Remove configurable key separator, hardcode the default.\n- Rename enumeration values (like `Locality.USER`) to be upper case.\n- Add `dump`, `dumpf` and `dumps` functions to dump `Configuration` instances to YAML format.\n\n0.9 (2021-02-01)\n----------------\n\n- Add type hints to confidence.\n\n0.8 (2020-12-14)\n----------------\n\n- Add human-readable `repr`s to `Configuration` and `ConfigurationSequence`.\n- Make `ConfigurationSequence` more list-like by enabling addition operator (`configured_sequence + [1, 2, 3]` or `(1, 2, 3) + configured_sequence`).\n\n0.7 (2020-07-10)\n----------------\n\n- Auto-wrap configured sequences to enable 'list-of-dicts' style configuration while retaining `Configuration` functionality.\n\n0.6.3 (2020-01-14)\n------------------\n\n- Restrict reference pattern to make a nested pattern work.\n\n0.6.2 (2019-11-25)\n------------------\n\n- Make `Configuration` instances picklable.\n\n0.6.1 (2019-04-12)\n------------------\n\n- Fix resolving references during loading when sources passed to `Configuration` are `Configuration` instances themselves.\n\n0.6 (2019-04-05)\n----------------\n\n- Add `Missing` policy to control what to do with unconfigured keys on attribute access.\n- Split single-file module into multi-module package (user-facing names importable from `confidence` package).\n- Raise errors when merging / splitting non-`str` type keys, avoiding issues with confusing and broken access patterns.\n\n0.5 (2019-02-01)\n----------------\n\n- Enable referencing keys from values.\n- Enable customizing load order for `load_name` through `loaders` and `Locality` (default behaviour remains unchanged).\n\n0.4.1 (2018-11-26)\n------------------\n\n- Warn about attribute access to configuration keys that collide with `Configuration` members.\n\n0.4 (2018-07-09)\n----------------\n\n- Enable escaping underscores in environment variables (`NAME_FOO__BAR` results in `config.foo_bar`).\n- Use `yaml.safe_load` to avoid security issues with `yaml.load`.\n- Raise `AttributeError` when attempting to set a non-protected attribute on a `Configuration` instance.\n\n0.3 (2018-05-24)\n----------------\n\n- Enable ignoring missing files in `loadf`.\n- Fix crashes when reading empty or comment-only yaml files.\n\n0.2 (2018-03-06)\n----------------\n\n- Read files from [XDG-specified](https://specifications.freedesktop.org/basedir-spec/latest/) directories.\n- Read files form system-wide and user-local directories specified in environment variables `PROGRAMDATA`, `APPDATA` and `LOCALAPPDATA` (in that order).\n- Read files from `/Library/Preferences` and `~/Library/Preferences`.\n\n0.1.1 (2018-01-12)\n------------------\n\n- Expand user dirs for arguments to `loadf`, including values for `EXAMPLE_CONFIG_FILE` environment variables.\n\n0.1 (2017-12-18)\n----------------\n\n- Initial release.\n\n\n",
    "bugtrack_url": null,
    "license": "Apache Software License 2.0",
    "summary": "Simple module to load and use configuration in a clean, 'pythonic' way.",
    "version": "0.15",
    "project_urls": {
        "Homepage": "https://github.com/NetherlandsForensicInstitute/confidence/"
    },
    "split_keywords": [
        "configuration"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "278d678967505627685f9c433a74ee450e644b19895fbb4489383a397abb55fc",
                "md5": "7488b93ee4ad5045601ff39478cedb2f",
                "sha256": "90a5d2ad3ceb6bec7ad94af317c61c4ed40dd7573eaa57c7a0ad0c5319ee371d"
            },
            "downloads": -1,
            "filename": "confidence-0.15-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7488b93ee4ad5045601ff39478cedb2f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 20210,
            "upload_time": "2023-06-26T12:31:06",
            "upload_time_iso_8601": "2023-06-26T12:31:06.568956Z",
            "url": "https://files.pythonhosted.org/packages/27/8d/678967505627685f9c433a74ee450e644b19895fbb4489383a397abb55fc/confidence-0.15-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-26 12:31:06",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "NetherlandsForensicInstitute",
    "github_project": "confidence",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "pyyaml",
            "specs": [
                [
                    "==",
                    "6.0"
                ]
            ]
        }
    ],
    "lcname": "confidence"
}
        
Elapsed time: 0.11666s