forked from gramps-project/addons-source
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffhandler.py
More file actions
383 lines (352 loc) · 14.8 KB
/
Copy pathdiffhandler.py
File metadata and controls
383 lines (352 loc) · 14.8 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2021-2024 David Straub
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""Class managing the difference between two databases."""
from __future__ import annotations
import logging
from copy import deepcopy
from datetime import datetime
from gramps.gen.db.base import DbReadBase
from gramps.gen.db import DbTxn
from gramps.gen.merge.diff import diff_dbs
from gramps.gen.user import User
from const import (
A_ADD_LOC,
A_ADD_REM,
A_DEL_LOC,
A_DEL_REM,
A_MRG_REM,
A_UPD_LOC,
A_UPD_REM,
C_ADD_LOC,
C_ADD_REM,
C_DEL_LOC,
C_DEL_REM,
C_UPD_BOTH,
C_UPD_LOC,
C_UPD_REM,
MODE_BIDIRECTIONAL,
MODE_RESET_TO_LOCAL,
MODE_RESET_TO_REMOTE,
MODE_MERGE,
OBJ_LST,
Action,
Actions,
GrampsObject,
)
LOG = logging.getLogger("grampswebsync")
class WebApiSyncDiffHandler:
"""Class managing the difference between two databases."""
def __init__(
self,
db1: DbReadBase,
db2: DbReadBase,
user: User,
last_synced: float | None = None,
) -> None:
"""Initialize given the two databases and a User instance."""
self.db1 = db1
self.db2 = db2
self.user = user
self._diff_dbs = self.get_diff_dbs()
self.differences: dict[tuple[str, str], tuple[GrampsObject, GrampsObject]] = {
(obj1.handle, obj_type): (obj1, obj2)
for (obj_type, obj1, obj2) in self._diff_dbs[0]
}
self.missing_from_db1: dict[tuple[str, str], GrampsObject] = {
(obj.handle, obj_type): obj for (obj_type, obj) in self._diff_dbs[1]
}
self.missing_from_db2: dict[tuple[str, str], GrampsObject] = {
(obj.handle, obj_type): obj for (obj_type, obj) in self._diff_dbs[2]
}
self._latest_common_timestamp = self.get_latest_common_timestamp()
LOG.debug("Last synced timestamp from config: %s (%s)",
last_synced,
datetime.fromtimestamp(last_synced).strftime('%Y-%m-%d %H:%M:%S %Z') if last_synced else "None")
LOG.debug("Latest common timestamp calculated: %s (%s)",
self._latest_common_timestamp,
datetime.fromtimestamp(self._latest_common_timestamp).strftime('%Y-%m-%d %H:%M:%S %Z'))
if last_synced and last_synced > self._latest_common_timestamp:
# if the last sync timestamp in the config is later than
# the latest common timestamp, use it
self._latest_common_timestamp = int(last_synced)
LOG.debug("Using last synced timestamp as cutoff: %s (%s)",
self._latest_common_timestamp,
datetime.fromtimestamp(self._latest_common_timestamp).strftime('%Y-%m-%d %H:%M:%S %Z'))
def get_diff_dbs(
self,
) -> tuple[
list[tuple[str, GrampsObject, GrampsObject]],
list[tuple[str, GrampsObject]],
list[tuple[str, GrampsObject]],
]:
"""Return a database diff tuple: changed, missing from 1, missing from 2."""
return diff_dbs(self.db1, self.db2, user=self.user)
def get_latest_common_timestamp(self) -> int:
"""Get the timestamp of the latest common object."""
dates = [
self._get_latest_common_timestamp(class_name) or 0 for class_name in OBJ_LST
]
return max(dates)
def _get_latest_common_timestamp(self, class_name: str) -> int | None:
"""Get the timestamp of the latest common object of given type."""
handles_func = self.db1.method("get_%s_handles", class_name)
handle_func = self.db1.method("get_%s_from_handle", class_name)
handle_func_db2 = self.db2.method("get_%s_from_handle", class_name)
assert handles_func and handle_func and handle_func_db2 # for type checker
# all handles in db1
all_handles = set(handles_func())
# all handles missing in db2
missing_in_db2 = set(
handle
for handle, obj_type in self.missing_from_db2.keys()
if obj_type == class_name
)
# all handles of objects that are different
different = set(
handle
for handle, obj_type in self.differences.keys()
if obj_type == class_name
)
# handles of all objects that are the same
same_handles = all_handles - missing_in_db2 - different
if not same_handles:
return None
date = 0
for handle in same_handles:
obj = handle_func(handle)
obj2 = handle_func_db2(handle)
if obj.change == obj2.change: # make sure last mod dates are equal
date = max(date, obj.change)
return date
@property
def modified_in_db1(
self,
) -> dict[tuple[str, str], tuple[GrampsObject, GrampsObject]]:
"""Objects that have been modifed in db1."""
return {
k: (obj1, obj2)
for k, (obj1, obj2) in self.differences.items()
if obj1.change > self._latest_common_timestamp
and obj2.change <= self._latest_common_timestamp
}
@property
def modified_in_db2(
self,
) -> dict[tuple[str, str], tuple[GrampsObject, GrampsObject]]:
"""Objects that have been modifed in db1."""
return {
k: (obj1, obj2)
for k, (obj1, obj2) in self.differences.items()
if obj1.change <= self._latest_common_timestamp
and obj2.change > self._latest_common_timestamp
}
@property
def modified_in_both(
self,
) -> dict[tuple[str, str], tuple[GrampsObject, GrampsObject]]:
"""Objects that have been modifed in both databases."""
return {
k: v
for k, v in self.differences.items()
if k not in self.modified_in_db1 and k not in self.modified_in_db2
}
@property
def added_to_db1(self) -> dict[tuple[str, str], GrampsObject]:
"""Objects that have been added to db1."""
return {
k: obj
for (k, obj) in self.missing_from_db2.items()
if obj.change > self._latest_common_timestamp
}
@property
def added_to_db2(self) -> dict[tuple[str, str], GrampsObject]:
"""Objects that have been added to db2."""
return {
k: obj
for (k, obj) in self.missing_from_db1.items()
if obj.change > self._latest_common_timestamp
}
@property
def deleted_from_db1(self) -> dict[tuple[str, str], GrampsObject]:
"""Objects that have been deleted from db1."""
return {
k: v for k, v in self.missing_from_db1.items() if k not in self.added_to_db2
}
@property
def deleted_from_db2(self) -> dict[tuple[str, str], GrampsObject]:
"""Objects that have been deleted from db2."""
return {
k: v for k, v in self.missing_from_db2.items() if k not in self.added_to_db1
}
def get_changes(self) -> Actions:
"""Get a list of objects and corresponding changes."""
lst = []
for (handle, obj_type), (obj1, obj2) in self.modified_in_both.items():
lst.append((C_UPD_BOTH, handle, obj_type, obj1, obj2))
for (handle, obj_type), obj in self.added_to_db1.items():
lst.append((C_ADD_LOC, handle, obj_type, obj, None))
for (handle, obj_type), obj in self.added_to_db2.items():
lst.append((C_ADD_REM, handle, obj_type, None, obj))
for (handle, obj_type), obj in self.deleted_from_db1.items():
lst.append((C_DEL_LOC, handle, obj_type, None, obj))
for (handle, obj_type), obj in self.deleted_from_db2.items():
lst.append((C_DEL_REM, handle, obj_type, obj, None))
for (handle, obj_type), (obj1, obj2) in self.modified_in_db1.items():
lst.append((C_UPD_LOC, handle, obj_type, obj1, obj2))
for (handle, obj_type), (obj1, obj2) in self.modified_in_db2.items():
lst.append((C_UPD_REM, handle, obj_type, obj1, obj2))
# Log summary of sync decisions
LOG.debug("=== SYNC DECISION SUMMARY ===")
LOG.debug("Cutoff timestamp used: %s (%s)",
self._latest_common_timestamp,
datetime.fromtimestamp(self._latest_common_timestamp).strftime('%Y-%m-%d %H:%M:%S %Z'))
LOG.debug("Added to local (remote objects): %d", len(self.added_to_db1))
LOG.debug("Added to remote (local objects): %d", len(self.added_to_db2))
LOG.debug("Deleted from local: %d", len(self.deleted_from_db1))
LOG.debug("Deleted from remote: %d", len(self.deleted_from_db2))
LOG.debug("Modified in local: %d", len(self.modified_in_db1))
LOG.debug("Modified in remote: %d", len(self.modified_in_db2))
LOG.debug("Modified in both: %d", len(self.modified_in_both))
LOG.debug("=== END SUMMARY ===")
return lst
def get_actions(self) -> Actions:
"""Get a list of objects and corresponding actions."""
lst = []
for (handle, obj_type), (obj1, obj2) in self.modified_in_both.items():
lst.append((A_MRG_REM, handle, obj_type, obj1, obj2))
for (handle, obj_type), obj in self.added_to_db1.items():
lst.append((A_ADD_REM, handle, obj_type, obj, None))
for (handle, obj_type), obj in self.added_to_db2.items():
lst.append((A_ADD_LOC, handle, obj_type, None, obj))
for (handle, obj_type), obj in self.deleted_from_db1.items():
lst.append((A_DEL_REM, handle, obj_type, None, obj))
for (handle, obj_type), obj in self.deleted_from_db2.items():
lst.append((A_DEL_LOC, handle, obj_type, obj, None))
for (handle, obj_type), (obj1, obj2) in self.modified_in_db1.items():
lst.append((A_UPD_REM, handle, obj_type, obj1, obj2))
for (handle, obj_type), (obj1, obj2) in self.modified_in_db2.items():
lst.append((A_UPD_LOC, handle, obj_type, obj1, obj2))
return lst
def commit_action(self, action: Action, trans1: DbTxn, trans2: DbTxn) -> None:
"""Commit an action into local and remote transaction objects."""
typ, handle, obj_type, obj1, obj2 = action
if typ == A_DEL_LOC:
method = self.db1.method("remove_%s", obj_type)
assert method # for type checker
method(handle, trans1)
elif typ == A_DEL_REM:
method = self.db2.method("remove_%s", obj_type)
assert method # for type checker
method(handle, trans2)
elif typ == A_ADD_LOC:
method = self.db1.method("add_%s", obj_type)
assert method # for type checker
method(obj2, trans1)
elif typ == A_ADD_REM:
method = self.db2.method("add_%s", obj_type)
assert method # for type checker
method(obj1, trans2)
elif typ == A_UPD_LOC:
method = self.db1.method("commit_%s", obj_type)
assert method # for type checker
method(obj2, trans1)
elif typ == A_UPD_REM:
method = self.db2.method("commit_%s", obj_type)
assert method # for type checker
method(obj1, trans2)
elif typ == A_MRG_REM:
assert obj1 and obj2 # for type checker
obj_merged = deepcopy(obj2)
obj1_nogid = deepcopy(obj1)
obj1_nogid.gramps_id = None
obj_merged.merge(obj1_nogid)
method = self.db1.method("commit_%s", obj_type)
assert method # for type checker
method(obj_merged, trans1)
method = self.db2.method("commit_%s", obj_type)
assert method # for type checker
method(obj_merged, trans2)
def commit_actions(self, actions: Actions, trans1: DbTxn, trans2: DbTxn) -> None:
"""Commit several actions into local and remote transaction objects."""
for action in actions:
self.commit_action(action, trans1, trans2)
def changes_to_actions(changes, sync_mode: int) -> Actions:
"""Get actions from changes depending on sync mode."""
if sync_mode == MODE_BIDIRECTIONAL:
change_to_action = {
C_UPD_BOTH: A_MRG_REM,
C_ADD_LOC: A_ADD_REM,
C_ADD_REM: A_ADD_LOC,
C_DEL_LOC: A_DEL_REM,
C_DEL_REM: A_DEL_LOC,
C_UPD_LOC: A_UPD_REM,
C_UPD_REM: A_UPD_LOC,
}
elif sync_mode == MODE_RESET_TO_LOCAL:
change_to_action = {
C_UPD_BOTH: A_UPD_REM,
C_ADD_LOC: A_ADD_REM,
C_ADD_REM: A_DEL_REM,
C_DEL_LOC: A_DEL_REM,
C_DEL_REM: A_ADD_REM,
C_UPD_LOC: A_UPD_REM,
C_UPD_REM: A_UPD_REM,
}
elif sync_mode == MODE_RESET_TO_REMOTE:
change_to_action = {
C_UPD_BOTH: A_UPD_LOC,
C_ADD_LOC: A_DEL_LOC,
C_ADD_REM: A_ADD_LOC,
C_DEL_LOC: A_ADD_LOC,
C_DEL_REM: A_DEL_LOC,
C_UPD_LOC: A_UPD_LOC,
C_UPD_REM: A_UPD_LOC,
}
elif sync_mode == MODE_MERGE:
change_to_action = {
C_UPD_BOTH: A_MRG_REM,
C_ADD_LOC: A_ADD_REM,
C_ADD_REM: A_ADD_LOC,
C_DEL_LOC: A_ADD_LOC,
C_DEL_REM: A_ADD_REM,
C_UPD_LOC: A_UPD_REM,
C_UPD_REM: A_UPD_LOC,
}
else:
raise ValueError(f"Invalid sync mode: {sync_mode}")
actions = []
for change in changes:
change_type, handle, obj_type, obj1, obj2 = change
action_type = change_to_action[change_type]
action = action_type, handle, obj_type, obj1, obj2
actions.append(action)
return actions
def has_local_actions(actions: Actions) -> bool:
"""Whether any of the changes affect the local database."""
for action in actions:
# note: A_MRG_REM affects both dbs
if action[0] in (A_ADD_LOC, A_DEL_LOC, A_UPD_LOC, A_MRG_REM):
return True
return False
def has_remote_actions(actions: Actions) -> bool:
"""Whether any of the changes affect the remote database."""
for action in actions:
# note: A_MRG_REM affects both dbs
if action[0] in (A_ADD_REM, A_DEL_REM, A_UPD_REM, A_MRG_REM):
return True
return False