Skip to content

Commit b1cd327

Browse files
committed
Add docs and example
1 parent 68a3ce1 commit b1cd327

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

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)