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