Skip to content

Commit 9abf34c

Browse files
committed
Merge branch 'release/4.33.0' into master
2 parents 585c717 + 31beb54 commit 9abf34c

File tree

8 files changed

+9514
-8630
lines changed

8 files changed

+9514
-8630
lines changed

docs/main/changelog.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ that were made in every particular version.
77
From version 0.7.6 *Dependency Injector* framework strictly
88
follows `Semantic versioning`_
99

10+
4.33.0
11+
------
12+
- Add support of default value for environment variable in INI and YAML
13+
configuration files with ``${ENV_NAME:default}`` format.
14+
See issue `#459 <https://github.com/ets-labs/python-dependency-injector/issues/459>`_.
15+
Thanks to `Maksym Shemet @hbmshemet <https://github.com/hbmshemet>`_ for suggesting the feature.
16+
- Add method ``Configuration.from_value()``.
17+
See issue `#462 <https://github.com/ets-labs/python-dependency-injector/issues/462>`_.
18+
Thanks to Mr. `Slack Clone <https://disqus.com/by/slackclone/>`_ for bringing it up
19+
in the comments for configuration provider docs.
20+
1021
4.32.3
1122
------
1223
- This fix a typo in ``di_in_python.rst`` doc.

docs/providers/configuration.rst

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Configuration provider
55

66
.. meta::
77
: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
99
:description: Configuration provides configuration options to the other providers. This page
1010
demonstrates how to use Configuration provider to inject the dependencies, load
1111
a configuration from an ini or yaml file, a dictionary, an environment variable,
@@ -43,8 +43,18 @@ where ``examples/providers/configuration/config.ini`` is:
4343
:language: ini
4444

4545
: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}
4858
4959
Loading from a YAML file
5060
------------------------
@@ -62,12 +72,22 @@ where ``examples/providers/configuration/config.yml`` is:
6272
.. literalinclude:: ../../examples/providers/configuration/config.yml
6373
:language: ini
6474

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
6683
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}
6988
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:
7191

7292
.. code-block:: python
7393
@@ -144,6 +164,17 @@ Loading from an environment variable
144164
:lines: 3-
145165
:emphasize-lines: 18-20
146166

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+
147178
Loading from the multiple sources
148179
---------------------------------
149180

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""`Configuration` provider values loading example."""
2+
3+
from datetime import date
4+
5+
from dependency_injector import containers, providers
6+
7+
8+
class Container(containers.DeclarativeContainer):
9+
10+
config = providers.Configuration()
11+
12+
13+
if __name__ == '__main__':
14+
container = Container()
15+
16+
container.config.option1.from_value(date(2021, 6, 13))
17+
container.config.option2.from_value(date(2021, 6, 14))
18+
19+
assert container.config() == {
20+
'option1': date(2021, 6, 13),
21+
'option2': date(2021, 6, 14),
22+
}
23+
assert container.config.option1() == date(2021, 6, 13)
24+
assert container.config.option2() == date(2021, 6, 14)

src/dependency_injector/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Top-level package."""
22

3-
__version__ = '4.32.3'
3+
__version__ = '4.33.0'
44
"""Version number.
55
66
:type: str

0 commit comments

Comments
 (0)