2020-09-03 20:19:40 +00:00
|
|
|
.. _configuration-provider:
|
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
Configuration provider
|
2020-09-02 21:33:02 +00:00
|
|
|
======================
|
2020-09-02 01:39:23 +00:00
|
|
|
|
|
|
|
.. meta::
|
|
|
|
:keywords: Python,DI,Dependency injection,IoC,Inversion of Control,Configuration,Injection,
|
|
|
|
Option,Ini,Json,Yaml,Dict,Environment Variable,Load,Read,Get
|
|
|
|
:description: Configuration provides configuration options to the other providers. This page
|
|
|
|
demonstrates how to use Configuration provider to inject the dependencies, load
|
|
|
|
a configuration from an ini or yaml file, dictionary or an environment variable.
|
2020-06-25 21:12:16 +00:00
|
|
|
|
|
|
|
.. currentmodule:: dependency_injector.providers
|
|
|
|
|
|
|
|
:py:class:`Configuration` provider provides configuration options to the other providers.
|
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/configuration/configuration.py
|
|
|
|
:language: python
|
2020-06-29 20:32:48 +00:00
|
|
|
:emphasize-lines: 4,9-10
|
|
|
|
:lines: 4-14
|
2020-06-25 21:12:16 +00:00
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
It implements the principle "use first, define later".
|
2020-06-25 21:12:16 +00:00
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
Loading from an INI file
|
2020-09-02 21:33:02 +00:00
|
|
|
------------------------
|
2020-06-25 21:12:16 +00:00
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
``Configuration`` provider can load configuration from an ``ini`` file using the
|
|
|
|
:py:meth:`Configuration.from_ini` method:
|
2020-06-25 21:12:16 +00:00
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/configuration/configuration_ini.py
|
|
|
|
:language: python
|
2020-06-29 20:32:48 +00:00
|
|
|
:lines: 3-5,6-
|
|
|
|
:emphasize-lines: 6
|
2020-06-25 21:12:16 +00:00
|
|
|
|
|
|
|
where ``examples/providers/configuration/config.ini`` is:
|
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/configuration/config.ini
|
|
|
|
:language: ini
|
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
:py:meth:`Configuration.from_ini` method supports environment variables interpolation. Use
|
|
|
|
``${ENV_NAME}`` format in the configuration file to substitute value of the environment
|
2020-06-26 03:48:55 +00:00
|
|
|
variable ``ENV_NAME``.
|
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
Loading from a YAML file
|
2020-09-02 21:33:02 +00:00
|
|
|
------------------------
|
2020-06-25 21:12:16 +00:00
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
``Configuration`` provider can load configuration from a ``yaml`` file using the
|
|
|
|
:py:meth:`Configuration.from_yaml` method:
|
2020-06-25 21:12:16 +00:00
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/configuration/configuration_yaml.py
|
|
|
|
:language: python
|
2020-06-29 20:32:48 +00:00
|
|
|
:lines: 3-5,6-
|
|
|
|
:emphasize-lines: 6
|
2020-06-25 21:12:16 +00:00
|
|
|
|
|
|
|
where ``examples/providers/configuration/config.yml`` is:
|
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/configuration/config.yml
|
|
|
|
:language: ini
|
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
:py:meth:`Configuration.from_yaml` method supports environment variables interpolation. Use
|
|
|
|
``${ENV_NAME}`` format in the configuration file to substitute value of the environment
|
2020-06-26 03:48:55 +00:00
|
|
|
variable ``ENV_NAME``.
|
|
|
|
|
2020-06-25 21:12:16 +00:00
|
|
|
.. note::
|
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
Loading of a yaml configuration requires ``PyYAML`` package.
|
|
|
|
|
|
|
|
You can install the ``Dependency Injector`` with an extra dependency::
|
2020-06-25 21:12:16 +00:00
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
pip install dependency-injector[yaml]
|
2020-06-29 20:32:48 +00:00
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
or install ``PyYAML`` directly::
|
|
|
|
|
|
|
|
pip install pyyaml
|
|
|
|
|
|
|
|
*Don't forget to mirror the changes in the requirements file.*
|
|
|
|
|
|
|
|
Loading from a dictionary
|
2020-09-02 21:33:02 +00:00
|
|
|
-------------------------
|
2020-09-02 01:39:23 +00:00
|
|
|
|
|
|
|
``Configuration`` provider can load configuration from a Python ``dict`` using the
|
|
|
|
:py:meth:`Configuration.from_dict` method:
|
2020-06-29 20:32:48 +00:00
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/configuration/configuration_dict.py
|
|
|
|
:language: python
|
|
|
|
:lines: 3-5,6-
|
|
|
|
:emphasize-lines: 6-13
|
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
Loading from an environment variable
|
2020-09-02 21:33:02 +00:00
|
|
|
------------------------------------
|
2020-06-25 21:12:16 +00:00
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
``Configuration`` provider can load configuration from an environment variable using the
|
|
|
|
:py:meth:`Configuration.from_env` method:
|
2020-06-25 21:12:16 +00:00
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/configuration/configuration_env.py
|
|
|
|
:language: python
|
2020-06-29 20:32:48 +00:00
|
|
|
:lines: 5-7,13-21
|
|
|
|
:emphasize-lines: 6-8
|
2020-06-25 21:12:16 +00:00
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
Loading from the multiple sources
|
2020-09-02 21:33:02 +00:00
|
|
|
---------------------------------
|
2020-06-25 21:12:16 +00:00
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
``Configuration`` provider can load configuration from the multiple sources. Loaded
|
|
|
|
configuration is merged recursively over the existing configuration.
|
2020-06-25 21:12:16 +00:00
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/configuration/configuration_multiple.py
|
|
|
|
:language: python
|
2020-06-29 20:32:48 +00:00
|
|
|
:lines: 3-5,6-14
|
|
|
|
:emphasize-lines: 6-7
|
2020-06-25 21:12:16 +00:00
|
|
|
|
|
|
|
where ``examples/providers/configuration/config.local.yml`` is:
|
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/configuration/config.local.yml
|
|
|
|
:language: ini
|
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
Specifying the value type
|
2020-09-02 21:33:02 +00:00
|
|
|
-------------------------
|
2020-08-24 17:34:47 +00:00
|
|
|
|
|
|
|
You can specify the type of the injected configuration value explicitly.
|
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
This helps when you read the value from an ini file or an environment variable and need to
|
2020-08-24 17:34:47 +00:00
|
|
|
convert it into an ``int`` or a ``float``.
|
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/configuration/configuration_type.py
|
|
|
|
:language: python
|
|
|
|
:lines: 3-
|
|
|
|
:emphasize-lines: 17
|
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
``Configuration`` provider has next helper methods:
|
2020-08-24 17:34:47 +00:00
|
|
|
|
|
|
|
- ``.as_int()``
|
|
|
|
- ``.as_float()``
|
|
|
|
- ``.as_(callback, *args, **kwargs)``
|
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
The last method ``.as_(callback, *args, **kwargs)`` helps to implement other conversions.
|
2020-08-24 17:34:47 +00:00
|
|
|
|
|
|
|
.. literalinclude:: ../../examples/providers/configuration/configuration_type_custom.py
|
|
|
|
:language: python
|
|
|
|
:lines: 3-
|
|
|
|
:emphasize-lines: 16
|
|
|
|
|
2020-09-02 01:39:23 +00:00
|
|
|
With the ``.as_(callback, *args, **kwargs)`` you can specify a function that will be called
|
2020-08-24 17:34:47 +00:00
|
|
|
before the injection. The value from the config will be passed as a first argument. The returned
|
|
|
|
value will be injected. Parameters ``*args`` and ``**kwargs`` are handled as any other injections.
|
|
|
|
|
2020-06-25 21:12:16 +00:00
|
|
|
.. disqus::
|