-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
214 lines (196 loc) · 7.57 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Copyright 2024.
# This file is part of Amity.
# Amity is free software: you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
import tools
log = tools.logger(__name__)
import os, re, shutil, tempfile, time, yaml
class Config(object):
def __init__(self, filename):
self.default_paths = {}
self.user_cfg = {}
self.cfg = {}
self.loaded = False
self.filename = filename
self.default('config.max_backups', 10)
log.info(f'filename {self.filename}')
def load(self):
"""Must be called after all default() calls have been made"""
log.info(f'Load {self.filename}')
try:
with open(self.filename, 'r') as file:
self.user_cfg = yaml.safe_load(file)
if self.user_cfg is None:
log.info(f'{self.filename} is empty. Defaulting to empty config.')
self.user_cfg = {}
except FileNotFoundError:
log.info(f'{self.filename} not found. Defaulting to empty config.')
self.__overlay(self.cfg, self.user_cfg)
self.loaded = True
def save(self, backup=False):
log.info(f'Save {self.filename}')
if backup:
# Make a backup of the config file
backup_time_str = time.strftime('%Y%m%d-%H%M%S')
backup_filename = f'{self.filename}-{backup_time_str}'
try:
shutil.copy(self.filename, backup_filename)
log.info(f'Created backup {backup_filename}')
# If running as root, make backup ownership the same as the original
if os.getuid() == 0:
stat = os.stat(self.filename)
shutil.chown(backup_filename, stat.st_uid, stat.st_gid)
except FileNotFoundError:
pass
self.clean_backups()
tmp_fd, tmp_path = tempfile.mkstemp()
with os.fdopen(tmp_fd, 'w') as file:
yaml.safe_dump(self.user_cfg, file)
shutil.move(tmp_path, self.filename)
# Fixup ownership, if running as root
if os.getuid() == 0:
stat = os.stat('.')
shutil.chown(self.filename, stat.st_uid, stat.st_gid)
def clean_backups(self):
path = os.path.dirname(self.filename)
if path == '':
path = '.'
basename = os.path.basename(self.filename)
entries = []
m = re.compile(basename + r'-(\d{8}-\d{6})')
for entry in os.scandir(path):
if m.match(entry.name):
entries.append(entry)
entries.sort(reverse=True, key=lambda entry: entry.stat().st_ctime)
max_backups = self['config.max_backups']
for entry in entries[max_backups:]:
log.info(f'Deleting backup {entry.name}')
os.unlink(os.path.join(path, entry.name))
def save_complete(self, filename):
with open(filename, 'w') as file:
yaml.safe_dump(self.cfg, file)
def __overlay(self, node, overlay_node):
if type(node) is dict:
# User misconfiguration if this assert triggers
assert type(node) is type(overlay_node)
for name, value in overlay_node.items():
if name not in node:
node[name] = value
else:
scalar = self.__overlay(node[name], overlay_node[name])
if scalar:
node[name] = overlay_node[name]
elif type(node) is list:
# User misconfiguration if this assert triggers
assert type(node) is type(overlay_node)
for i, value in enumerate(overlay_node):
if i >= len(node):
node.append(value)
else:
scalar = self.__overlay(node[i], overlay_node[i])
if scalar:
node[i] = overlay_node[i]
else:
return True
return False
def default(self, path, value):
assert not self.loaded
assert type(path) is str
if path in self.default_paths:
assert value == self.default_paths[path]
else:
self.default_paths[path] = value
self.__apply(self.cfg, path, value)
log.info(f"Default '{path}' = '{value}'")
def __apply(self, node, path, value):
elems = list(self.__elements(path))
end_elem = (None, None)
elems.append(end_elem)
for i, elem in enumerate(elems[:-1]):
next_elem = elems[i + 1]
sep, name = elem
if sep == '.': # dict
assert type(node) is dict
if next_elem is end_elem:
# Set the default value
node[name] = value
return
if name in node:
node = node[name]
else:
next_sep, _ = next_elem
if next_sep == '.':
next_node = {}
else:
next_node = []
node[name] = next_node
node = next_node
else: # sep == ',' # list
assert type(node) is list
index = int(name)
if index >= len(node):
node.extend([None] * (index - len(node) + 1))
if next_elem is end_elem:
# Set the default value
node[index] = value
return
if node[index] is None:
next_sep, _ = next_elem
if next_sep == '.':
next_node = {}
else:
next_node = []
node[index] = next_node
node = node[index]
def __elements(self, path):
elems = re.split(r'(\.|,)', path)
if elems[0] == '':
elems.pop(0)
else:
elems.insert(0, '.')
assert len(elems) % 2 == 0
return ((elems[i], elems[i+1]) for i in range(0, len(elems), 2))
def __getitem__(self, path):
assert type(path) is str
elems = self.__elements(path)
node = self.cfg
for sep, name in elems:
if sep == '.': # dict
assert type(node) is dict
if name not in node:
return None
node = node[name]
else: # sep == ',' # list
assert type(node) is list
index = int(name)
if index >= len(node):
return None
node = node[index]
return node
def __setitem__(self, path, value):
assert type(path) is str
if not path:
self.cfg = value
self.user_cfg = value
else:
self.__apply(self.user_cfg, path, value)
self.__apply(self.cfg, path, value)
def replace_user_root(self, value):
""" Replace all user settings while maintaining defaults """
self.cfg = {}
for path, default_value in self.default_paths.items():
self.__apply(self.cfg, path, default_value)
self.user_cfg = value
self.__overlay(self.cfg, self.user_cfg)
def main():
c = Config('test.yaml')
c.default('remote.mac', 0)
c.load()
print(f'1 {c.user_cfg}')
previous = c['remote.mac']
c['remote.mac'] = previous + 1
print(f'2 {c.user_cfg}')
c.save(True)
if __name__ == '__main__':
main()