-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.py
137 lines (96 loc) · 3.86 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import os
import sys
import yaml
import inflect
import contextlib
import numpy as np
from pathlib import Path
from collections import OrderedDict
class CustomDumper(yaml.SafeDumper):
# inspired by https://stackoverflow.com/a/44284819/3786245
def write_line_break(self, data=None):
super().write_line_break(data)
if len(self.indents) == 1:
super().write_line_break()
@contextlib.contextmanager
def smart_open(filename=None):
if filename and filename != '-':
fh = open(filename, 'w')
else:
fh = sys.stdout
try:
yield fh
finally:
if fh is not sys.stdout:
fh.close()
class Configuration:
name = 'Configuration'
def __init__(self, fpath=None):
if fpath == None:
fpath = os.environ['CODE_ROOT'] + '/configs/config.yaml'
self.fpath = fpath
print(f'\n\nReading {fpath}')
# Load the config data into memory.
with open(fpath) as f:
config = yaml.safe_load(f)
self.config_types = sorted(list(config.keys()))
for xx in ['runtime', 'comments']:
if xx in self.config_types:
self.config_types.remove(xx)
self.config_types = ['runtime'] + self.config_types + ['comments']
self.attributes = OrderedDict()
# print(self.config_types)
for key in self.config_types:
self.attributes[key] = config[key]
sub_config = config[key]
keys = sub_config.keys()
for key in keys:
setattr(self, key, sub_config[key])
if self.attributes['runtime']['replay']:
raise NotImplementedError()
self.fpath = os.environ['GOLD_DIR'] + '/configs/config.yaml'
def update_comments(self, comments):
p = inflect.engine()
ps = [p.ordinal(i) for i in range(1, 50, 1)]
ps = [str(p) for p in ps if p not in self.attributes['comments'].keys()]
for p, comment in zip(ps, comments):
self.attributes['comments'][p] = comment
def print_attributes(self, output=None):
attr = dict(self.attributes)
# https://stackoverflow.com/questions/17602878/how-to-handle-both-with-open-and-sys-stdout-nicely
with smart_open(output) as fh:
yaml.dump(attr, fh, default_flow_style=False, Dumper=CustomDumper, sort_keys=False)
def update_attributes(self, script, new):
if hasattr(new, '__dict__'):
new = new.__dict__
assert isinstance(new, dict)
for key in sorted(new.keys()):
setattr(self, key, new[key])
if script in self.config_types:
base_types = np.array([x.split('_')[0] for x in self.config_types])
script += '_{}'.format(np.count_nonzero(base_types == script))
self.config_types += [script]
self.attributes[script] = new
'''
for key in self.config_types:
keys = self.attributes[key].keys()
for subkey in keys:
self.attributes[key][subkey] = getattr(self, subkey)
'''
def setup_replay(self, new_vars):
for key in self.config_types:
new_vars.update(self.attributes[key])
def write(self, opath=None):
if opath == None:
opath = self.fpath
self.attributes['runtime']['user'] = os.environ['USER']
Path(os.path.dirname(opath)).mkdir(parents=True, exist_ok=True)
print(f'Writing {opath}')
with open(opath, 'w', encoding = 'utf-8') as ofile:
# TODO: remove dict to retain order.
yaml.dump(dict(self.attributes), ofile, default_flow_style=False, Dumper=CustomDumper, sort_keys=False)
if __name__ == '__main__':
config = Configuration()
config.update_comments(['New comment'])
config.write()
print('\n\nDone.\n\n')