paul@120 | 1 | #!/usr/bin/env python |
paul@120 | 2 | |
paul@367 | 3 | from imiptools.data import get_window_end, Object |
paul@507 | 4 | from imiptools.dates import format_datetime, get_default_timezone, to_recurrence_start |
paul@563 | 5 | from imiptools.period import FreeBusyPeriod, is_replaced |
paul@291 | 6 | from imiptools.profile import Preferences |
paul@120 | 7 | from imip_store import FileStore, FilePublisher |
paul@120 | 8 | import sys |
paul@120 | 9 | |
paul@395 | 10 | def get_periods(fb, obj, tzid, window_end, only_organiser, recurrenceids): |
paul@377 | 11 | |
paul@377 | 12 | # Update free/busy details with the actual periods associated with the event. |
paul@377 | 13 | |
paul@563 | 14 | recurrenceid = obj.get_recurrenceid() |
paul@563 | 15 | recurrenceids = [to_recurrence_start(r) for r in recurrenceids] |
paul@395 | 16 | |
paul@563 | 17 | for p in obj.get_periods(tzid, window_end): |
paul@563 | 18 | if recurrenceid or not is_replaced(p, recurrenceids, tzid): |
paul@481 | 19 | fb.append(FreeBusyPeriod( |
paul@563 | 20 | p.get_start(), |
paul@563 | 21 | p.get_end(), |
paul@395 | 22 | obj.get_value("UID"), |
paul@395 | 23 | only_organiser and "ORG" or obj.get_value("TRANSP") or "OPAQUE", |
paul@395 | 24 | recurrenceid, |
paul@395 | 25 | obj.get_value("SUMMARY"), |
paul@563 | 26 | obj.get_value("ORGANIZER"), |
paul@563 | 27 | p.get_tzid() |
paul@395 | 28 | )) |
paul@377 | 29 | |
paul@377 | 30 | # Main program. |
paul@377 | 31 | |
paul@120 | 32 | try: |
paul@120 | 33 | user = sys.argv[1] |
paul@395 | 34 | args = sys.argv[2:] |
paul@481 | 35 | participant = args and args[0] not in ("-n", "-s", "-v") and args[0] or user |
paul@395 | 36 | store_and_publish = "-s" in args |
paul@395 | 37 | include_needs_action = "-n" in args |
paul@481 | 38 | verbose = "-v" in args |
paul@120 | 39 | except IndexError: |
paul@395 | 40 | print >>sys.stderr, """\ |
paul@395 | 41 | Need a user and an optional participant (if different from the user), |
paul@395 | 42 | along with the -s option if updating the store and the published details. |
paul@395 | 43 | """ |
paul@120 | 44 | sys.exit(1) |
paul@120 | 45 | |
paul@291 | 46 | preferences = Preferences(user) |
paul@291 | 47 | tzid = preferences.get("TZID") or get_default_timezone() |
paul@291 | 48 | |
paul@367 | 49 | # Get the size of the free/busy window. |
paul@120 | 50 | |
paul@367 | 51 | try: |
paul@367 | 52 | window_size = int(preferences.get("window_size")) |
paul@367 | 53 | except (TypeError, ValueError): |
paul@367 | 54 | window_size = 100 |
paul@367 | 55 | window_end = get_window_end(tzid, window_size) |
paul@367 | 56 | |
paul@367 | 57 | store = FileStore() |
paul@367 | 58 | publisher = FilePublisher() |
paul@367 | 59 | |
paul@367 | 60 | # Get all identifiers for events. |
paul@367 | 61 | |
paul@367 | 62 | uids = store.get_events(user) |
paul@349 | 63 | |
paul@349 | 64 | all_events = set() |
paul@367 | 65 | for uid in uids: |
paul@349 | 66 | all_events.add((uid, None)) |
paul@367 | 67 | all_events.update([(uid, recurrenceid) for recurrenceid in store.get_recurrences(user, uid)]) |
paul@367 | 68 | |
paul@367 | 69 | # Filter out cancelled events. |
paul@349 | 70 | |
paul@367 | 71 | cancelled = store.get_cancellations(user) or [] |
paul@349 | 72 | all_events.difference_update(cancelled) |
paul@120 | 73 | |
paul@367 | 74 | # Obtain event objects. |
paul@367 | 75 | |
paul@120 | 76 | objs = [] |
paul@349 | 77 | for uid, recurrenceid in all_events: |
paul@481 | 78 | if verbose: |
paul@481 | 79 | print >>sys.stderr, uid, recurrenceid |
paul@367 | 80 | event = store.get_event(user, uid, recurrenceid) |
paul@367 | 81 | if event: |
paul@367 | 82 | objs.append(Object(event)) |
paul@367 | 83 | |
paul@367 | 84 | # Build a free/busy collection for the given user. |
paul@120 | 85 | |
paul@120 | 86 | fb = [] |
paul@120 | 87 | for obj in objs: |
paul@377 | 88 | attendees = obj.get_value_map("ATTENDEE") |
paul@377 | 89 | organiser = obj.get_value("ORGANIZER") |
paul@395 | 90 | recurrenceids = store.get_recurrences(user, obj.get_value("UID")) |
paul@291 | 91 | |
paul@377 | 92 | for attendee, attendee_attr in attendees.items(): |
paul@367 | 93 | |
paul@395 | 94 | # Only consider events where the stated participant actually attends. |
paul@395 | 95 | |
paul@395 | 96 | if attendee == participant: |
paul@395 | 97 | partstat = attendee_attr.get("PARTSTAT", "NEEDS-ACTION") |
paul@367 | 98 | |
paul@395 | 99 | if partstat not in ("DECLINED", "DELEGATED", "NEEDS-ACTION") or \ |
paul@395 | 100 | include_needs_action and partstat == "NEEDS-ACTION": |
paul@395 | 101 | |
paul@395 | 102 | get_periods(fb, obj, tzid, window_end, False, recurrenceids) |
paul@395 | 103 | |
paul@377 | 104 | break |
paul@367 | 105 | |
paul@377 | 106 | # Where not attending, retain the affected periods and mark them as |
paul@377 | 107 | # organising periods. |
paul@377 | 108 | |
paul@377 | 109 | else: |
paul@395 | 110 | if organiser == participant: |
paul@395 | 111 | get_periods(fb, obj, tzid, window_end, True, recurrenceids) |
paul@120 | 112 | |
paul@120 | 113 | fb.sort() |
paul@120 | 114 | |
paul@367 | 115 | # Store and publish the free/busy collection. |
paul@367 | 116 | |
paul@367 | 117 | if store_and_publish: |
paul@395 | 118 | if user == participant: |
paul@395 | 119 | store.set_freebusy(user, fb) |
paul@395 | 120 | publisher.set_freebusy(user, fb) |
paul@395 | 121 | else: |
paul@395 | 122 | store.set_freebusy_for_other(user, fb, participant) |
paul@367 | 123 | else: |
paul@367 | 124 | for item in fb: |
paul@563 | 125 | print "\t".join(item.as_tuple(strings_only=True)) |
paul@120 | 126 | |
paul@120 | 127 | # vim: tabstop=4 expandtab shiftwidth=4 |