Skip to content

Configuration(yaml_files=[...]) #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/main/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Develop
``container.wire(modules=["yourapp.module1"])``.
- Add container wiring configuration ``wiring_config = containers.WiringConfiguration()``.
- Add support of ``with`` statement for ``container.override_providers()`` method.
- Add ``Configuration(yaml_files=[...])`` argument.
- Fix ``envs_required=False`` behavior in ``Configuration.from_*()`` methods
to give a priority to the explicitly provided value.
- Drop support of Python 3.4. There are no immediate breaking changes, but Dependency Injector
will no longer be tested on Python 3.4 and any bugs will not be fixed.
- Fix ``Dependency.is_defined`` attribute to always return boolean value.
Expand Down
14 changes: 14 additions & 0 deletions docs/providers/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ where ``examples/providers/configuration/config.yml`` is:
.. literalinclude:: ../../examples/providers/configuration/config.yml
:language: ini

Alternatively, you can provide a path to the YAML file over the configuration provider argument. In that case,
the container will call ``config.from_yaml()`` automatically:

.. code-block:: python
:emphasize-lines: 3

class Container(containers.DeclarativeContainer):

config = providers.Configuration(yaml_files=["./config.yml"])


if __name__ == "__main__":
container = Container() # Config is loaded from ./config.yml

:py:meth:`Configuration.from_yaml` method supports environment variables interpolation.

.. code-block:: ini
Expand Down
25 changes: 25 additions & 0 deletions examples/providers/configuration/configuration_yaml_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""`Configuration` provider values loading example."""

from dependency_injector import containers, providers


class Container(containers.DeclarativeContainer):

config = providers.Configuration(yaml_files=["./config.yml"])


if __name__ == "__main__":
container = Container()

assert container.config() == {
"aws": {
"access_key_id": "KEY",
"secret_access_key": "SECRET",
},
}
assert container.config.aws() == {
"access_key_id": "KEY",
"secret_access_key": "SECRET",
}
assert container.config.aws.access_key_id() == "KEY"
assert container.config.aws.secret_access_key() == "SECRET"
Loading