10000 Merge branch 'release/3.18.0' into master · kiq7/python-dependency-injector@5718140 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5718140

Browse files
committed
Merge branch 'release/3.18.0' into master
2 parents 3a0c746 + 525ddbc commit 5718140

20 files changed

+5733
-3180
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ test-py3: build
6161

6262
check:
6363
# Static analysis
64-
flake8 --max-complexity=10 src/dependency_injector/
65-
flake8 --max-complexity=10 examples/
64+
flake8 src/dependency_injector/
65+
flake8 examples/
6666
# Code style analysis
6767
pydocstyle src/dependency_injector/
6868
pydocstyle examples/

docs/main/changelog.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ that were made in every particular version.
77
From version 0.7.6 *Dependency Injector* framework strictly
88
follows `Semantic versioning`_
99

10+
3.18.0
11+
------
12+
- Add ``Configuration.from_yaml()`` method to load configuration from the yaml file.
13+
- Add ``Configuration.from_ini()`` method to load configuration from the ini file.
14+
- Add ``Configuration.from_dict()`` method to load configuration from the dictionary.
15+
- Add ``Configuration.from_env()`` method to load configuration from the environment variable.
16+
- Add default value for ``name`` argument of ``Configuration`` provider.
17+
- Add documentation for ``Configuration`` provider.
18+
- Remove undocumented positional parameter of ``DependenciesContainer`` provider.
19+
1020
3.17.1
1121
------
1222
- Fix ``DynamicContainer`` deep-copying bug.

docs/providers/configuration.rst

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
Configuration providers
2+
-----------------------
3+
4+
.. currentmodule:: dependency_injector.providers
5+
6+
:py:class:`Configuration` provider provides configuration options to the other providers.
7+
8+
.. literalinclude:: ../../examples/providers/configuration/configuration.py
9+
:language: python
10+
:emphasize-lines: 7,12-13,18-25
11+
:linenos:
12+
13+
It implements "use first, define later" principle.
14+
15+
Loading from ``ini`` file
16+
~~~~~~~~~~~~~~~~~~~~~~~~~
17+
18+
:py:class:`Configuration` provider can load configuration from ``ini`` file using
19+
:py:meth:`Configuration.from_ini`:
20+
21+
.. literalinclude:: ../../examples/providers/configuration/configuration_ini.py
22+
:language: python
23+
:lines: 6-
24+
:emphasize-lines: 3
25+
:linenos:
26+
27+
where ``examples/providers/configuration/config.ini`` is:
28+
29+
.. literalinclude:: ../../examples/providers/configuration/config.ini
30+
:language: ini
31+
:linenos:
32+
33+
Loading from ``yaml`` file
34+
~~~~~~~~~~~~~~~~~~~~~~~~~~
35+
36+
:py:class:`Configuration` provider can load configuration from ``yaml`` file using
37+
:py:meth:`Configuration.from_yaml`:
38+
39+
.. literalinclude:: ../../examples/providers/configuration/configuration_yaml.py
40+
:language: python
41+
:lines: 6-
42+
:emphasize-lines: 3
43+
:linenos:
44+
45+
where ``examples/providers/configuration/config.yml`` is:
46+
47+
.. literalinclude:: ../../examples/providers/configuration/config.yml
48+
:language: ini
49+
:linenos:
50+
51+
.. note::
52+
53+
Loading configuration from yaml requires ``PyYAML`` package. You can install
54+
`Dependency Injector` with extras ``pip install dependency-injector[yaml]`` or install
55+
``PyYAML`` separately ``pip install pyyaml``.
56+
57+
Loading from environment variable
58+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59+
60+
:py:class:`Configuration` provider can load configuration from environment variable using
61+
:py:meth:`Configuration.from_env`:
62+
63+
.. literalinclude:: ../../examples/providers/configuration/configuration_env.py
64+
:language: python
65+
:lines: 13-21
66+
:emphasize-lines: 3-5
67+
:linenos:
68+
69+
70+
Loading from multiple sources
71+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72+
73+
:py:class:`Configuration` provider can load configuration from multiple sources. Loaded
74+
configuration is merged recursively over existing configuration.
75+
76+
.. literalinclude:: ../../examples/providers/configuration/configuration_multiple.py
77+
:language: python
78+
:lines: 6-14
79+
:emphasize-lines: 3-4
80+
:linenos:
81+
82+
where ``examples/providers/configuration/config.local.yml`` is:
83+
84+
.. literalinclude:: ../../examples/providers/configuration/config.local.yml
85+
:language: ini
86+
:linenos:
87+
88+
.. disqus::

docs/providers/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Providers package API docs - :py:mod:`dependency_injector.providers`
2323
coroutine
2424
object
2525
list
26+
configuration
2627
dependency
2728
overriding
2829
custom
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[aws]
2+
access_key_id = KEY
3+
secret_access_key = SECRET
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
aws:
2+
access_key_id: "LOCAL-KEY"
3+
secret_access_key: "LOCAL-SECRET"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
aws:
2+
access_key_id: "KEY"
3+
secret_access_key: "SECRET"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""`Configuration` provider example."""
2+
3+
import boto3
4+
from dependency_injector import providers
5+
6+
7+
config = providers.Configuration()
8+
9+
s3_client_factory = providers.Factory(
10+
boto3.client,
11+
's3',
12+
aws_access_key_id=config.aws.access_key_id,
13+
aws_secret_access_key=config.aws.secret_access_key,
14+
)
15+
16+
17+
if __name__ == '__main__':
18+
config.from_dict(
19+
{
20+
'aws': {
21+
'access_key_id': 'KEY',
22+
'secret_access_key': 'SECRET',
23+
},
24+
},
25+
)
26+
s3_client = s3_client_factory()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""`Configuration` provider values loading example."""
2+
3+
import os
4+
5+
from dependency_injector import providers
6+
7+
8+
# Emulate environment variables
9+
os.environ['AWS_ACCESS_KEY_ID'] = 'KEY'
10+
os.environ['AWS_SECRET_ACCESS_KEY'] = 'SECRET'
11+
12+
13+
config = providers.Configuration()
14+
15+
config.aws.access_key_id.from_env('AWS_ACCESS_KEY_ID')
16+
config.aws.secret_access_key.from_env('AWS_SECRET_ACCESS_KEY')
17+
config.optional.from_env('UNDEFINED', 'default_value')
18+
19+
assert config.aws.access_key_id() == 'KEY'
20+
assert config.aws.secret_access_key() == 'SECRET'
21+
assert config.optional() == 'default_value'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""`Configuration` provider values loading example."""
2+
3+
from dependency_injector import providers
4+
5+
6+
config = providers.Configuration()
7+
8+
config.from_ini('examples/providers/configuration/config.ini')
9+
10+
assert config() == {'aws': {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}}
11+
assert config.aws() == {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}
12+
assert config.aws.access_key_id() == 'KEY'
13+
assert config.aws.secret_access_key() == 'SECRET'

0 commit comments

Comments
 (0)
0