imip-agent

tools/make_freebusy.py

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