paul@120 | 1 | #!/usr/bin/env python |
paul@120 | 2 | |
paul@349 | 3 | from imiptools.data import get_freebusy_period, get_datetime_item, get_utc_datetime, get_value, get_value_map |
paul@291 | 4 | from imiptools.dates import format_datetime, get_default_timezone |
paul@291 | 5 | from imiptools.profile import Preferences |
paul@120 | 6 | from imip_store import FileStore, FilePublisher |
paul@120 | 7 | import sys |
paul@120 | 8 | |
paul@120 | 9 | try: |
paul@120 | 10 | user = sys.argv[1] |
paul@120 | 11 | except IndexError: |
paul@120 | 12 | print >>sys.stderr, "Need a user." |
paul@120 | 13 | sys.exit(1) |
paul@120 | 14 | |
paul@291 | 15 | preferences = Preferences(user) |
paul@291 | 16 | tzid = preferences.get("TZID") or get_default_timezone() |
paul@291 | 17 | |
paul@120 | 18 | s = FileStore() |
paul@120 | 19 | p = FilePublisher() |
paul@120 | 20 | |
paul@349 | 21 | events = s.get_events(user) |
paul@349 | 22 | |
paul@349 | 23 | all_events = set() |
paul@349 | 24 | for uid in events: |
paul@349 | 25 | all_events.add((uid, None)) |
paul@349 | 26 | all_events.update([(uid, recurrenceid) for recurrenceid in s.get_recurrences(user, uid)]) |
paul@349 | 27 | |
paul@291 | 28 | cancelled = s.get_cancellations(user) or [] |
paul@349 | 29 | all_events.difference_update(cancelled) |
paul@120 | 30 | |
paul@120 | 31 | objs = [] |
paul@349 | 32 | for uid, recurrenceid in all_events: |
paul@349 | 33 | print >>sys.stderr, uid, recurrenceid |
paul@349 | 34 | objs.append(s.get_event(user, uid, recurrenceid)) |
paul@120 | 35 | |
paul@120 | 36 | fb = [] |
paul@120 | 37 | for obj in objs: |
paul@120 | 38 | if not obj: |
paul@120 | 39 | continue |
paul@120 | 40 | details, details_attr = obj.values()[0] |
paul@263 | 41 | |
paul@291 | 42 | participants = {} |
paul@291 | 43 | participants.update(get_value_map(details, "ATTENDEE")) |
paul@291 | 44 | participants.update(get_value_map(details, "ORGANIZER")) |
paul@291 | 45 | |
paul@291 | 46 | for participant, participant_attr in participants.items(): |
paul@291 | 47 | if participant == user: |
paul@291 | 48 | if participant_attr.get("PARTSTAT") != "DECLINED": |
paul@291 | 49 | dtstart, dtstart_attr = get_datetime_item(details, "DTSTART") |
paul@291 | 50 | dtend, dtend_attr = get_datetime_item(details, "DTEND") |
paul@291 | 51 | event_tzid = dtstart_attr.get("TZID") or dtend_attr.get("TZID") or tzid |
paul@291 | 52 | dtstart, dtend = get_freebusy_period(dtstart, dtend, event_tzid) |
paul@291 | 53 | fb.append((format_datetime(dtstart), |
paul@291 | 54 | format_datetime(dtend), |
paul@263 | 55 | get_value(details, "UID"), |
paul@349 | 56 | get_value(details, "TRANSP") or "OPAQUE", |
paul@349 | 57 | format_datetime(get_utc_datetime(details, "RECURRENCE-ID")) or "", |
paul@349 | 58 | )) |
paul@263 | 59 | break |
paul@120 | 60 | |
paul@120 | 61 | fb.sort() |
paul@120 | 62 | |
paul@120 | 63 | s.set_freebusy(user, fb) |
paul@120 | 64 | p.set_freebusy(user, fb) |
paul@120 | 65 | |
paul@120 | 66 | # vim: tabstop=4 expandtab shiftwidth=4 |