Skip to main content

Generic configuration mechanism

Project description

Confetti is a Python library for dealing with hierarchical configuration data.

Updating Paths

Setting paths is done by settings items:

>>> c['a'] = 3
>>> c.root.a
3

Setting paths that didn’t exist before is not allowed, unless you assign a config object:

>>> c['b'] = 3 #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
 ...
CannotSetValue: Cannot set key 'b'

>>> c['b'] = Config(2)
>>> c.root.b
2

Assigning can also be done via the root proxy:

>>> c.root.a = 3
>>> c.root.a
3

Backing Up/Restoring

Whenever you want to preserve the configuration prior to a change and restore it later, you can do it with backup() and restore(). They work like a stack, so they push and pop states:

>>> c = Config({"value":2})
>>> c['value']
2
>>> c.backup()
>>> c['value'] = 3
>>> c['value']
3
>>> c.backup()
>>> c['value'] = 4
>>> c['value']
4
>>> c.restore()
>>> c['value']
3
>>> c.restore()
>>> c['value']
2

Metadata

You can store metadata on config variables and paths. This is useful for documenting paths or for attaching arbitrary information:

>>> from confetti import Metadata
>>> c = Config({
...     "key" : "value" // Metadata(some_key="some_value"),
...     })

It can later be retrieved:

>>> c.get_config("key").metadata
{'some_key': 'some_value'}

Metadata can also be attached to config branches:

>>> c = Config({
...     "key" : {
...         "a" : 1,
...         "b" : 2,
...         } // Metadata(doc="this is a nested dict")
...     }) // Metadata(doc="and this is the root")
>>> c.metadata
{'doc': 'and this is the root'}
>>> c.get_config("key").metadata
{'doc': 'this is a nested dict'}

Utilities

Path Assignment

It is possible to assign to a config via path assignment, e.g:

>>> c = Config(dict(a=dict(b=dict(c=3))))
>>> c.assign_path("a.b.c", 4)
>>> c.root.a.b.c
4

Expression Path Assignment

In some cases you would like to receive strings like this:

a.b.c=2

And make sense of them in the context of the configuration. This might be because they originate from command line, overlay files, or whatever other source comes to mind. confetti’s utilities provide a function for this:

>>> from confetti.utils import assign_path_expression
>>> assign_path_expression(c, "a.b.c=2")
>>> c.root.a.b.c
'2'

Note that in this method, types are always strings. If your leaf already has a value, the deduce_type flag can be used to deduce the type from the current value:

>>> c['a']['b']['c'] = 3
>>> assign_path_expression(c, 'a.b.c=666', deduce_type=True)
>>> c.root.a.b.c
666

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

confetti-2.0.0.dev1.tar.gz (5.7 kB view details)

Uploaded Source

File details

Details for the file confetti-2.0.0.dev1.tar.gz.

File metadata

File hashes

Hashes for confetti-2.0.0.dev1.tar.gz
Algorithm Hash digest
SHA256 4998660ffe069ad54ab284db88a501cdce69b8aa6b1b6d49d7c89f6482edd368
MD5 9f953cc26219af56f8733c50b9724fb6
BLAKE2b-256 a4e69d4e83c2d7733b354e1fee98766e975453b7ee360fcad49dd94b28eac463

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page