Skip to content

Commit e48746d

Browse files
committed
Add usage of the container to the configuration examples
1 parent 2a23f3d commit e48746d

File tree

10 files changed

+207
-118
lines changed

10 files changed

+207
-118
lines changed

docs/providers/configuration.rst

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Configuration provider
1616

1717
.. literalinclude:: ../../examples/providers/configuration/configuration.py
1818
:language: python
19-
:emphasize-lines: 4,9-10
20-
:lines: 4-14
19+
:emphasize-lines: 7,12-13
20+
:lines: 3-
2121

2222
It implements the principle "use first, define later".
2323

@@ -29,8 +29,8 @@ Loading from an INI file
2929

3030
.. literalinclude:: ../../examples/providers/configuration/configuration_ini.py
3131
:language: python
32-
:lines: 3-5,6-
33-
:emphasize-lines: 6
32+
:lines: 3-
33+
:emphasize-lines: 12
3434

3535
where ``examples/providers/configuration/config.ini`` is:
3636

@@ -49,8 +49,8 @@ Loading from a YAML file
4949

5050
.. literalinclude:: ../../examples/providers/configuration/configuration_yaml.py
5151
:language: python
52-
:lines: 3-5,6-
53-
:emphasize-lines: 6
52+
:lines: 3-
53+
:emphasize-lines: 12
5454

5555
where ``examples/providers/configuration/config.yml`` is:
5656

@@ -83,8 +83,8 @@ Loading from a dictionary
8383

8484
.. literalinclude:: ../../examples/providers/configuration/configuration_dict.py
8585
:language: python
86-
:lines: 3-5,6-
87-
:emphasize-lines: 6-13
86+
:lines: 3-
87+
:emphasize-lines: 12-19
8888

8989
Loading from an environment variable
9090
------------------------------------
@@ -94,8 +94,8 @@ Loading from an environment variable
9494

9595
.. literalinclude:: ../../examples/providers/configuration/configuration_env.py
9696
:language: python
97-
:lines: 5-7,13-21
98-
:emphasize-lines: 6-8
97+
:lines: 3-
98+
:emphasize-lines: 18-20
9999

100100
Loading from the multiple sources
101101
---------------------------------
@@ -105,8 +105,8 @@ configuration is merged recursively over the existing configuration.
105105

106106
.. literalinclude:: ../../examples/providers/configuration/configuration_multiple.py
107107
:language: python
108-
:lines: 3-5,6-14
109-
:emphasize-lines: 6-7
108+
:lines: 3-
109+
:emphasize-lines: 12-13
110110

111111
where ``examples/providers/configuration/config.local.yml`` is:
112112

@@ -124,7 +124,7 @@ convert it into an ``int`` or a ``float``.
124124
.. literalinclude:: ../../examples/providers/configuration/configuration_type.py
125125
:language: python
126126
:lines: 3-
127-
:emphasize-lines: 17
127+
:emphasize-lines: 19
128128

129129
``Configuration`` provider has next helper methods:
130130

@@ -137,10 +137,27 @@ The last method ``.as_(callback, *args, **kwargs)`` helps to implement other con
137137
.. literalinclude:: ../../examples/providers/configuration/configuration_type_custom.py
138138
:language: python
139139
:lines: 3-
140-
:emphasize-lines: 16
140+
:emphasize-lines: 18
141141

142142
With the ``.as_(callback, *args, **kwargs)`` you can specify a function that will be called
143143
before the injection. The value from the config will be passed as a first argument. The returned
144144
value will be injected. Parameters ``*args`` and ``**kwargs`` are handled as any other injections.
145145

146+
Injecting invariants
147+
--------------------
148+
149+
You can inject invariant configuration options based on the value of the other configuration
150+
option.
151+
152+
To use that you should provide the switch-value as an item of the configuration option that
153+
contains sections ``config.options[config.switch]``:
154+
155+
- When the value of the ``config.switch`` is ``A``, the ``config.options.A`` is injected
156+
- When the value of the ``config.switch`` is ``B``, the ``config.options.B`` is injected
157+
158+
.. literalinclude:: ../../examples/providers/configuration/configuration_itemselector.py
159+
:language: python
160+
:lines: 3-
161+
:emphasize-lines: 15,30-31,38
162+
146163
.. disqus::
Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
"""`Configuration` provider example."""
22

