paul@120 | 1 | #!/usr/bin/env python |
paul@120 | 2 | |
paul@367 | 3 | from imiptools.data import get_window_end, 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@367 | 11 | store_and_publish = "-s" in sys.argv[2:] |
paul@120 | 12 | except IndexError: |
paul@367 | 13 | print >>sys.stderr, "Need a user, along with the -s option if updating the store." |
paul@120 | 14 | sys.exit(1) |
paul@120 | 15 | |
paul@291 | 16 | preferences = Preferences(user) |
paul@291 | 17 | tzid = preferences.get("TZID") or get_default_timezone() |
paul@291 | 18 | |
paul@367 | 19 | # Get the size of the free/busy window. |
paul@120 | 20 | |
paul@367 | 21 | try: |
paul@367 | 22 | window_size = int(preferences.get("window_size")) |
paul@367 | 23 | except (TypeError, ValueError): |
paul@367 | 24 | window_size = 100 |
paul@367 | 25 | window_end = get_window_end(tzid, window_size) |
paul@367 | 26 | |
paul@367 | 27 | store = FileStore() |
paul@367 | 28 | publisher = FilePublisher() |
paul@367 | 29 | |
paul@367 | 30 | # Get all identifiers for events. |
paul@367 | 31 | |
paul@367 | 32 | uids = store.get_events(user) |
paul@349 | 33 | |
paul@349 | 34 | all_events = set() |
paul@367 | 35 | for uid in uids: |
paul@349 | 36 | all_events.add((uid, None)) |
paul@367 | 37 | all_events.update([(uid, recurrenceid) for recurrenceid in store.get_recurrences(user, uid)]) |
paul@367 | 38 | |
paul@367 | 39 | # Filter out cancelled events. |
paul@349 | 40 | |
paul@367 | 41 | cancelled = store.get_cancellations(user) or [] |
paul@349 | 42 | all_events.difference_update(cancelled) |
paul@120 | 43 | |
paul@367 | 44 | # Obtain event objects. |
paul@367 | 45 | |
paul@120 | 46 | objs = [] |
paul@349 | 47 | for uid, recurrenceid in all_events: |
paul@349 | 48 | print >>sys.stderr, uid, recurrenceid |
paul@367 | 49 | event = store.get_event(user, uid, recurrenceid) |
paul@367 | 50 | if event: |
paul@367 | 51 | objs.append(Object(event)) |
paul@367 | 52 | |
paul@367 | 53 | # Build a free/busy collection for the given user. |
paul@120 | 54 | |
paul@120 | 55 | fb = [] |
paul@120 | 56 | for obj in objs: |
paul@291 | 57 | participants = {} |
paul@367 | 58 | participants.update(obj.get_value_map("ATTENDEE")) |
paul@367 | 59 | participants.update(obj.get_value_map("ORGANIZER")) |
paul@291 | 60 | |
paul@291 | 61 | for participant, participant_attr in participants.items(): |
paul@367 | 62 | |
paul@367 | 63 | # Only consider events where this user actually participates. |
paul@367 | 64 | |
paul@291 | 65 | if participant == user: |
paul@368 | 66 | if participant_attr.get("PARTSTAT", "NEEDS-ACTION") not in ("DECLINED", "DELEGATED", "NEEDS-ACTION"): |
paul@367 | 67 | |
paul@367 | 68 | # Obtain the actual periods associated with the event. |
paul@367 | 69 | |
paul@367 | 70 | for start, end in obj.get_periods_for_freebusy(tzid, window_end): |
paul@367 | 71 | fb.append((start, end, |
paul@367 | 72 | obj.get_value("UID"), |
paul@367 | 73 | obj.get_value("TRANSP") or "OPAQUE", |
paul@367 | 74 | format_datetime(obj.get_utc_datetime("RECURRENCE-ID")) or "", |
paul@349 | 75 | )) |
paul@263 | 76 | break |
paul@120 | 77 | |
paul@120 | 78 | fb.sort() |
paul@120 | 79 | |
paul@367 | 80 | # Store and publish the free/busy collection. |
paul@367 | 81 | |
paul@367 | 82 | if store_and_publish: |
paul@367 | 83 | store.set_freebusy(user, fb) |
paul@367 | 84 | publisher.set_freebusy(user, fb) |
paul@367 | 85 | else: |
paul@367 | 86 | for item in fb: |
paul@367 | 87 | print "\t".join(item) |
paul@120 | 88 | |
paul@120 | 89 | # vim: tabstop=4 expandtab shiftwidth=4 |