-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.py
43 lines (30 loc) · 1020 Bytes
/
settings.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
from __future__ import print_function
import collections
import yaml
class Settings(collections.MutableMapping):
def __init__(self, **entries):
self.__dict__.update(entries)
def __repr__(self):
return str(self.__dict__)
def __iter__(self):
return self.__dict__.__iter__()
def __len__(self):
return len(self.__dict__)
def __getitem__(self, key):
return self.__dict__[key]
def __setitem__(self, key, value):
self.__dict__[key] = value
def __delitem__(self, key):
del self.__dict__[key]
def load_config(fname, section):
"""YAML file name, section string -> Settings object.
"""
with open(fname, "r") as f:
config = yaml.safe_load(f)
return Settings(**config[section])
def formatted_settings_str(settings, headerstr):
s = "{}\n".format(headerstr)
s += "{}\n".format("-" * len(headerstr))
for (key, val) in settings.iteritems():
s += "{}: {}\n".format(key, str(val))
return s