forked from HBNetwork/python-decouple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecouple.py
252 lines (187 loc) · 6.3 KB
/
decouple.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# coding: utf-8
import os
import sys
import string
from shlex import shlex
import yaml
# Useful for very coarse version differentiation.
PY3 = sys.version_info[0] == 3
if PY3:
from configparser import ConfigParser
text_type = str
else:
from ConfigParser import SafeConfigParser as ConfigParser
text_type = unicode
class UndefinedValueError(Exception):
pass
class Undefined(object):
"""
Class to represent undefined type.
"""
pass
# Reference instance to represent undefined values
undefined = Undefined()
class Config(object):
"""
Handle .env file format used by Foreman.
"""
_BOOLEANS = {'1': True, 'yes': True, 'true': True, 'on': True,
'0': False, 'no': False, 'false': False, 'off': False}
def __init__(self, repository):
self.repository = repository
def _cast_boolean(self, value):
"""
Helper to convert config values to boolean as ConfigParser do.
"""
value = str(value)
if value.lower() not in self._BOOLEANS:
raise ValueError('Not a boolean: %s' % value)
return self._BOOLEANS[value.lower()]
def get(self, option, default=undefined, cast=undefined):
"""
Return the value for option or default if defined.
"""
if option in self.repository:
value = self.repository.get(option)
else:
value = default
if isinstance(value, Undefined):
raise UndefinedValueError('%s option not found and default value was not defined.' % option)
if isinstance(cast, Undefined):
cast = lambda v: v # nop
elif cast is bool:
cast = self._cast_boolean
return cast(value)
def __call__(self, *args, **kwargs):
"""
Convenient shortcut to get.
"""
return self.get(*args, **kwargs)
class RepositoryBase(object):
def __init__(self, source):
raise NotImplementedError
def __contains__(self, key):
raise NotImplementedError
def get(self, key):
raise NotImplementedError
class RepositoryIni(RepositoryBase):
"""
Retrieves option keys from .ini files.
"""
SECTION = 'settings'
def __init__(self, source):
self.parser = ConfigParser()
self.parser.readfp(open(source))
def __contains__(self, key):
return (key in os.environ or
self.parser.has_option(self.SECTION, key))
def get(self, key):
return (os.environ.get(key) or
self.parser.get(self.SECTION, key))
class RepositoryEnv(RepositoryBase):
"""
Retrieves option keys from .env files with fall back to os.environ.
"""
def __init__(self, source):
self.data = {}
for line in open(source):
line = line.strip()
if not line or line.startswith('#') or '=' not in line:
continue
k, v = line.split('=', 1)
k = k.strip()
v = v.strip().strip('\'"')
self.data[k] = v
def __contains__(self, key):
return key in os.environ or key in self.data
def get(self, key):
return os.environ.get(key) or self.data[key]
class RepositoryShell(RepositoryBase):
"""
Retrieves option keys from os.environ.
"""
def __init__(self, source=None):
pass
def __contains__(self, key):
return key in os.environ
def get(self, key):
return os.environ[key]
class RepositoryYaml(RepositoryBase):
"""
Retrieves option keys from .yaml files
"""
def __init__(self, source):
try:
self.data = yaml.load(open(source))
except yaml.YAMLError as exc:
print("Error in settings file:", exc)
def __contains__(self, key):
return key in os.environ or key in self.data
def get(self, key):
return os.environ.get(key) or self.data[key]
class AutoConfig(object):
"""
Autodetects the config file and type.
"""
SUPPORTED = {
'settings.ini': RepositoryIni,
'.env': RepositoryEnv,
}
def __init__(self):
self.config = None
def _find_file(self, path):
# look for all files in the current path
for configfile in self.SUPPORTED:
filename = os.path.join(path, configfile)
if os.path.isfile(filename):
return filename
# search the parent
parent = os.path.dirname(path)
if parent and parent != os.path.sep:
return self._find_file(parent)
# reached root without finding any files.
return ''
def _load(self, path):
# Avoid unintended permission errors
try:
filename = self._find_file(os.path.abspath(path))
except Exception:
filename = ''
Repository = self.SUPPORTED.get(os.path.basename(filename))
if not Repository:
Repository = RepositoryShell
self.config = Config(Repository(filename))
def _caller_path(self):
# MAGIC! Get the caller's module path.
frame = sys._getframe()
path = os.path.dirname(frame.f_back.f_back.f_code.co_filename)
return path
def __call__(self, *args, **kwargs):
if not self.config:
self._load(self._caller_path())
return self.config(*args, **kwargs)
# A pré-instantiated AutoConfig to improve decouple's usability
# now just import config and start using with no configuration.
config = AutoConfig()
# Helpers
class Csv(object):
"""
Produces a csv parser that return a list of transformed elements.
"""
def __init__(self, cast=text_type, delimiter=',', strip=string.whitespace):
"""
Parameters:
cast -- callable that transforms the item just before it's added to the list.
delimiter -- string of delimiters chars passed to shlex.
strip -- string of non-relevant characters to be passed to str.strip after the split.
"""
self.cast = cast
self.delimiter = delimiter
self.strip = strip
def __call__(self, value):
"""The actual transformation"""
transform = lambda s: self.cast(s.strip(self.strip))
splitter = shlex(value, posix=True)
splitter.whitespace = self.delimiter
splitter.whitespace_split = True
return [transform(s) for s in splitter]