项目作者: NetherlandsForensicInstitute

项目描述 :
Simple configuration usage module for python apps
高级语言: Python
项目地址: git://github.com/NetherlandsForensicInstitute/confidence.git
创建时间: 2014-02-02T21:28:02Z
项目社区:https://github.com/NetherlandsForensicInstitute/confidence

开源协议:Apache License 2.0

下载


Build Status
PyPI Version

confidence 😎

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:

  1. foo:
  2. bar: 42
  3. foo.baz: '21 is only half the answer'
  4. foobar: the answer is ${foo.bar}…

Use it with confidence:

  1. # load configuration from a YAML file
  2. configuration = confidence.loadf('path/to/configuration.yaml')
  3. # a Configuration object is like a read-only dict, but better
  4. value = configuration.get('foo.bar')
  5. value = configuration.get('foo.bar', default=42)
  6. # or even kwargs, should you want to
  7. # (passing bar=42 and baz='21 is only half the answer')
  8. function(**configuration.foo)
  9. # namespaces are one honking great idea -- let's do more of those!
  10. value = configuration.foo.bar
  11. # they're even safe when values might be missing
  12. value = configuration.foo.whoopsie
  13. if value is NotConfigured:
  14. value = 42
  15. # or, similar
  16. value = configuration.foo.whoopsie or 42
  17. # even references to other configured values will work
  18. value = configuration.foobar # 'the answer is 42…'

Often, combining multiple sources of configuration can be useful when
defining defaults or reading from multiple files:

  1. configuration = confidence.loadf('/etc/system-wide-defaults.yaml',
  2. './local-overrides.yaml')
  3. # confidence provides a convenient way of using this kind of precedence,
  4. # letting 'more local' files take precedence over system-wide sources
  5. # load_name will attempt to load multiple files, skipping ones that
  6. # don't exist (using typical *nix paths, XDG-specified locations, some
  7. # Windows environment variables and typical OSX paths):
  8. # - /etc/xdg/app.yaml
  9. # - /etc/app.yaml
  10. # - /Library/Preferences/app.yaml
  11. # - C:/ProgramData/app.yaml
  12. # - ~/.config/app.yaml
  13. # - ~/Library/Preferences/app.yaml
  14. # - ~/AppData/Roaming/app.yaml
  15. # - ~/.app.yaml
  16. # - ./app.yaml
  17. configuration = confidence.load_name('app')
  18. # if set, load_name will take a look at environment variables like
  19. # APP_FOO_BAR and APP_FOO_BAZ, mixing those in as foo.bar and foo.baz
  20. # the default load order can be overridden if necessary:
  21. configuration = confidence.load_name('app', load_order=confidence.loaders(
  22. # loading system after user makes system locations take precedence
  23. confidence.Locality.USER, confidence.Locality.SYSTEM
  24. ))

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.

  1. # let's say application defaults are available as a dict in source
  2. app_defaults = {'foo': {'bar': 42},
  3. 'foo.baz': '21 is only half the answer'}
  4. # and we've already created a way to read a dict from somewhere
  5. def read_from_source(name):
  6. ...
  7. return read_values
  8. # all of this can be combined to turn it into a single glorious Configuration instance
  9. # precedence rules apply here, values from read_from_source will overwrite both
  10. # app_defaults and values read from file
  11. configuration = confidence.Configuration(app_defaults,
  12. # yeah, this would be a Configuration instance
  13. # remember it's just like a dict?
  14. confidence.loadf('path/to/app.yaml'),
  15. read_from_source('app'))
  16. # make it so, no. 1
  17. run_app(configuration)