33
import boto3
4-
from dependency_injector import providers
4+
from dependency_injector import containers, providers
55

66

7-
config = providers.Configuration()
7+
class Container(containers.DeclarativeContainer):
88

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-
)
9+
config = providers.Configuration()
10+
11+
s3_client_factory = providers.Factory(
12+
boto3.client,
13+
's3',
14+
aws_access_key_id=config.aws.access_key_id,
15+
aws_secret_access_key=config.aws.secret_access_key,
16+
)
1517

1618

1719
if __name__ == '__main__':
18-
config.from_dict(
20+
container = Container()
21+
container.config.from_dict(
1922
{
2023
'aws': {
2124
'access_key_id': 'KEY',
2225
'secret_access_key': 'SECRET',
2326
},
2427
},
2528
)
26-
s3_client = s3_client_factory()
29+
s3_client = container.s3_client_factory()
Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
"""`Configuration` provider values loading example."""
22

3-
from dependency_injector import providers
3+
from dependency_injector import containers, providers
44

55

6-
config = providers.Configuration()
6+
class Container(containers.DeclarativeContainer):
77

8-
config.from_dict(
9-
{
8+
config = providers.Configuration()
9+
10+
11+
if __name__ == '__main__':
12+
container = Container()
13+
14+
container.config.from_dict(
15+
{
16+
'aws': {
17+
'access_key_id': 'KEY',
18+
'secret_access_key': 'SECRET',
19+
},
20+
},
21+
)
22+
23+
assert container.config() == {
1024
'aws': {
11-
'access_key_id': 'KEY',
12-
'secret_access_key': 'SECRET',
13-
},
14-
},
15-
)
16-
17-
assert config() == {'aws': {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}}
18-
assert config.aws() == {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}
19-
assert config.aws.access_key_id() == 'KEY'
20-
assert config.aws.secret_access_key() == 'SECRET'
25+
'access_key_id': 'KEY',
26+
'secret_access_key': 'SECRET',
27+
},
28+
}
29+
assert container.config.aws() == {
30+
'access_key_id': 'KEY',
31+
'secret_access_key': 'SECRET',
32+
}
33+
assert container.config.aws.access_key_id() == 'KEY'
34+
assert container.config.aws.secret_access_key() == 'SECRET'

examples/providers/configuration/configuration_env.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@
22

33
import os
44

5-
from dependency_injector import providers
5+
from dependency_injector import containers, providers
66

77

8-
# Emulate environment variables
9-
os.environ['AWS_ACCESS_KEY_ID'] = 'KEY'
10-
os.environ['AWS_SECRET_ACCESS_KEY'] = 'SECRET'
8+
class Container(containers.DeclarativeContainer):
119

10+
config = providers.Configuration()
1211

13-
config = providers.Configuration()
1412

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')
13+
if __name__ == '__main__':
14+
container = Container()
1815

19-
assert config.aws.access_key_id() == 'KEY'
20-
assert config.aws.secret_access_key() == 'SECRET'
21-
assert config.optional() == 'default_value'
16+
# Emulate environment variables
17+
os.environ['AWS_ACCESS_KEY_ID'] = 'KEY'
18+
os.environ['AWS_SECRET_ACCESS_KEY'] = 'SECRET'
19+
20+
container.config.aws.access_key_id.from_env('AWS_ACCESS_KEY_ID')
21+
container.config.aws.secret_access_key.from_env('AWS_SECRET_ACCESS_KEY')
22+
container.config.optional.from_env('UNDEFINED', 'default_value')
23+
24+
assert container.config.aws.access_key_id() == 'KEY'
25+
assert container.config.aws.secret_access_key() == 'SECRET'
26+
assert container.config.optional() == 'default_value'
Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
"""`Configuration` provider values loading example."""
22

3-
from dependency_injector import providers
3+
from dependency_injector import containers, providers
44

55

6-
config = providers.Configuration()
6+
class Container(containers.DeclarativeContainer):
77

