-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
53 lines (41 loc) · 1.5 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
53
#!/usr/bin/python -p
import logging
import yaml
lg = logging.getLogger(__name__)
input_cache = {}
def load_config(input_defaults=None, input_file=None):
"""
Load config parameters either from file or stdin
"""
if input_file:
lg.debug("Loading default inputs from yaml file {}".format(input_file))
try:
with open(input_file) as f:
ifile = yaml.full_load(f)
except IOError as e:
lg.error("Can't open config file {0}: {1}".format(input_file, e))
raise
for key, value in ifile.items():
if not isinstance(value, str):
lg.error("Input key {0} has not string-typed value ({1}), ignoring".format(key, value))
continue
input_cache[key] = value
if input_defaults:
for input in input_defaults:
try:
key, value = input.split('=')
except ValueError:
lg.error("Submitted input value {} is not a valid key=pair, skipping".format(input))
continue
input_cache[key] = value
return input_cache
def load_measurements(input_file):
lg.debug("Loading measurements list from file {}".format(input_file))
try:
with open(input_file) as f:
mlist = f.read().splitlines()
except IOError as e:
lg.error("Can't open config file {0}: {1}".format(input_file, e))
raise
return mlist
# print(load_config(input_file='secrets.yml')["loxone::host"])