-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.py
239 lines (193 loc) · 7.8 KB
/
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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
class Settings:
def __init__(self, db):
self.db = db
self.hosts = [
row['hostname']
for row in db.load_table('settings').distinct('hostname')
]
self.keys = [
row['value']
for row in db.load_table('settings').distinct('value')
]
self.data = {}
for host in self.hosts:
self.data[host] = {}
for key in self.keys:
self.data[host][key] = None
for row in db.load_table('settings'):
host = row['hostname']
key = row['value']
value = row['data']
self.data[host][key] = value
def compare(self, host1, host2):
for host in [host1, host2]:
if host not in self.hosts:
raise RuntimeError(f'Host {host} does not exist')
only_host1 = []
only_host2 = []
matching = []
different = []
data1 = self.data[host1]
data2 = self.data[host2]
for key in self.keys:
if data1[key] is not None and data2[key] is None:
only_host1.append((key, data1[key]))
elif data2[key] is not None and data1[key] is None:
only_host2.append((key, data2[key]))
else:
if data1[key] == data2[key]:
matching.append((key, data1[key]))
else:
different.append((key, data1[key], data2[key]))
return (only_host1, only_host2, matching, different)
def copy(self, from_host, to_host, copy_keys):
for host in [from_host, to_host]:
if host not in self.hosts:
raise RuntimeError(f'Host {host} does not exist')
table = self.db.load_table('settings')
for key in copy_keys:
if key not in self.keys:
raise RuntimeError(f'Key {key} does not exist')
if self.data[from_host][key] is None:
raise RuntimeError(f'Cannot copy missing value for key {key}')
if self.data[from_host][key] == self.data[to_host][key]:
continue
row = {
'hostname': to_host,
'value': key,
'data': self.data[from_host][key],
}
table.upsert(row, keys = ['hostname', 'value'])
self.data[to_host][key] = self.data[from_host][key]
class Bindings:
def __init__(self, db):
self.db = db
self.hosts = [
row['hostname']
for row in db.load_table('keybindings').distinct('hostname')
]
self.actions = [
(row['context'], row['action'])
for row in db.load_table('keybindings').distinct('context', 'action')
]
self.data = {}
for host in self.hosts:
self.data[host] = {}
for action in self.actions:
self.data[host][action] = None
for row in db.load_table('keybindings'):
host = row['hostname']
action = (row['context'], row['action'])
keys = row['keylist']
self.data[host][action] = keys
def compare(self, host1, host2):
for host in [host1, host2]:
if host not in self.hosts:
raise RuntimeError(f'Host {host} does not exist')
only_host1 = []
only_host2 = []
matching = []
different = []
data1 = self.data[host1]
data2 = self.data[host2]
for action in self.actions:
if data1[action] is not None and data2[action] is None:
only_host1.append((action, data1[action]))
elif data2[action] is not None and data1[action] is None:
only_host2.append((action, data2[action]))
else:
if data1[action] == data2[action]:
matching.append((action, data1[action]))
else:
different.append((action, data1[action], data2[action]))
return (only_host1, only_host2, matching, different)
def copy(self, from_host, to_host, copy_actions):
for host in [from_host, to_host]:
if host not in self.hosts:
raise RuntimeError(f'Host {host} does not exist')
table = self.db.load_table('keybindings')
for action in copy_actions:
if action not in self.actions:
raise RuntimeError(f'Action {action} does not exist')
if self.data[from_host][action] is None:
raise RuntimeError(f'Cannot copy missing keys for action {action}')
if self.data[from_host][action] == self.data[to_host][action]:
continue
row = {
'hostname': to_host,
'context': action[0],
'action': action[1],
'keylist': self.data[from_host][action],
'description': table.find_one(
hostname = from_host,
context = action[0],
action = action[1],
)['description'],
}
table.upsert(row, keys = ['hostname', 'context', 'action'])
self.data[to_host][action] = self.data[from_host][action]
class JumpPoints:
def __init__(self, db):
self.db = db
self.hosts = [
row['hostname']
for row in db.load_table('jumppoints').distinct('hostname')
]
self.dests = [
row['destination']
for row in db.load_table('jumppoints').distinct('destination')
]
self.data = {}
for host in self.hosts:
self.data[host] = {}
for dest in self.dests:
self.data[host][dest] = None
for row in db.load_table('jumppoints'):
host = row['hostname']
dest = row['destination']
keys = row['keylist']
self.data[host][dest] = keys
def compare(self, host1, host2):
for host in [host1, host2]:
if host not in self.hosts:
raise RuntimeError(f'Host {host} does not exist')
only_host1 = []
only_host2 = []
matching = []
different = []
data1 = self.data[host1]
data2 = self.data[host2]
for dest in self.dests:
if data1[dest] is not None and data2[dest] is None:
only_host1.append((dest, data1[dest]))
elif data2[dest] is not None and data1[dest] is None:
only_host2.append((dest, data2[dest]))
else:
if data1[dest] == data2[dest]:
matching.append((dest, data1[dest]))
else:
different.append((dest, data1[dest], data2[dest]))
return (only_host1, only_host2, matching, different)
def copy(self, from_host, to_host, copy_dests):
for host in [from_host, to_host]:
if host not in self.hosts:
raise RuntimeError(f'Host {host} does not exist')
table = self.db.load_table('jumppoints')
for dest in copy_dests:
if dest not in self.dests:
raise RuntimeError(f'Destination {dest} does not exist')
if self.data[from_host][dest] is None:
raise RuntimeError(f'Cannot copy missing keys for destination {dest}')
if self.data[from_host][dest] == self.data[to_host][dest]:
continue
row = {
'hostname': to_host,
'destination': dest,
'keylist': self.data[from_host][dest],
'description': table.find_one(
hostname = from_host,
destination = dest,
)['description'],
}
table.upsert(row, keys = ['hostname', 'destination'])
self.data[to_host][dest] = self.data[from_host][dest]