forked from randsleadershipslack/destalinator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
52 lines (37 loc) · 1.44 KB
/
config.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
#! /usr/bin/env python
import os
import yaml
from utils.with_logger import WithLogger
class Config(WithLogger):
config_fname = "configuration.yaml"
def __init__(self, config_fname=None):
config_fname = config_fname or self.config_fname
fo = open(config_fname, "r")
blob = fo.read()
fo.close()
self.config = yaml.load(blob, Loader=yaml.FullLoader)
def __getattr__(self, attrname):
upper_attrname = attrname.upper()
envvar = os.getenv(upper_attrname)
if envvar is not None:
self.logger.warning("The `%s` environment variable is deprecated in favor of the `DESTALINATOR_%s` environment variable", upper_attrname, upper_attrname)
else:
envvar = os.getenv('DESTALINATOR_' + upper_attrname)
if envvar is not None:
# allow certain types (e.g. warning, closure text) to be used as-is
if "RAW" in upper_attrname:
return envvar
else:
split_envvar = [x.strip() for x in envvar.split(',') if x] if ',' in envvar else envvar
return split_envvar
file_based_config = self.config.get(attrname, '')
return file_based_config
def get(self, attrname, fallback=None):
return self.config.get(attrname, fallback)
_config = Config()
def get_config():
return _config
class WithConfig(object):
@property
def config(self):
return get_config()