Package: confidence
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:foo:bar:42foo.baz:'21isonlyhalftheanswer'foobar:the answer is ${foo.bar}…Use it with confidence:# load configuration from a YAML fileconfiguration=confidence.loadf('path/to/configuration.yaml')# a Configuration object is like a read-only dict, but bettervalue=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 missingvalue=configuration.foo.whoopsieifvalueisNotConfigured:value=42# or, similarvalue=configuration.foo.whoopsieor42# even references to other configured values will workvalue=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.yamlconfiguration=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 precedenceconfidence.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 sourceapp_defaults={'foo':{'bar':42},'foo.baz':'21 is only half the answer'}# and we've already created a way to read a dict from somewheredefread_from_source(name):...returnread_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 fileconfiguration=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. 1run_app(configuration)Changes0.15 (2023-06-26)Addunwrapfunction to the public API, unwrapping aConfigurationobject into a plaindict(note that references are not resolved and will remain references in the result).Change string-representations (result ofrepr()) ofConfigurationandConfigurationSequenceto be more like builtin types.0.14 (2023-02-28)Add system-wide.../name/name.yamlpaths to the default load order, aiding in the use configurationdirectories(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 ofConfiguration.0.12 (2022-03-01)Use named loggers, defaultconfidence.*library loggers to silence asdescribed in the docs.Resolve references in sequences.0.11 (2021-11-25)Parse values of environment variables as YAML values (e.g.NAME_KEY=yeswill result inkeybeingTrue).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 (likeLocality.USER) to be upper case.Adddump,dumpfanddumpsfunctions to dumpConfigurationinstances to YAML format.0.9 (2021-02-01)Add type hints to confidence.0.8 (2020-12-14)Add human-readablereprs toConfigurationandConfigurationSequence.MakeConfigurationSequencemore 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 retainingConfigurationfunctionality.0.6.3 (2020-01-14)Restrict reference pattern to make a nested pattern work.0.6.2 (2019-11-25)MakeConfigurationinstances picklable.0.6.1 (2019-04-12)Fix resolving references during loading when sources passed toConfigurationareConfigurationinstances themselves.0.6 (2019-04-05)AddMissingpolicy to control what to do with unconfigured keys on attribute access.Split single-file module into multi-module package (user-facing names importable fromconfidencepackage).Raise errors when merging / splitting non-strtype keys, avoiding issues with confusing and broken access patterns.0.5 (2019-02-01)Enable referencing keys from values.Enable customizing load order forload_namethroughloadersandLocality(default behaviour remains unchanged).0.4.1 (2018-11-26)Warn about attribute access to configuration keys that collide withConfigurationmembers.0.4 (2018-07-09)Enable escaping underscores in environment variables (NAME_FOO__BARresults inconfig.foo_bar).Useyaml.safe_loadto avoid security issues withyaml.load.RaiseAttributeErrorwhen attempting to set a non-protected attribute on aConfigurationinstance.0.3 (2018-05-24)Enable ignoring missing files inloadf.Fix crashes when reading empty or comment-only yaml files.0.2 (2018-03-06)Read files fromXDG-specifieddirectories.Read files form system-wide and user-local directories specified in environment variablesPROGRAMDATA,APPDATAandLOCALAPPDATA(in that order).Read files from/Library/Preferencesand~/Library/Preferences.0.1.1 (2018-01-12)Expand user dirs for arguments toloadf, including values forEXAMPLE_CONFIG_FILEenvironment variables.0.1 (2017-12-18)Initial release.