@@ -5,7 +5,7 @@ Configuration provider
5
5
6
6
.. meta ::
7
7
:keywords: Python,DI,Dependency injection,IoC,Inversion of Control,Configuration,Injection,
8
- Option,Ini,Json,Yaml,Pydantic,Dict,Environment Variable,Load,Read,Get
8
+ Option,Ini,Json,Yaml,Pydantic,Dict,Environment Variable,Default, Load,Read,Get
9
9
:description: Configuration provides configuration options to the other providers. This page
10
10
demonstrates how to use Configuration provider to inject the dependencies, load
11
11
a configuration from an ini or yaml file, a dictionary, an environment variable,
@@ -43,8 +43,18 @@ where ``examples/providers/configuration/config.ini`` is:
43
43
:language: ini
44
44
45
45
:py:meth: `Configuration.from_ini ` method supports environment variables interpolation. Use
46
- ``${ENV_NAME} `` format in the configuration file to substitute value of the environment
47
- variable ``ENV_NAME ``.
46
+ ``${ENV_NAME} `` format in the configuration file to substitute value from ``ENV_NAME `` environment
47
+ variable.
48
+
49
+ You can also specify a default value using ``${ENV_NAME:default} `` format. If environment
50
+ variable ``ENV_NAME `` is undefined, configuration provider will substitute value ``default ``.
51
+
52
+ .. code-block :: ini
53
+
54
+ [section]
55
+ option1 = {$ENV_VAR}
56
+ option2 = {$ENV_VAR}/path
57
+ option3 = {$ENV_VAR:default}
48
58
49
59
Loading from a YAML file
50
60
------------------------
@@ -62,12 +72,22 @@ where ``examples/providers/configuration/config.yml`` is:
62
72
.. literalinclude :: ../../examples/providers/configuration/config.yml
63
73
:language: ini
64
74
65
- :py:meth: `Configuration.from_yaml ` method uses custom version of ``yaml.SafeLoader ``.
75
+ :py:meth: `Configuration.from_yaml ` method supports environment variables interpolation. Use
76
+ ``${ENV_NAME} `` format in the configuration file to substitute value from ``ENV_NAME `` environment
77
+ variable.
78
+
79
+ You can also specify a default value using ``${ENV_NAME:default} `` format. If environment
80
+ variable ``ENV_NAME `` is undefined, configuration provider will substitute value ``default ``.
81
+
82
+ .. code-block :: ini
66
83
67
- The loader supports environment variables interpolation. Use ``${ENV_NAME} `` format
68
- in the configuration file to substitute value of the environment variable ``ENV_NAME ``.
84
+ section:
85
+ option1: {$ENV_VAR}
86
+ option2: {$ENV_VAR}/path
87
+ option3: {$ENV_VAR:default}
69
88
70
- You can also specify a YAML loader as an argument:
89
+ :py:meth: `Configuration.from_yaml ` method uses custom version of ``yaml.SafeLoader ``.
90
+ To use another loader use ``loader `` argument:
71
91
72
92
.. code-block :: python
73
93
@@ -144,6 +164,17 @@ Loading from an environment variable
144
164
:lines: 3-
145
165
:emphasize-lines: 18-20
146
166
167
+ Loading a value
168
+ ---------------
169
+
170
+ ``Configuration `` provider can load configuration value using the
171
+ :py:meth: `Configuration.from_value ` method:
172
+
173
+ .. literalinclude :: ../../examples/providers/configuration/configuration_value.py
174
+ :language: python
175
+ :lines: 3-
176
+ :emphasize-lines: 14-15
177
+
147
178
Loading from the multiple sources
148
179
---------------------------------
149
180
0 commit comments