Rewrite stuff

This commit is contained in:
Lynn Ochs 2020-07-18 14:49:37 +02:00
parent 9de30d0cbf
commit f0e5680f6c

76
sync.py
View File

@ -2,6 +2,10 @@ import yaml
import caldav import caldav
import sys import sys
if len(sys.argv) > 1:
import os
os.chdir(sys.argv[1])
with open('auth.yaml') as f: with open('auth.yaml') as f:
auth = yaml.full_load(f) auth = yaml.full_load(f)
try: try:
@ -18,7 +22,6 @@ cals = []
for cal in auth['cals']: for cal in auth['cals']:
cals.append(caldav.Calendar(client = logins[cal['login']], url = cal['url'])) cals.append(caldav.Calendar(client = logins[cal['login']], url = cal['url']))
new = {}
changed = {} changed = {}
deleted = {} deleted = {}
@ -30,58 +33,39 @@ def cal_contains_uid(cal, uid):
return False return False
for cal in cals: for cal in cals:
print("Checking %s" % (cal.canonical_url,))
for event in cal.events(): for event in cal.events():
uid = event.vobject_instance.vevent.uid.value uid = event.vobject_instance.vevent.uid.value
data = event.data data = event.data
entry = (cal.canonical_url, data) entry = (cal.canonical_url, event)
if uid not in known: if uid not in known:
if uid not in new: print("Found new event with summary %s" % (event.vobject_instance.vevent.summary.value,))
new[uid] = [entry] if uid not in changed:
changed[uid] = [entry]
else: else:
new[uid].append(entry) changed[uid].append(entry)
else: else:
if data != known[uid]: if data != known[uid]:
print("Found changed event with summary %s" % (event.vobject_instance.vevent.summary.value,))
if uid not in changed: if uid not in changed:
changed[uid] = [entry] changed[uid] = [entry]
else: else:
changed[uid].append(entry) changed[uid].append(entry)
print("Checking for deletions")
for uid, data in known.items(): for uid, data in known.items():
for cal in cals: for cal in cals:
if not cal_contains_uid(cal, uid): if not cal_contains_uid(cal, uid):
print("Item with uid %s got deleted in %s" % (uid, cal.canonical_url))
if uid not in deleted: if uid not in deleted:
deleted[uid] = [cal.canonical_url] deleted[uid] = [cal.canonical_url]
else: else:
deleted[uid].append(cal.canonical_url) deleted[uid].append(cal.canonical_url)
print("New:")
print(new)
print()
print("Changed:")
print(changed)
print()
print("Deleted:")
print(deleted)
print()
for uid, arr in new.items():
if len(set([entry[1] for entry in arr])) != 1:
print("Multiple new entries with the same UID (%s) detected:" % (uid,), file=sys.stderr)
for ev in arr:
print(" %s: %s" % (ev[0], ev[1].vobject_instance.vevent.summary.value), file = sys.stderr)
print("", file=sys.stderr)
continue
entry = arr[0]
known_cals = [ entry[0] for entry in arr ]
for cal in cals:
if cal.canonical_url not in known_cals:
cal.save_event(entry[1], no_overwrite=True)
known[uid] = entry[1]
for uid, arr in changed.items(): for uid, arr in changed.items():
if uid in deleted: if uid in deleted:
print("Changed entry with UID (%s) and was also deleted:" % (uid, ), file=sys.stderr) print("Changed/new entry with UID (%s), summary \"%s\" was also deleted, recreating it." % (uid, arr[0][1].vobject_instance.vevent.uid.value), file=sys.stderr)
print("Deletions in:", file=sys.stderr) print("Deletions in:", file=sys.stderr)
for cal in deleted[uid]: for cal in deleted[uid]:
print(" - %s" % (cal,), file=sys.stderr) print(" - %s" % (cal,), file=sys.stderr)
@ -89,30 +73,40 @@ for uid, arr in changed.items():
for entry in arr: for entry in arr:
print(" - %s" % (entry[0],), file=sys.stderr) print(" - %s" % (entry[0],), file=sys.stderr)
print("", file=sys.stderr) print("", file=sys.stderr)
continue del deleted[uid]
if len(set([entry[1] for entry in arr])) != 1: if len(set([entry[1].data for entry in arr])) != 1:
print("Multiple changed entries with the same UID (%s) detected:" % (uid,), file=sys.stderr) print("Multiple changed/new entries with the same UID (%s) detected:" % (uid,), file=sys.stderr)
for ev in arr: for ev in arr:
print(" %s: %s" % (ev[0], ev[1].vobject_instance.vevent.summary.value), file = sys.stderr) print(" %s: %s" % (ev[0], ev[1].vobject_instance.vevent.summary.value), file = sys.stderr)
del deleted[uid]
continue continue
entry = arr[0] entry = arr[0]
changed_cals = [ entry[0] for entry in arr ] changed_cals = [ entry[0] for entry in arr ]
for cal in cals: for cal in cals:
if cal.canonical_url not in changed_cals: if cal.canonical_url not in changed_cals:
cal.save_event(entry[1], no_create=True) try:
known[uid] = entry[1] old = cal.event_by_uid(uid)
print("Overwriting event from %s with data %s (source: %s)" % (cal.canonical_url, repr(old.data), changed_cals))
new = False
except:
new = True
cal.save_event(entry[1].data)
if new:
print("Creating new event in %s with summary %s" % (cal.canonical_url, cal.event_by_uid(uid).vobject_instance.vevent.summary.value))
known[uid] = entry[1].data
for uid in deleted: for uid in deleted:
for cal in cals: for cal in cals:
try: if cal_contains_uid(cal, uid):
evt = cal.event_by_uid(uid)
print("Deleting event from %s with data %s (source: %s)" % (cal.canonical_url, repr(evt.data), deleted[uid]))
cal.event_by_uid(uid).delete() cal.event_by_uid(uid).delete()
except: del known[uid]
pass
print(known) print("Done.")
print()
with open("data.yaml", "w") as f: with open("data.yaml", "w") as f:
yaml.dump(known, f) yaml.dump(known, f)