mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-07 07:00:49 +03:00
Add usage of the container to the configuration examples
This commit is contained in:
parent
2a23f3d2f4
commit
e48746d65f
|
@ -16,8 +16,8 @@ Configuration provider
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/providers/configuration/configuration.py
|
.. literalinclude:: ../../examples/providers/configuration/configuration.py
|
||||||
:language: python
|
:language: python
|
||||||
:emphasize-lines: 4,9-10
|
:emphasize-lines: 7,12-13
|
||||||
:lines: 4-14
|
:lines: 3-
|
||||||
|
|
||||||
It implements the principle "use first, define later".
|
It implements the principle "use first, define later".
|
||||||
|
|
||||||
|
@ -29,8 +29,8 @@ Loading from an INI file
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/providers/configuration/configuration_ini.py
|
.. literalinclude:: ../../examples/providers/configuration/configuration_ini.py
|
||||||
:language: python
|
:language: python
|
||||||
:lines: 3-5,6-
|
:lines: 3-
|
||||||
:emphasize-lines: 6
|
:emphasize-lines: 12
|
||||||
|
|
||||||
where ``examples/providers/configuration/config.ini`` is:
|
where ``examples/providers/configuration/config.ini`` is:
|
||||||
|
|
||||||
|
@ -49,8 +49,8 @@ Loading from a YAML file
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/providers/configuration/configuration_yaml.py
|
.. literalinclude:: ../../examples/providers/configuration/configuration_yaml.py
|
||||||
:language: python
|
:language: python
|
||||||
:lines: 3-5,6-
|
:lines: 3-
|
||||||
:emphasize-lines: 6
|
:emphasize-lines: 12
|
||||||
|
|
||||||
where ``examples/providers/configuration/config.yml`` is:
|
where ``examples/providers/configuration/config.yml`` is:
|
||||||
|
|
||||||
|
@ -83,8 +83,8 @@ Loading from a dictionary
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/providers/configuration/configuration_dict.py
|
.. literalinclude:: ../../examples/providers/configuration/configuration_dict.py
|
||||||
:language: python
|
:language: python
|
||||||
:lines: 3-5,6-
|
:lines: 3-
|
||||||
:emphasize-lines: 6-13
|
:emphasize-lines: 12-19
|
||||||
|
|
||||||
Loading from an environment variable
|
Loading from an environment variable
|
||||||
------------------------------------
|
------------------------------------
|
||||||
|
@ -94,8 +94,8 @@ Loading from an environment variable
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/providers/configuration/configuration_env.py
|
.. literalinclude:: ../../examples/providers/configuration/configuration_env.py
|
||||||
:language: python
|
:language: python
|
||||||
:lines: 5-7,13-21
|
:lines: 3-
|
||||||
:emphasize-lines: 6-8
|
:emphasize-lines: 18-20
|
||||||
|
|
||||||
Loading from the multiple sources
|
Loading from the multiple sources
|
||||||
---------------------------------
|
---------------------------------
|
||||||
|
@ -105,8 +105,8 @@ configuration is merged recursively over the existing configuration.
|
||||||
|
|
||||||
.. literalinclude:: ../../examples/providers/configuration/configuration_multiple.py
|
.. literalinclude:: ../../examples/providers/configuration/configuration_multiple.py
|
||||||
:language: python
|
:language: python
|
||||||
:lines: 3-5,6-14
|
:lines: 3-
|
||||||
:emphasize-lines: 6-7
|
:emphasize-lines: 12-13
|
||||||
|
|
||||||
where ``examples/providers/configuration/config.local.yml`` is:
|
where ``examples/providers/configuration/config.local.yml`` is:
|
||||||
|
|
||||||
|
@ -124,7 +124,7 @@ convert it into an ``int`` or a ``float``.
|
||||||
.. literalinclude:: ../../examples/providers/configuration/configuration_type.py
|
.. literalinclude:: ../../examples/providers/configuration/configuration_type.py
|
||||||
:language: python
|
:language: python
|
||||||
:lines: 3-
|
:lines: 3-
|
||||||
:emphasize-lines: 17
|
:emphasize-lines: 19
|
||||||
|
|
||||||
``Configuration`` provider has next helper methods:
|
``Configuration`` provider has next helper methods:
|
||||||
|
|
||||||
|
@ -137,10 +137,27 @@ The last method ``.as_(callback, *args, **kwargs)`` helps to implement other con
|
||||||
.. literalinclude:: ../../examples/providers/configuration/configuration_type_custom.py
|
.. literalinclude:: ../../examples/providers/configuration/configuration_type_custom.py
|
||||||
:language: python
|
:language: python
|
||||||
:lines: 3-
|
:lines: 3-
|
||||||
:emphasize-lines: 16
|
:emphasize-lines: 18
|
||||||
|
|
||||||
With the ``.as_(callback, *args, **kwargs)`` you can specify a function that will be called
|
With the ``.as_(callback, *args, **kwargs)`` you can specify a function that will be called
|
||||||
before the injection. The value from the config will be passed as a first argument. The returned
|
before the injection. The value from the config will be passed as a first argument. The returned
|
||||||
value will be injected. Parameters ``*args`` and ``**kwargs`` are handled as any other injections.
|
value will be injected. Parameters ``*args`` and ``**kwargs`` are handled as any other injections.
|
||||||
|
|
||||||
|
Injecting invariants
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
You can inject invariant configuration options based on the value of the other configuration
|
||||||
|
option.
|
||||||
|
|
||||||
|
To use that you should provide the switch-value as an item of the configuration option that
|
||||||
|
contains sections ``config.options[config.switch]``:
|
||||||
|
|
||||||
|
- When the value of the ``config.switch`` is ``A``, the ``config.options.A`` is injected
|
||||||
|
- When the value of the ``config.switch`` is ``B``, the ``config.options.B`` is injected
|
||||||
|
|
||||||
|
.. literalinclude:: ../../examples/providers/configuration/configuration_itemselector.py
|
||||||
|
:language: python
|
||||||
|
:lines: 3-
|
||||||
|
:emphasize-lines: 15,30-31,38
|
||||||
|
|
||||||
.. disqus::
|
.. disqus::
|
||||||
|
|
|
@ -1,21 +1,24 @@
|
||||||
"""`Configuration` provider example."""
|
"""`Configuration` provider example."""
|
||||||
|
|
||||||
import boto3
|
import boto3
|
||||||
from dependency_injector import providers
|
from dependency_injector import containers, providers
|
||||||
|
|
||||||
|
|
||||||
config = providers.Configuration()
|
class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
s3_client_factory = providers.Factory(
|
config = providers.Configuration()
|
||||||
|
|
||||||
|
s3_client_factory = providers.Factory(
|
||||||
boto3.client,
|
boto3.client,
|
||||||
's3',
|
's3',
|
||||||
aws_access_key_id=config.aws.access_key_id,
|
aws_access_key_id=config.aws.access_key_id,
|
||||||
aws_secret_access_key=config.aws.secret_access_key,
|
aws_secret_access_key=config.aws.secret_access_key,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
config.from_dict(
|
container = Container()
|
||||||
|
container.config.from_dict(
|
||||||
{
|
{
|
||||||
'aws': {
|
'aws': {
|
||||||
'access_key_id': 'KEY',
|
'access_key_id': 'KEY',
|
||||||
|
@ -23,4 +26,4 @@ if __name__ == '__main__':
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
s3_client = s3_client_factory()
|
s3_client = container.s3_client_factory()
|
||||||
|
|
|
@ -1,20 +1,34 @@
|
||||||
"""`Configuration` provider values loading example."""
|
"""`Configuration` provider values loading example."""
|
||||||
|
|
||||||
from dependency_injector import providers
|
from dependency_injector import containers, providers
|
||||||
|
|
||||||
|
|
||||||
config = providers.Configuration()
|
class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
config.from_dict(
|
config = providers.Configuration()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
container = Container()
|
||||||
|
|
||||||
|
container.config.from_dict(
|
||||||
{
|
{
|
||||||
'aws': {
|
'aws': {
|
||||||
'access_key_id': 'KEY',
|
'access_key_id': 'KEY',
|
||||||
'secret_access_key': 'SECRET',
|
'secret_access_key': 'SECRET',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
assert config() == {'aws': {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}}
|
assert container.config() == {
|
||||||
assert config.aws() == {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}
|
'aws': {
|
||||||
assert config.aws.access_key_id() == 'KEY'
|
'access_key_id': 'KEY',
|
||||||
assert config.aws.secret_access_key() == 'SECRET'
|
'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'
|
||||||
|
|
|
@ -2,20 +2,25 @@
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from dependency_injector import providers
|
from dependency_injector import containers, providers
|
||||||
|
|
||||||
|
|
||||||
# Emulate environment variables
|
class Container(containers.DeclarativeContainer):
|
||||||
os.environ['AWS_ACCESS_KEY_ID'] = 'KEY'
|
|
||||||
os.environ['AWS_SECRET_ACCESS_KEY'] = 'SECRET'
|
config = providers.Configuration()
|
||||||
|
|
||||||
|
|
||||||
config = providers.Configuration()
|
if __name__ == '__main__':
|
||||||
|
container = Container()
|
||||||
|
|
||||||
config.aws.access_key_id.from_env('AWS_ACCESS_KEY_ID')
|
# Emulate environment variables
|
||||||
config.aws.secret_access_key.from_env('AWS_SECRET_ACCESS_KEY')
|
os.environ['AWS_ACCESS_KEY_ID'] = 'KEY'
|
||||||
config.optional.from_env('UNDEFINED', 'default_value')
|
os.environ['AWS_SECRET_ACCESS_KEY'] = 'SECRET'
|
||||||
|
|
||||||
assert config.aws.access_key_id() == 'KEY'
|
container.config.aws.access_key_id.from_env('AWS_ACCESS_KEY_ID')
|
||||||
assert config.aws.secret_access_key() == 'SECRET'
|
container.config.aws.secret_access_key.from_env('AWS_SECRET_ACCESS_KEY')
|
||||||
assert config.optional() == 'default_value'
|
container.config.optional.from_env('UNDEFINED', 'default_value')
|
||||||
|
|
||||||
|
assert container.config.aws.access_key_id() == 'KEY'
|
||||||
|
assert container.config.aws.secret_access_key() == 'SECRET'
|
||||||
|
assert container.config.optional() == 'default_value'
|
||||||
|
|
|
@ -1,13 +1,27 @@
|
||||||
"""`Configuration` provider values loading example."""
|
"""`Configuration` provider values loading example."""
|
||||||
|
|
||||||
from dependency_injector import providers
|
from dependency_injector import containers, providers
|
||||||
|
|
||||||
|
|
||||||
config = providers.Configuration()
|
class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
config.from_ini('examples/providers/configuration/config.ini')
|
config = providers.Configuration()
|
||||||
|
|
||||||
assert config() == {'aws': {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}}
|
|
||||||
assert config.aws() == {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}
|
if __name__ == '__main__':
|
||||||
assert config.aws.access_key_id() == 'KEY'
|
container = Container()
|
||||||
assert config.aws.secret_access_key() == 'SECRET'
|
|
||||||
|
container.config.from_ini('examples/providers/configuration/config.ini')
|
||||||
|
|
||||||
|
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'
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
"""`Configuration` provider dynamic item selector.
|
"""`Configuration` provider dynamic item selector."""
|
||||||
|
|
||||||
Details: https://github.com/ets-labs/python-dependency-injector/issues/274
|
|
||||||
"""
|
|
||||||
|
|
||||||
import dataclasses
|
import dataclasses
|
||||||
|
|
||||||
from dependency_injector import providers
|
from dependency_injector import containers, providers
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
|
@ -14,7 +11,9 @@ class Foo:
|
||||||
option2: object
|
option2: object
|
||||||
|
|
||||||
|
|
||||||
config = providers.Configuration(default={
|
class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
|
config = providers.Configuration(default={
|
||||||
'target': 'A',
|
'target': 'A',
|
||||||
'items': {
|
'items': {
|
||||||
'A': {
|
'A': {
|
||||||
|
@ -26,22 +25,23 @@ config = providers.Configuration(default={
|
||||||
'option2': 20,
|
'option2': 20,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
foo = providers.Factory(
|
foo_factory = providers.Factory(
|
||||||
Foo,
|
Foo,
|
||||||
option1=config.items[config.target].option1,
|
option1=config.items[config.target].option1,
|
||||||
option2=config.items[config.target].option2,
|
option2=config.items[config.target].option2,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
config.target.from_env('TARGET')
|
container = Container()
|
||||||
f = foo()
|
|
||||||
print(f.option1, f.option2)
|
|
||||||
|
|
||||||
|
container.config.target.from_env('TARGET')
|
||||||
|
foo = container.foo_factory()
|
||||||
|
print(foo.option1, foo.option2)
|
||||||
|
|
||||||
# $ TARGET=A python configuration_itemselector.py
|
# $ TARGET=A python configuration_itemselector.py
|
||||||
# 60 80
|
# 60 80
|
||||||
# $ TARGET=B python configuration_itemselector.py
|
# $ TARGET=B python configuration_itemselector.py
|
||||||
# 10 20
|
# 10 20
|
||||||
|
|
|
@ -1,14 +1,28 @@
|
||||||
"""`Configuration` provider values loading example."""
|
"""`Configuration` provider values loading example."""
|
||||||
|
|
||||||
from dependency_injector import providers
|
from dependency_injector import containers, providers
|
||||||
|
|
||||||
|
|
||||||
config = providers.Configuration()
|
class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
config.from_yaml('examples/providers/configuration/config.yml')
|
config = providers.Configuration()
|
||||||
config.from_yaml('examples/providers/configuration/config.local.yml')
|
|
||||||
|
|
||||||
assert config() == {'aws': {'access_key_id': 'LOCAL-KEY', 'secret_access_key': 'LOCAL-SECRET'}}
|
|
||||||
assert config.aws() == {'access_key_id': 'LOCAL-KEY', 'secret_access_key': 'LOCAL-SECRET'}
|
if __name__ == '__main__':
|
||||||
assert config.aws.access_key_id() == 'LOCAL-KEY'
|
container = Container()
|
||||||
assert config.aws.secret_access_key() == 'LOCAL-SECRET'
|
|
||||||
|
container.config.from_yaml('examples/providers/configuration/config.yml')
|
||||||
|
container.config.from_yaml('examples/providers/configuration/config.local.yml')
|
||||||
|
|
||||||
|
assert container.config() == {
|
||||||
|
'aws': {
|
||||||
|
'access_key_id': 'LOCAL-KEY',
|
||||||
|
'secret_access_key': 'LOCAL-SECRET',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
assert container.config.aws() == {
|
||||||
|
'access_key_id': 'LOCAL-KEY',
|
||||||
|
'secret_access_key': 'LOCAL-SECRET',
|
||||||
|
}
|
||||||
|
assert container.config.aws.access_key_id() == 'LOCAL-KEY'
|
||||||
|
assert container.config.aws.secret_access_key() == 'LOCAL-SECRET'
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from dependency_injector import providers
|
from dependency_injector import containers, providers
|
||||||
|
|
||||||
|
|
||||||
class ApiClient:
|
class ApiClient:
|
||||||
|
@ -11,24 +11,28 @@ class ApiClient:
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
|
|
||||||
|
|
||||||
config = providers.Configuration()
|
class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
api_client_factory = providers.Factory(
|
config = providers.Configuration()
|
||||||
|
|
||||||
|
api_client_factory = providers.Factory(
|
||||||
ApiClient,
|
ApiClient,
|
||||||
api_key=config.api.key,
|
api_key=config.api.key,
|
||||||
timeout=config.api.timeout.as_int(),
|
timeout=config.api.timeout.as_int(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
container = Container()
|
||||||
|
|
||||||
# Emulate environment variables
|
# Emulate environment variables
|
||||||
os.environ['API_KEY'] = 'secret'
|
os.environ['API_KEY'] = 'secret'
|
||||||
os.environ['API_TIMEOUT'] = '5'
|
os.environ['API_TIMEOUT'] = '5'
|
||||||
|
|
||||||
config.api.key.from_env('API_KEY')
|
container.config.api.key.from_env('API_KEY')
|
||||||
config.api.timeout.from_env('API_TIMEOUT')
|
container.config.api.timeout.from_env('API_TIMEOUT')
|
||||||
|
|
||||||
api_client = api_client_factory()
|
api_client = container.api_client_factory()
|
||||||
|
|
||||||
assert api_client.api_key == 'secret'
|
assert api_client.api_key == 'secret'
|
||||||
assert api_client.timeout == 5
|
assert api_client.timeout == 5
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
import os
|
import os
|
||||||
import decimal
|
import decimal
|
||||||
|
|
||||||
from dependency_injector import providers
|
from dependency_injector import containers, providers
|
||||||
|
|
||||||
|
|
||||||
class Calculator:
|
class Calculator:
|
||||||
|
@ -11,20 +11,24 @@ class Calculator:
|
||||||
self.pi = pi
|
self.pi = pi
|
||||||
|
|
||||||
|
|
||||||
config = providers.Configuration()
|
class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
calculator_factory = providers.Factory(
|
config = providers.Configuration()
|
||||||
|
|
||||||
|
calculator_factory = providers.Factory(
|
||||||
Calculator,
|
Calculator,
|
||||||
pi=config.pi.as_(decimal.Decimal),
|
pi=config.pi.as_(decimal.Decimal),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
container = Container()
|
||||||
|
|
||||||
# Emulate environment variables
|
# Emulate environment variables
|
||||||
os.environ['PI'] = '3.1415926535897932384626433832'
|
os.environ['PI'] = '3.1415926535897932384626433832'
|
||||||
|
|
||||||
config.pi.from_env('PI')
|
container.config.pi.from_env('PI')
|
||||||
|
|
||||||
calculator = calculator_factory()
|
calculator = container.calculator_factory()
|
||||||
|
|
||||||
assert calculator.pi == decimal.Decimal('3.1415926535897932384626433832')
|
assert calculator.pi == decimal.Decimal('3.1415926535897932384626433832')
|
||||||
|
|
|
@ -1,13 +1,27 @@
|
||||||
"""`Configuration` provider values loading example."""
|
"""`Configuration` provider values loading example."""
|
||||||
|
|
||||||
from dependency_injector import providers
|
from dependency_injector import containers, providers
|
||||||
|
|
||||||
|
|
||||||
config = providers.Configuration()
|
class Container(containers.DeclarativeContainer):
|
||||||
|
|
||||||
config.from_yaml('examples/providers/configuration/config.yml')
|
config = providers.Configuration()
|
||||||
|
|
||||||
assert config() == {'aws': {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}}
|
|
||||||
assert config.aws() == {'access_key_id': 'KEY', 'secret_access_key': 'SECRET'}
|
if __name__ == '__main__':
|
||||||
assert config.aws.access_key_id() == 'KEY'
|
container = Container()
|
||||||
assert config.aws.secret_access_key() == 'SECRET'
|
|
||||||
|
container.config.from_yaml('examples/providers/configuration/config.yml')
|
||||||
|
|
||||||
|
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…
Reference in New Issue
Block a user