项目作者: RobertoPrevato

项目描述 :
Key-value pair based configuration for Python applications.
高级语言: Python
项目地址: git://github.com/RobertoPrevato/roconfiguration.git
创建时间: 2018-09-14T19:47:38Z
项目社区:https://github.com/RobertoPrevato/roconfiguration

开源协议:MIT License

下载


Build
pypi
versions
codecov
license

Python configuration utilities

Implementation of key-value pair based configuration for Python applications.


:warning: Please use essentials-configuration instead of this library. This library won’t be extended with more features.


Features:

  • support for most common sources of application settings
  • support for overriding settings in sequence
  • support for nested structures and lists, using attribute notation
  • strategy to use environment specific settings

This library is freely inspired by .NET Core Microsoft.Extensions.Configuration namespace and its pleasant design (ref. MSDN documentation, Microsoft Extensions Configuration Deep Dive).

The main class is influenced by Luciano Ramalho`s example of
JSON structure explorer using attribute notation, in his book Fluent Python.

Supported sources:

  • yaml files
  • json files
  • ini files
  • environmental variables
  • dictionaries
  • keys and values

Installation

  1. pip install roconfiguration

Examples

YAML file and environmental variables

In this example, configuration will be comprised of anything inside a file
settings.yaml and environmental variables. Settings are applied in order, so
environmental variables with matching name override values from the yaml
file.

  1. from roconfiguration import Configuration
  2. config = Configuration()
  3. config.add_yaml_file("settings.yaml")
  4. config.add_environmental_variables()

YAML file, optional file by environment

In this example, if an environmental variable with name APP_ENVIRONMENT and
value dev exists, and a configuration file with name settings.dev.yaml is
present, it is read to override values configured in settings.yaml file.

  1. import os
  2. from roconfiguration import Configuration
  3. environment_name = os.environ["APP_ENVIRONMENT"]
  4. config = Configuration()
  5. config.add_yaml_file("settings.yaml")
  6. config.add_yaml_file(f"settings.{environment_name}.yaml", optional=True)
  7. config.add_environmental_variables()

Filtering environmental variables by prefix

  1. import os
  2. from roconfiguration import Configuration
  3. config = Configuration()
  4. # will read only environmental variables
  5. # starting with "APP_", case insensitively
  6. config.add_environmental_variables("APP_")

Ini files

Ini files are parsed using the built-in configparser module, therefore
support [DEFAULT] section; all values are kept as strings.

  1. from roconfiguration import Configuration
  2. config = Configuration()
  3. config.add_ini_file("settings.ini")

JSON files

JSON files are parsed using the built-in json module.

  1. from roconfiguration import Configuration
  2. config = Configuration()
  3. config.add_json_file("settings.json")

Dictionaries

  1. from roconfiguration import Configuration
  2. config = Configuration({"host": "localhost", "port": 8080})
  3. config.add_map({"hello": "world", "example": [{"id": 1}, {"id": 2}]})
  4. assert config.host == "localhost"
  5. assert config.port == 8080
  6. assert config.hello == "world"
  7. assert config.example[0].id == 1
  8. assert config.example[1].id == 2

Keys and values

  1. from roconfiguration import Configuration
  2. config = Configuration({"host": "localhost", "port": 8080})
  3. config.add_value("port", 44555)
  4. assert config.host == "localhost"
  5. assert config.port == 44555

Overriding nested values

  1. config = Configuration(
  2. {
  3. "a": {
  4. "b": 1,
  5. "c": 2,
  6. "d": {
  7. "e": 3,
  8. "f": 4,
  9. },
  10. }
  11. }
  12. )
  13. assert config.a.b == 1
  14. assert config.a.d.e == 3
  15. assert config.a.d.f == 4
  16. config.add_value("a:d:e", 5)
  17. assert config.a.d.e == 5
  18. assert config.a.d.f == 4

Overriding nested values using env variables

  1. config = Configuration(
  2. {
  3. "a": {
  4. "b": 1,
  5. "c": 2,
  6. "d": {
  7. "e": 3,
  8. "f": 4,
  9. },
  10. }
  11. }
  12. )
  13. assert config.a.b == 1
  14. assert config.a.d.e == 3
  15. assert config.a.d.f == 4
  16. # NB: if an env variable such as:
  17. # a:d:e=5
  18. # or...
  19. # a__d__e=5
  20. #
  21. # is defined, it overrides the value from the dictionary
  22. config.add_environmental_variables()
  23. assert config.a.d.e == 5

Overriding values in list items using env variables

  1. config = Configuration(
  2. {
  3. "b2c": [
  4. {"tenant": "1"},
  5. {"tenant": "2"},
  6. {"tenant": "3"},
  7. ]
  8. }
  9. )
  10. config.add_value("b2c:1:tenant", "4")
  11. assert config.b2c[0].tenant == "1"
  12. assert config.b2c[1].tenant == "4"
  13. assert config.b2c[2].tenant == "3"

Develop and run tests locally

  1. pip install -r requirements.txt
  2. # run tests using automatic discovery:
  3. pytest