File tree Expand file tree Collapse file tree 2 files changed +46
-5
lines changed
examples/providers/configuration
tests/unit/containers/instance Expand file tree Collapse file tree 2 files changed +46
-5
lines changed Original file line number Diff line number Diff line change
1
+ """`Configuration` provider values loading example."""
2
+
3
+ from dependency_injector import containers , providers
4
+
5
+
6
+ class Container (containers .DeclarativeContainer ):
7
+
8
+ config = providers .Configuration (ini_files = ["./config.ini" ])
9
+
10
+
11
+ if __name__ == "__main__" :
12
+ container = Container ()
13
+
14
+ assert container .config () == {
15
+ "aws" : {
16
+ "access_key_id" : "KEY" ,
17
+ "secret_access_key" : "SECRET" ,
18
+ },
19
+ }
20
+ assert container .config .aws () == {
21
+ "access_key_id" : "KEY" ,
22
+ "secret_access_key" : "SECRET" ,
23
+ }
24
+ assert container .config .aws .access_key_id () == "KEY"
25
+ assert container .config .aws .secret_access_key () == "SECRET"
Original file line number Diff line number Diff line change 6
6
7
7
@fixture
8
8
def yaml_config_file (tmp_path ):
9
- yaml_config_file = str (tmp_path / "config.yml" )
10
- with open (yaml_config_file , "w" ) as file :
9
+ config_file = str (tmp_path / "config.yml" )
10
+ with open (config_file , "w" ) as file :
11
11
file .write (
12
12
"section1:\n "
13
13
" value1: yaml-loaded\n "
14
14
)
15
- return yaml_config_file
15
+ return config_file
16
16
17
17
18
- def test_auto_load (yaml_config_file ):
18
+ @fixture
19
+ def ini_config_file (tmp_path ):
20
+ config_file = str (tmp_path / "config.ini" )
21
+ with open (config_file , "w" ) as file :
22
+ file .write (
23
+ "[section2]:\n "
24
+ "value2 = ini-loaded\n "
25
+ )
26
+ return config_file
27
+
28
+
29
+
30
+ def test_auto_load (yaml_config_file , ini_config_file ):
19
31
class ContainerWithConfig (containers .DeclarativeContainer ):
20
- config = providers .Configuration (yaml_files = [yaml_config_file ])
32
+ config = providers .Configuration (
33
+ yaml_files = [yaml_config_file ],
34
+ ini_files = [ini_config_file ],
35
+ )
21
36
22
37
container = ContainerWithConfig ()
23
38
assert container .config .section1 .value1 () == "yaml-loaded"
39
+ assert container .config .section2 .value2 () == "ini-loaded"
24
40
25
41
26
42
def test_auto_load_and_overriding (yaml_config_file ):
You can’t perform that action at this time.
0 commit comments