Skip to content

Commit 95bf683

Browse files
Add contrib script to resolve conflicts
Asks the user when two iCalender objects conflict during a sync, which one to take.
1 parent 5844480 commit 95bf683

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python3
2+
"""Ask user to resolve a vdirsyncer sync conflict interactively.
3+
4+
Needs a way to ask the user.
5+
The use of https://apps.kde.org/kdialog/ for GNU/Linix is hardcoded.
6+
7+
Depends on python>3.5 and KDialog.
8+
9+
Usage:
10+
Ensure the file executable and use it in the vdirsyncer.conf file, e.g.
11+
12+
conflict_resolution = ["command", "/home/bern/vdirsyncer/resolve_interactively.py"]
13+
14+
This file is Free Software under the following license:
15+
SPDX-License-Identifier: Apache-2.0
16+
SPDX-FileCopyrightText: 2021 Intevation GmbH <https://intevation.de>
17+
18+
"""
19+
from pathlib import Path
20+
import re
21+
import subprocess
22+
import sys
23+
24+
KDIALOG = "/usr/bin/kdialog"
25+
26+
SUMMARY_PATTERN = re.compile("^(SUMMARY:.*)$", re.MULTILINE)
27+
28+
def get_summary(icalendar_text:str):
29+
"""Get the first SUMMARY: line from an iCalendar text.
30+
31+
Do not care about the line being continued.
32+
"""
33+
match = re.search(SUMMARY_PATTERN, icalendar_text)
34+
return match[1]
35+
36+
37+
def main(ical1_filename, ical2_filename):
38+
ical1 = ical1_filename.read_text()
39+
ical2 = ical2_filename.read_text()
40+
41+
additional_args = ["--yes-label", "take first"] # return code == 0
42+
additional_args += ["--no-label", "take second"] # return code == 1
43+
additional_args += ["--cancel-label", "do not resolve"] # return code == 2
44+
45+
r = subprocess.run(args = [
46+
KDIALOG,
47+
"--warningyesnocancel",
48+
"There was a sync conflict, do you prefer the first entry: \n" +
49+
get_summary(ical1) + "...\n(full contents: " + str(ical1_filename) +
50+
" )\n\nor the second entry: \n" +
51+
get_summary(ical2) + "...\n(full contents: " + str(ical2_filename) +
52+
" )?"
53+
] + additional_args)
54+
55+
if r.returncode == 2:
56+
# cancel was pressed
57+
return # shall lead to items not changed, because not copied
58+
59+
if r.returncode == 0:
60+
# we want to take the first item, so overwrite the second
61+
ical2_filename.write_text(ical1)
62+
else: # r.returncode == 1, we want the second item, so overwrite the first
63+
ical1_filename.write_text(ical2)
64+
65+
if len(sys.argv) != 3:
66+
sys.stdout.write(__doc__)
67+
else:
68+
main(Path(sys.argv[1]), Path(sys.argv[2]))

0 commit comments

Comments
 (0)