Skip to content

Add Configuration.from_json() method #602

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 10 commits into from
Jul 11, 2022
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
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Key features of the ``Dependency Injector``:
- **Overriding**. Can override any provider by another provider on the fly. This helps in testing
and configuring dev/stage environment to replace API clients with stubs etc. See
`Provider overriding <https://python-dependency-injector.ets-labs.org/providers/overriding.html>`_.
- **Configuration**. Reads configuration from ``yaml`` & ``ini`` files, ``pydantic`` settings,
- **Configuration**. Reads configuration from ``yaml``, ``ini``, and ``json`` files, ``pydantic`` settings,
environment variables, and dictionaries.
See `Configuration provider <https://python-dependency-injector.ets-labs.org/providers/configuration.html>`_.
- **Resources**. Helps with initialization and configuring of logging, event loop, thread
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Key features of the ``Dependency Injector``:
- **Overriding**. Can override any provider by another provider on the fly. This helps in testing
and configuring dev/stage environment to replace API clients with stubs etc. See
:ref:`provider-overriding`.
- **Configuration**. Reads configuration from ``yaml`` & ``ini`` files, ``pydantic`` settings,
- **Configuration**. Reads configuration from ``yaml``, ``ini``, and ``json`` files, ``pydantic`` settings,
environment variables, and dictionaries. See :ref:`configuration-provider`.
- **Resources**. Helps with initialization and configuring of logging, event loop, thread
or process pool, etc. Can be used for per-function execution scope in tandem with wiring.
Expand Down
2 changes: 1 addition & 1 deletion docs/introduction/key_features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Key features of the ``Dependency Injector``:
- **Overriding**. Can override any provider by another provider on the fly. This helps in testing
and configuring dev/stage environment to replace API clients with stubs etc. See
:ref:`provider-overriding`.
- **Configuration**. Reads configuration from ``yaml`` & ``ini`` files, ``pydantic`` settings,
- **Configuration**. Reads configuration from ``yaml``, ``ini``, and ``json`` files, ``pydantic`` settings,
environment variables, and dictionaries. See :ref:`configuration-provider`.
- **Resources**. Helps with initialization and configuring of logging, event loop, thread
or process pool, etc. Can be used for per-function execution scope in tandem with wiring.
Expand Down
1 change: 1 addition & 0 deletions docs/main/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ follows `Semantic versioning`_

Development
-----------
- Add ``Configuration.from_json()`` method to load configuration from a json file.
- Improve wording on the "Dependency injection and inversion of control in Python" docs page.
- Update typing in the main example and cohesion/coupling correlation definition in
"Dependency injection and inversion of control in Python".
Expand Down
44 changes: 44 additions & 0 deletions docs/providers/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,50 @@ To use another loader use ``loader`` argument:

*Don't forget to mirror the changes in the requirements file.*

Loading from a JSON file
------------------------

``Configuration`` provider can load configuration from a ``json`` file using the
:py:meth:`Configuration.from_json` method:

.. literalinclude:: ../../examples/providers/configuration/configuration_json.py
:language: python
:lines: 3-
:emphasize-lines: 12

where ``examples/providers/configuration/config.json`` is:

.. literalinclude:: ../../examples/providers/configuration/config.json
:language: json

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

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

class Container(containers.DeclarativeContainer):

config = providers.Configuration(json_files=["./config.json"])


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

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

.. code-block:: json

{
"section": {
"option1": "${ENV_VAR}",
"option2": "${ENV_VAR}/path",
"option3": "${ENV_VAR:default}"
}
}

See also: :ref:`configuration-envs-interpolation`.

Loading from a Pydantic settings
--------------------------------

Expand Down
6 changes: 6 additions & 0 deletions examples/providers/configuration/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"aws": {
"access_key_id": "KEY",
"secret_access_key": "SECRET"
}
}
27 changes: 27 additions & 0 deletions examples/providers/configuration/configuration_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""`Configuration` provider values loading example."""

from dependency_injector import containers, providers


class Container(containers.DeclarativeContainer):

config = providers.Configuration()


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

container.config.from_json("./config.json")

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"
25 changes: 25 additions & 0 deletions examples/providers/configuration/configuration_json_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(json_files=["./config.json"])


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