-
Notifications
You must be signed in to change notification settings - Fork 1
/
py_environ.py
40 lines (29 loc) · 1.16 KB
/
py_environ.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
import logging
import os
from six.moves.configparser import SafeConfigParser
log = logging.getLogger(__name__)
TRUE_EQUIVALENT_STRINGS = ["1", "yes", "true", "on"]
FALSE_EQUIVALENT_STRINGS = ["0", "no", "false", "off"]
def to_environ_key(k):
return k.upper() \
.replace('-', '_') \
.replace('.', '_DOT_') \
.replace(' ', '_') \
.replace(':', '_') \
class EnvironmentConfigWrapper(SafeConfigParser, object):
def has_option(self, section, option):
env_prefix = to_environ_key(section)
env_option = to_environ_key(option)
env_name = "%s_%s" % (env_prefix, env_option)
if env_name in os.environ:
return True
else:
return super(EnvironmentConfigWrapper, self).has_option(section, option)
def get(self, section, option, **kwargs):
env_prefix = to_environ_key(section)
env_option = to_environ_key(option)
env_name = "%s_%s" % (env_prefix, env_option)
if env_name in os.environ:
return os.environ[env_name]
else:
return super(EnvironmentConfigWrapper, self).get(section, option, **kwargs)