2020-09-04 00:37:03 +03:00
|
|
|
"""`Configuration` provider dynamic item selector."""
|
2020-08-04 01:01:20 +03:00
|
|
|
|
|
|
|
import dataclasses
|
|
|
|
|
2020-09-04 00:37:03 +03:00
|
|
|
from dependency_injector import containers, providers
|
2020-08-04 01:01:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
class Foo:
|
|
|
|
option1: object
|
|
|
|
option2: object
|
|
|
|
|
|
|
|
|
2020-09-04 00:37:03 +03:00
|
|
|
class Container(containers.DeclarativeContainer):
|
|
|
|
|
|
|
|
config = providers.Configuration(default={
|
2021-09-30 22:32:21 +03:00
|
|
|
"target": "A",
|
|
|
|
"items": {
|
|
|
|
"A": {
|
|
|
|
"option1": 60,
|
|
|
|
"option2": 80,
|
2020-09-04 00:37:03 +03:00
|
|
|
},
|
2021-09-30 22:32:21 +03:00
|
|
|
"B": {
|
|
|
|
"option1": 10,
|
|
|
|
"option2": 20,
|
2020-09-04 00:37:03 +03:00
|
|
|
},
|
2020-08-04 01:01:20 +03:00
|
|
|
},
|
2020-09-04 00:37:03 +03:00
|
|
|
})
|
2020-08-04 01:01:20 +03:00
|
|
|
|
2020-09-04 00:37:03 +03:00
|
|
|
foo_factory = providers.Factory(
|
|
|
|
Foo,
|
|
|
|
option1=config.items[config.target].option1,
|
|
|
|
option2=config.items[config.target].option2,
|
|
|
|
)
|
2020-08-04 01:01:20 +03:00
|
|
|
|
|
|
|
|
2021-09-30 22:32:21 +03:00
|
|
|
if __name__ == "__main__":
|
2020-09-04 00:37:03 +03:00
|
|
|
container = Container()
|
2020-08-04 01:01:20 +03:00
|
|
|
|
2021-09-30 22:32:21 +03:00
|
|
|
container.config.target.from_env("TARGET")
|
2020-09-04 00:37:03 +03:00
|
|
|
foo = container.foo_factory()
|
|
|
|
print(foo.option1, foo.option2)
|
2020-08-04 01:01:20 +03:00
|
|
|
|
2020-09-04 00:37:03 +03:00
|
|
|
# $ TARGET=A python configuration_itemselector.py
|
|
|
|
# 60 80
|
|
|
|
# $ TARGET=B python configuration_itemselector.py
|
|
|
|
# 10 20
|