8-
config.from_ini('examples/providers/configuration/config.ini')
8+
config = providers.Configuration()
99

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'
10+
11+
if __name__ == '__main__':
12+
container = Container()
13+
14+
container.config.from_ini('examples/providers/configuration/config.ini')
15+
16+
assert container.config() == {
17+
'aws': {
18+
'access_key_id': 'KEY',
19+
'secret_access_key': 'SECRET',
20+
},
21+
}
22+
assert container.config.aws() == {
23+
'access_key_id': 'KEY',
24+
'secret_access_key': 'SECRET',
25+
}
26+
assert container.config.aws.access_key_id() == 'KEY'
27+
assert container.config.aws.secret_access_key() == 'SECRET'
Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
"""`Configuration` provider dynamic item selector.
2-
3-
Details: https://github.com/ets-labs/python-dependency-injector/issues/274
4-
"""
1+
"""`Configuration` provider dynamic item selector."""
52

63
import dataclasses
74

8-
from dependency_injector import providers
5+
from dependency_injector import containers, providers
96

107

118
@dataclasses.dataclass
@@ -14,34 +11,37 @@ class Foo:
1411
option2: object
1512

1613

17-
config = providers.Configuration(default={
18-
'target': 'A',
19-
'items': {
20-
'A': {
21-
'option1': 60,
22-
'option2': 80,
23-
},
24-
'B': {
25-
'option1': 10,
26-
'option2': 20,
14+
class Container(containers.DeclarativeContainer):
15+
16+
config = providers.Configuration(default={
17+
'target': 'A',
18+
'items': {
19+
'A': {
20+
'option1': 60,
21+
'option2': 80,
22+
},
23+
'B': {
24+
'option1': 10,
25+
'option2': 20,
26+
},
2727
},
28-
},
29-
})
28+
})
3029

31-
foo = providers.Factory(
32-
Foo,
33-
option1=config.items[config.target].option1,
34-
option2=config.items[config.target].option2,
35-
)
30+
foo_factory = providers.Factory(
31+
Foo,
32+
option1=config.items[config.target].option1,
33+
option2=config.items[config.target].option2,
34+
)
3635

3736

3837
if __name__ == '__main__':
39-
config.target.from_env('TARGET')
40-
f = foo()
41-
print(f.option1, f.option2)
38+
container = Container()
4239

40+
container.config.target.from_env('TARGET')
41+
foo = container.foo_factory()
42+
print(foo.option1, foo.option2)
4343

44-
# $ TARGET=A python configuration_itemselector.py
45-
# 60 80
46-
# $ TARGET=B python configuration_itemselector.py
47-
# 10 20
44+
# $ TARGET=A python configuration_itemselector.py
45+
# 60 80
46+
# $ TARGET=B python configuration_itemselector.py
47+
# 10 20
Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
"""`Configuration` provider values loading example."""
22

3-
from dependency_injector import providers
3+
from dependency_injector import containers, providers
44

55

6-
config = providers.Configuration()
6+
class Container(containers.DeclarativeContainer):
77

8-
config.from_yaml('examples/providers/configuration/config.yml')
9-
config.from_yaml('examples/providers/configuration/config.local.yml')
8+
config = providers.Configuration()
109

11-
assert config() == {'aws': {'access_key_id': 'LOCAL-KEY', 'secret_access_key': 'LOCAL-SECRET'}}
12-
assert config.aws() == {'access_key_id': 'LOCAL-KEY', 'secret_access_key': 'LOCAL-SECRET'}
13-
assert config.aws.access_key_id() == 'LOCAL-KEY'
14-
assert config.aws.secret_access_key() == 'LOCAL-SECRET'
10+
11+
if __name__ == '__main__':
12+
container = Container()
13+
14+
container.config.from_yaml('examples/providers/configuration/config.yml')
15+
container.config.from_yaml('examples/providers/configuration/config.local.yml')
16+
17+
assert container.config() == {
18+
'aws': {
19+
'access_key_id': 'LOCAL-KEY',
20+
'secret_access_key': 'LOCAL-SECRET',
21+
},
22+
}
23+
assert container.config.aws() == {
24+
'access_key_id': 'LOCAL-KEY',
25+
'secret_access_key': 'LOCAL-SECRET',
26+
}
27+
assert container.config.aws.access_key_id() == 'LOCAL-KEY'
28+
assert container.config.aws.secret_access_key() == 'LOCAL-SECRET'

0 commit comments

Comments
 (0)