Skip to content

Commit 34902db

Browse files
authored
Configuration(ini_files=[...]) (ets-labs#524)
* Update changelog * Add implementation * Add tests * Add more tests and example * Update changelog * Update documentation
1 parent b16b190 commit 34902db

File tree

10 files changed

+6734
-6137
lines changed

10 files changed

+6734
-6137
lines changed

docs/main/changelog.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ Develop
1515
- Add container wiring configuration ``wiring_config = containers.WiringConfiguration()``.
1616
- Add support of ``with`` statement for ``container.override_providers()`` method.
1717
- Add ``Configuration(yaml_files=[...])`` argument.
18-
- Fix ``envs_required=False`` behavior in ``Configuration.from_*()`` methods
19-
to give a priority to the explicitly provided value.
18+
- Add ``Configuration(ini_files=[...])`` argument.
2019
- Drop support of Python 3.4. There are no immediate breaking changes, but Dependency Injector
2120
will no longer be tested on Python 3.4 and any bugs will not be fixed.
2221
- Fix ``Dependency.is_defined`` attribute to always return boolean value.
22+
- Fix ``envs_required=False`` behavior in ``Configuration.from_*()`` methods
23+
to give a priority to the explicitly provided value.
2324
- Update documentation and fix typos.
2425
- Migrate tests to ``pytest``.
2526

docs/providers/configuration.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ where ``examples/providers/configuration/config.ini`` is:
4545
.. literalinclude:: ../../examples/providers/configuration/config.ini
4646
:language: ini
4747

48+
Alternatively, you can provide a path to the INI file over the configuration provider argument. In that case,
49+
the container will call ``config.from_ini()`` automatically:
50+
51+
.. code-block:: python
52+
:emphasize-lines: 3
53+
54+
class Container(containers.DeclarativeContainer):
55+
56+
config = providers.Configuration(ini_files=["./config.ini"])
57+
58+
59+
if __name__ == "__main__":
60+
container = Container() # Config is loaded from ./config.ini
61+
62+
4863
:py:meth:`Configuration.from_ini` method supports environment variables interpolation.
4964

5065
.. code-block:: ini
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""`Configuration` provider values loading example."""
2+
3+
from dependency_injector import containers, providers
4+
5+
6+
class Container(containers.DeclarativeContainer):
7+
8+
config = providers.Configuration(ini_files=["./config.ini"])
9+
10+
11+
if __name__ == "__main__":
12+
container = Container()
13+
14+
assert container.config() == {
15+
"aws": {
16+
"access_key_id": "KEY",
17+
"secret_access_key": "SECRET",
18+
},
19+
}
20+
assert container.config.aws() == {
21+
"access_key_id": "KEY",
22+
"secret_access_key": "SECRET",
23+
}
24+
assert container.config.aws.access_key_id() == "KEY"
25+
assert container.config.aws.secret_access_key() == "SECRET"

0 commit comments

Comments
 (0)