Configuration providers

Configuration provider provides configuration options to the other providers.

from dependency_injector import providers


config = providers.Configuration()

s3_client_factory = providers.Factory(
    boto3.client,
    's3',
    aws_access_key_id=config.aws.access_key_id,
    aws_secret_access_key=config.aws.secret_access_key,
)

It implements “use first, define later” principle.

Loading from ini file

Configuration provider can load configuration from ini file using Configuration.from_ini():

from dependency_injector import providers


config = providers.Configuration()

config.from_ini('examples/providers/configuration/config.ini')

assert config() == {'aws': {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}}
assert config.aws() == {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}
assert config.aws.access_key_id() == 'KEY'
assert config.aws.secret_access_key() == 'SECRET'

where examples/providers/configuration/config.ini is:

[aws]
access_key_id = KEY
secret_access_key = SECRET

Configuration.from_ini() supports environment variables interpolation. Use ${ENV_NAME} format in the configuration file to substitute value of environment variable ENV_NAME.

Loading from yaml file

Configuration provider can load configuration from yaml file using Configuration.from_yaml():

from dependency_injector import providers


config = providers.Configuration()

config.from_yaml('examples/providers/configuration/config.yml')

assert config() == {'aws': {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}}
assert config.aws() == {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}
assert config.aws.access_key_id() == 'KEY'
assert config.aws.secret_access_key() == 'SECRET'

where examples/providers/configuration/config.yml is:

aws:
  access_key_id: "KEY"
  secret_access_key: "SECRET"

Configuration.from_yaml() supports environment variables interpolation. Use ${ENV_NAME} format in the configuration file to substitute value of environment variable ENV_NAME.

Note

Loading configuration from yaml requires PyYAML package. You can install Dependency Injector with extras pip install dependency-injector[yaml] or install PyYAML separately pip install pyyaml.

Loading from dict

Configuration provider can load configuration from Python dict using Configuration.from_dict():

from dependency_injector import providers


config = providers.Configuration()

config.from_dict(
    {
        'aws': {
             'access_key_id': 'KEY',
             'secret_access_key': 'SECRET',
         },
    },
)

assert config() == {'aws': {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}}
assert config.aws() == {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}
assert config.aws.access_key_id() == 'KEY'
assert config.aws.secret_access_key() == 'SECRET'

Loading from environment variable

Configuration provider can load configuration from environment variable using Configuration.from_env():

from dependency_injector import providers


config = providers.Configuration()

config.aws.access_key_id.from_env('AWS_ACCESS_KEY_ID')
config.aws.secret_access_key.from_env('AWS_SECRET_ACCESS_KEY')
config.optional.from_env('UNDEFINED', 'default_value')

assert config.aws.access_key_id() == 'KEY'
assert config.aws.secret_access_key() == 'SECRET'
assert config.optional() == 'default_value'

Loading from multiple sources

Configuration provider can load configuration from multiple sources. Loaded configuration is merged recursively over existing configuration.

from dependency_injector import providers


config = providers.Configuration()

config.from_yaml('examples/providers/configuration/config.yml')
config.from_yaml('examples/providers/configuration/config.local.yml')

assert config() == {'aws': {'access_key_id': 'LOCAL-KEY', 'secret_access_key': 'LOCAL-SECRET'}}
assert config.aws() == {'access_key_id': 'LOCAL-KEY', 'secret_access_key': 'LOCAL-SECRET'}
assert config.aws.access_key_id() == 'LOCAL-KEY'
assert config.aws.secret_access_key() == 'LOCAL-SECRET'

where examples/providers/configuration/config.local.yml is:

aws:
  access_key_id: "LOCAL-KEY"
  secret_access_key: "LOCAL-SECRET"