-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynsettings.py
83 lines (72 loc) · 2.6 KB
/
dynsettings.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
# -*- coding: utf-8 -*-
"""
Function import_settings() will load config file in yaml/json format into the caller's local namespace
"""
import os
import sys
import yaml
# import json
def import_settings(fname="settings.yaml"):
flocals = sys._getframe().f_back.f_locals
# mname = sys._getframe().f_back.f_globals['__name__']
ydata = []
with open(fname, 'r') as ysfile:
if fname.endswith('.yaml'):
ydata = yaml.load(ysfile.read())
elif fname.endswith('.json'):
ydata = json.loads(ysfile.read())
if ydata:
for p in ydata:
pname = p
pvalue = None
if isinstance(ydata, list):
if isinstance(p, dict):
pname = p.keys()[0]
pvalue = p[pname]
elif isinstance(ydata, dict):
pname = p
pvalue = ydata[pname]
else:
# print("Incorrect type")
break
# print("Define %s=%s" % (pname, pvalue))
# globals()[pname] = pvalue
# setattr(sys.modules[mname], pname, pvalue)
flocals[pname] = pvalue
def _dictmerge(d1, d2):
""" Recursive merges d2 into d1 if they are dictionaries (by keys), otherwise returns d2 """
if isinstance(d1, dict) and isinstance(d2, dict):
rd = d1
for k in d2:
if k in d1:
rd[k] = _dictmerge(d1[k], d2[k])
else:
rd[k] = d2[k]
return rd
return d2
def dict_chainget(obj, *chain):
""" Get the value from dictionary 'obj' one-by one in chain """
if not chain:
return obj
if not isinstance(obj, dict):
return None
if chain[0] not in obj:
return None
return dict_chainget(obj[chain[0]], *chain[1:])
def load_settings(settings_file='settings.yaml', defaultsettings_file='settings.default.yaml'):
CUR_DIR = os.path.dirname(os.path.abspath(__file__))
_defaultsettings_file = defaultsettings_file
if not _defaultsettings_file.startswith('/'): # not absolute path
_defaultsettings_file = os.path.join(CUR_DIR, _defaultsettings_file)
_settings_file = settings_file
if not _settings_file.startswith('/'): # not absolute path
_settings_file = os.path.join(CUR_DIR, _settings_file)
ds, s = {}, {}
if os.path.exists(_defaultsettings_file):
with open(_defaultsettings_file, 'r') as sf:
ds = yaml.load(sf.read())
if os.path.exists(_settings_file):
with open(_settings_file, 'r') as sf:
s = yaml.load(sf.read())
s = _dictmerge(ds, s)
return s