library/calendar_sync.py aktualisiert

This commit is contained in:
2025-07-12 07:20:35 +00:00
parent 76e5b191df
commit 03520d595f

View File

@ -23,13 +23,11 @@ def connect_caldav(url, user=None, password=None, verify_ssl=True):
password=password, password=password,
ssl_verify_cert=verify_ssl ssl_verify_cert=verify_ssl
) )
# Kalender-URL in String umwandeln
target_path = urlparse(url).path target_path = urlparse(url).path
principal = client.principal() principal = client.principal()
# Alle Kalender durchsuchen und URL als String vergleichen
for calendar in principal.calendars(): for calendar in principal.calendars():
if target_path in str(calendar.url): # WICHTIG: URL in String konvertieren if target_path in str(calendar.url):
return calendar return calendar
raise Exception(f"Kalender mit Pfad '{target_path}' nicht gefunden") raise Exception(f"Kalender mit Pfad '{target_path}' nicht gefunden")
except Exception as e: except Exception as e:
@ -50,7 +48,7 @@ def sync_ics_to_caldav(module):
) )
existing_events = { existing_events = {
event.icalendar_component.uid.value: event str(event.icalendar_component.uid): event
for event in target_cal.events() for event in target_cal.events()
} }
@ -58,7 +56,7 @@ def sync_ics_to_caldav(module):
results = {'added': [], 'updated': [], 'removed': []} results = {'added': [], 'updated': [], 'removed': []}
for vevent in ics_events: for vevent in ics_events:
uid = str(vevent.uid.value) uid = str(vevent.uid)
ical_data = vevent.serialize() ical_data = vevent.serialize()
if uid not in existing_events: if uid not in existing_events:
@ -72,7 +70,8 @@ def sync_ics_to_caldav(module):
changed = True changed = True
if module.params['purge']: if module.params['purge']:
for uid in set(existing_events.keys()) - {str(e.uid.value) for e in ics_events}: current_uids = {str(e.uid) for e in ics_events}
for uid in set(existing_events.keys()) - current_uids:
existing_events[uid].delete() existing_events[uid].delete()
results['removed'].append(uid) results['removed'].append(uid)
changed = True changed = True
@ -92,7 +91,7 @@ def run_module():
) )
module = AnsibleModule(argument_spec=module_args, supports_check_mode=False) module = AnsibleModule(argument_spec=module_args, supports_check_mode=False)
try: try:
changed, results = sync_ics_to_caldav(module) changed, results = sync_ics_to_caldav(module)
module.exit_json(changed=changed, **results) module.exit_json(changed=changed, **results)
@ -100,4 +99,4 @@ def run_module():
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
if __name__ == '__main__': if __name__ == '__main__':
run_module() run_module()