From f0e5680f6cc8905671d422c567628127e28b59fd Mon Sep 17 00:00:00 2001 From: Valentin Ochs Date: Sat, 18 Jul 2020 14:49:37 +0200 Subject: [PATCH] Rewrite stuff --- sync.py | 76 ++++++++++++++++++++++++++------------------------------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/sync.py b/sync.py index 9c00818..8ac387c 100644 --- a/sync.py +++ b/sync.py @@ -2,6 +2,10 @@ import yaml import caldav import sys +if len(sys.argv) > 1: + import os + os.chdir(sys.argv[1]) + with open('auth.yaml') as f: auth = yaml.full_load(f) try: @@ -18,7 +22,6 @@ cals = [] for cal in auth['cals']: cals.append(caldav.Calendar(client = logins[cal['login']], url = cal['url'])) -new = {} changed = {} deleted = {} @@ -30,58 +33,39 @@ def cal_contains_uid(cal, uid): return False for cal in cals: + print("Checking %s" % (cal.canonical_url,)) for event in cal.events(): uid = event.vobject_instance.vevent.uid.value data = event.data - entry = (cal.canonical_url, data) + entry = (cal.canonical_url, event) if uid not in known: - if uid not in new: - new[uid] = [entry] + print("Found new event with summary %s" % (event.vobject_instance.vevent.summary.value,)) + if uid not in changed: + changed[uid] = [entry] else: - new[uid].append(entry) + changed[uid].append(entry) else: if data != known[uid]: + print("Found changed event with summary %s" % (event.vobject_instance.vevent.summary.value,)) if uid not in changed: changed[uid] = [entry] else: changed[uid].append(entry) + +print("Checking for deletions") for uid, data in known.items(): for cal in cals: 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: deleted[uid] = [cal.canonical_url] else: 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(): 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) for cal in deleted[uid]: print(" - %s" % (cal,), file=sys.stderr) @@ -89,30 +73,40 @@ for uid, arr in changed.items(): for entry in arr: print(" - %s" % (entry[0],), file=sys.stderr) print("", file=sys.stderr) - continue + del deleted[uid] + - if len(set([entry[1] for entry in arr])) != 1: - print("Multiple changed entries with the same UID (%s) detected:" % (uid,), file=sys.stderr) + if len(set([entry[1].data for entry in arr])) != 1: + print("Multiple changed/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) - del deleted[uid] continue entry = arr[0] changed_cals = [ entry[0] for entry in arr ] for cal in cals: if cal.canonical_url not in changed_cals: - cal.save_event(entry[1], no_create=True) - known[uid] = entry[1] + try: + 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 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() - except: - pass + del known[uid] -print(known) +print("Done.") +print() with open("data.yaml", "w") as f: yaml.dump(known, f)