File tree Expand file tree Collapse file tree 4 files changed +45
-0
lines changed Expand file tree Collapse file tree 4 files changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -146,6 +146,28 @@ def __getitem__(self, key):
146
146
return self .data [key ]
147
147
148
148
149
+ class RepositorySecret (RepositoryEmpty ):
150
+ """
151
+ Retrieves option keys from files,
152
+ where title of file is a key, content of file is a value
153
+ e.g. Docker swarm secrets
154
+ """
155
+
156
+ def __init__ (self , source = '/run/secrets/' ):
157
+ self .data = {}
158
+
159
+ ls = os .listdir (source )
160
+ for file in ls :
161
+ with open (os .path .join (source , file ), 'r' ) as f :
162
+ self .data [file ] = f .read ()
163
+
164
+ def __contains__ (self , key ):
165
+ return key in os .environ or key in self .data
166
+
167
+ def __getitem__ (self , key ):
168
+ return self .data [key ]
169
+
170
+
149
171
class AutoConfig (object ):
150
172
"""
151
173
Autodetects the config file and type.
Original file line number Diff line number Diff line change
1
+ world
Original file line number Diff line number Diff line change
1
+ hello
Original file line number Diff line number Diff line change
1
+ # coding: utf-8
2
+ import os
3
+
4
+ from decouple import Config , RepositorySecret
5
+
6
+
7
+ def test_secrets ():
8
+ path = os .path .join (os .path .dirname (__file__ ), 'secrets' )
9
+ config = Config (RepositorySecret (path ))
10
+
11
+ assert 'hello' == config ('db_user' )
12
+ assert 'world' == config ('db_password' )
13
+
14
+
15
+ def test_env_undefined_but_present_in_os_environ ():
16
+ path = os .path .join (os .path .dirname (__file__ ), 'secrets' )
17
+ config = Config (RepositorySecret (path ))
18
+
19
+ os .environ ['KeyOnlyEnviron' ] = ''
20
+ assert '' == config ('KeyOnlyEnviron' )
21
+ del os .environ ['KeyOnlyEnviron' ]
You can’t perform that action at this time.
0 commit comments