Skip to content

Commit b16b190

Browse files
authored
Configuration(yaml_files=[...]) (ets-labs#522)
* Add provider changes and tests * Move config test fixtures * Fix issue with explicit providing of envs_required=False for configuration from_*() * Implement container API * Increase priority of overriding from context * Add docs and example * Update changelog * Update changelog
1 parent b97862c commit b16b190

19 files changed

+9331
-8203
lines changed

docs/main/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Develop
1414
``container.wire(modules=["yourapp.module1"])``.
1515
- Add container wiring configuration ``wiring_config = containers.WiringConfiguration()``.
1616
- Add support of ``with`` statement for ``container.override_providers()`` method.
17+
- 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.
1720
- Drop support of Python 3.4. There are no immediate breaking changes, but Dependency Injector
1821
will no longer be tested on Python 3.4 and any bugs will not be fixed.
1922
- Fix ``Dependency.is_defined`` attribute to always return boolean value.

docs/providers/configuration.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ where ``examples/providers/configuration/config.yml`` is:
7272
.. literalinclude:: ../../examples/providers/configuration/config.yml
7373
:language: ini
7474

75+
Alternatively, you can provide a path to the YAML file over the configuration provider argument. In that case,
76+
the container will call ``config.from_yaml()`` automatically:
77+
78+
.. code-block:: python
79+
:emphasize-lines: 3
80+
81+
class Container(containers.DeclarativeContainer):
82+
83+
config = providers.Configuration(yaml_files=["./config.yml"])
84+
85+
86+
if __name__ == "__main__":
87+
container = Container() # Config is loaded from ./config.yml
88+
7589
:py:meth:`Configuration.from_yaml` method supports environment variables interpolation.
7690

7791
.. 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(yaml_files=["./config.yml"])
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)