Managing snap configuration

Snap configuration is managed through SnapConfig (which is also accessible as config in the Snap object).

To get configuration for a set of keys, SnapConfig.get_options() can be used. This returns a SnapConfigOptions instance, which allows accessing subkeys of the specified top-level keys with a dict-like interface.

It’s possible to use dotted-notation to access subkeys.

>>> from snaphelpers import SnapConfig
>>> config = SnapConfig()
>>> options = config.get_options('foo', 'asdf')
>>> options['foo']
{'bar': 'baz'}
>>> options['foo.bar']
'baz'
>>> options['asdf']
3
>>> options.as_dict()
{'asdf': 3, 'foo': {'bar': 'baz'}}

It’s also possible to get a single value for a key (at any level) with SnapConfig.get():

>>> config.get('foo.bar')
'baz'

Configuration options can be set in bulk by passing a dict with keys and values. Values can be of any JSON-serializable type, and keys can use the dotted-notation to only set certain subkeys:

>>> config.set({'foo.bar': 'baz', 'asdf': 3})

Note

calling SnapConfig.set() requires root access.