paul@120 | 1 | #!/usr/bin/env python |
paul@120 | 2 | |
paul@291 | 3 | from imiptools.data import get_freebusy_period, get_datetime_item, get_value, get_value_map, parse_object |
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@263 | 21 | events = set(s.get_events(user)) |
paul@291 | 22 | cancelled = s.get_cancellations(user) or [] |
paul@263 | 23 | |
paul@263 | 24 | events.difference_update(cancelled) |
paul@120 | 25 | |
paul@120 | 26 | objs = [] |
paul@263 | 27 | for i in events: |
paul@120 | 28 | print >>sys.stderr, i |
paul@120 | 29 | objs.append(parse_object(s.get_event(user, i), "utf-8")) |
paul@120 | 30 | |
paul@120 | 31 | fb = [] |
paul@120 | 32 | for obj in objs: |
paul@120 | 33 | if not obj: |
paul@120 | 34 | continue |
paul@120 | 35 | details, details_attr = obj.values()[0] |
paul@263 | 36 | |
paul@291 | 37 | participants = {} |
paul@291 | 38 | participants.update(get_value_map(details, "ATTENDEE")) |
paul@291 | 39 | participants.update(get_value_map(details, "ORGANIZER")) |
paul@291 | 40 | |
paul@291 | 41 | for participant, participant_attr in participants.items(): |
paul@291 | 42 | if participant == user: |
paul@291 | 43 | if participant_attr.get("PARTSTAT") != "DECLINED": |
paul@291 | 44 | dtstart, dtstart_attr = get_datetime_item(details, "DTSTART") |
paul@291 | 45 | dtend, dtend_attr = get_datetime_item(details, "DTEND") |
paul@291 | 46 | event_tzid = dtstart_attr.get("TZID") or dtend_attr.get("TZID") or tzid |
paul@291 | 47 | dtstart, dtend = get_freebusy_period(dtstart, dtend, event_tzid) |
paul@291 | 48 | fb.append((format_datetime(dtstart), |
paul@291 | 49 | format_datetime(dtend), |
paul@263 | 50 | get_value(details, "UID"), |
paul@263 | 51 | get_value(details, "TRANSP"))) |
paul@263 | 52 | break |
paul@120 | 53 | |
paul@120 | 54 | fb.sort() |
paul@120 | 55 | |
paul@120 | 56 | s.set_freebusy(user, fb) |
paul@120 | 57 | p.set_freebusy(user, fb) |
paul@120 | 58 | |
paul@120 | 59 | # vim: tabstop=4 expandtab shiftwidth=4 |