From fe0ddaf533406b98323fae9a14fc1dae91156e51 Mon Sep 17 00:00:00 2001 From: iamdoubz <> Date: Thu, 5 Nov 2026 16:08:31 -0600 Subject: [PATCH] Add RFC 5545 export with a self-test --- parentcalendar/ics.py | 82 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 parentcalendar/ics.py diff --git a/parentcalendar/ics.py b/parentcalendar/ics.py new file mode 100644 index 0000000..9d6d433 --- /dev/null +++ b/parentcalendar/ics.py @@ -0,0 +1,82 @@ +"""RFC 5545 export. icalendar handles CRLF line endings, 75-octet folding and escaping.""" +from datetime import date, datetime, time, timedelta, timezone +from zoneinfo import ZoneInfo + +from icalendar import Alarm, Calendar, Event + +PRODID = "-//ParentCalendar//ParentCalendar//EN" + + +def utc(stamp): + """SQLite's 'YYYY-MM-DD HH:MM:SS' (UTC) as an aware datetime.""" + return datetime.fromisoformat(stamp).replace(tzinfo=timezone.utc) + + +def build(name, events, tz_name, alarm_minutes=None): + """events: dicts from the event table (status confirmed or deleted). Returns .ics bytes.""" + cal = Calendar() + cal.add("prodid", PRODID) + cal.add("version", "2.0") + cal.add("calscale", "GREGORIAN") + cal.add("x-wr-calname", name) + cal.add("x-wr-timezone", tz_name) + zone = ZoneInfo(tz_name) + now = datetime.now(timezone.utc).replace(microsecond=0) + for e in events: + ev = Event() + ev.add("uid", e["uid"]) + ev.add("dtstamp", now) + ev.add("summary", e["title"]) + start = date.fromisoformat(e["date"]) + end = date.fromisoformat(e["end_date"]) if e.get("end_date") else start + if e["all_day"] or not e.get("start_time"): + ev.add("dtstart", start) + ev.add("dtend", end + timedelta(days=1)) # DTEND is exclusive for all-day events + else: + ev.add("dtstart", datetime.combine(start, time.fromisoformat(e["start_time"]), zone)) + if e.get("end_time"): # no end time: no DTEND, a zero-length event (DTEND may not equal DTSTART) + ev.add("dtend", datetime.combine(end, time.fromisoformat(e["end_time"]), zone)) + ev.add("sequence", e.get("sequence") or 0) + ev.add("last-modified", utc(e["updated_at"])) + ev.add("status", "CANCELLED" if e["status"] == "deleted" else "CONFIRMED") + ev.add("categories", [e["kind"]]) + if e.get("notes"): + ev.add("description", e["notes"]) + if e.get("location"): + ev.add("location", e["location"]) + if alarm_minutes and e["status"] != "deleted": + alarm = Alarm() + alarm.add("action", "DISPLAY") + alarm.add("description", e["title"]) + alarm.add("trigger", timedelta(minutes=-alarm_minutes)) + ev.add_component(alarm) + cal.add_component(ev) + cal.add_missing_timezones() + return cal.to_ical() + + +if __name__ == "__main__": + def ev(**kw): + return {"uid": "a@x", "title": "No School, Fall Break", "date": "2026-10-30", "end_date": None, + "all_day": True, "start_time": None, "end_time": None, "kind": "no_school", "status": "confirmed", + "sequence": 0, "updated_at": "2026-11-05 12:00:00", "notes": None, "location": None, **kw} + out = build("Kids", [ + ev(), + ev(uid="b@x", title="Winter Break", date="2026-12-21", end_date="2027-01-01"), + ev(uid="c@x", title="Trunk or Treat", all_day=False, start_time="17:00", end_time="18:30", date="2026-10-29"), + ev(uid="d@x", title="PTO", all_day=False, start_time="16:00", date="2026-10-20", location="Library"), + ev(uid="e@x", title="Spirit Day", status="deleted", sequence=2), + ], "America/Chicago", alarm_minutes=60).decode() + assert "\r\n" in out and "VERSION:2.0" in out and "X-WR-TIMEZONE:America/Chicago" in out + assert "DTSTART;VALUE=DATE:20261030" in out and "DTEND;VALUE=DATE:20261031" in out # exclusive end + assert "DTSTART;VALUE=DATE:20261221" in out and "DTEND;VALUE=DATE:20270102" in out # break runs through Jan 1 + assert "DTSTART;TZID=America/Chicago:20261029T170000" in out and "DTEND;TZID=America/Chicago:20261029T183000" in out + assert "BEGIN:VTIMEZONE" in out and "TZID:America/Chicago" in out + block = lambda uid: next(b for b in out.split("BEGIN:VEVENT")[1:] if f"UID:{uid}" in b) + pto = block("d@x") + assert "DTSTART;TZID=America/Chicago:20261020T160000" in pto and "DTEND" not in pto # zero-length + assert "SUMMARY:No School\\, Fall Break" in out # commas escaped + spirit = block("e@x") + assert "STATUS:CANCELLED" in spirit and "SEQUENCE:2" in spirit and "VALARM" not in spirit + assert out.count("BEGIN:VALARM") == 4 and "TRIGGER:-PT1H" in out + print("ics selftest ok")