# HG changeset patch # User Paul Boddie # Date 1437844412 -7200 # Node ID 068646dcc51f064eccd01b0f807cc9dd8fad9ae0 # Parent af91a0b74a87c470d1ebc974f3be7a1208bcdc11 Added docstrings. diff -r af91a0b74a87 -r 068646dcc51f tools/make_freebusy.py --- a/tools/make_freebusy.py Sat Jul 25 18:19:33 2015 +0200 +++ b/tools/make_freebusy.py Sat Jul 25 19:13:32 2015 +0200 @@ -1,5 +1,11 @@ #!/usr/bin/env python +""" +Construct free/busy records for a user, either recording that user's own +availability schedule or the schedule of another user (using details provided +when scheduling events with that user). +""" + from imiptools.data import get_window_end, Object from imiptools.dates import format_datetime, get_default_timezone, to_recurrence_start from imiptools.period import FreeBusyPeriod, is_replaced @@ -9,7 +15,10 @@ def get_periods(fb, obj, tzid, window_end, only_organiser, recurrenceids): - # Update free/busy details with the actual periods associated with the event. + """ + Update free/busy details 'fb' with the actual periods associated with the + event 'obj'. + """ recurrenceid = obj.get_recurrenceid() recurrenceids = [to_recurrence_start(r) for r in recurrenceids] @@ -29,99 +38,103 @@ # Main program. -try: - user = sys.argv[1] - args = sys.argv[2:] - participant = args and args[0] not in ("-n", "-s", "-v") and args[0] or user - store_and_publish = "-s" in args - include_needs_action = "-n" in args - verbose = "-v" in args -except IndexError: - print >>sys.stderr, """\ -Need a user and an optional participant (if different from the user), -along with the -s option if updating the store and the published details. -""" - sys.exit(1) +if __name__ == "__main__": + + # Interpret the command line arguments. -preferences = Preferences(user) -tzid = preferences.get("TZID") or get_default_timezone() + try: + user = sys.argv[1] + args = sys.argv[2:] + participant = args and args[0] not in ("-n", "-s", "-v") and args[0] or user + store_and_publish = "-s" in args + include_needs_action = "-n" in args + verbose = "-v" in args + except IndexError: + print >>sys.stderr, """\ + Need a user and an optional participant (if different from the user), + along with the -s option if updating the store and the published details. + """ + sys.exit(1) -# Get the size of the free/busy window. + preferences = Preferences(user) + tzid = preferences.get("TZID") or get_default_timezone() + + # Get the size of the free/busy window. -try: - window_size = int(preferences.get("window_size")) -except (TypeError, ValueError): - window_size = 100 -window_end = get_window_end(tzid, window_size) + try: + window_size = int(preferences.get("window_size")) + except (TypeError, ValueError): + window_size = 100 + window_end = get_window_end(tzid, window_size) -store = FileStore() -publisher = FilePublisher() + store = FileStore() + publisher = FilePublisher() -# Get all identifiers for events. + # Get all identifiers for events. -uids = store.get_events(user) + uids = store.get_events(user) -all_events = set() -for uid in uids: - all_events.add((uid, None)) - all_events.update([(uid, recurrenceid) for recurrenceid in store.get_recurrences(user, uid)]) + all_events = set() + for uid in uids: + all_events.add((uid, None)) + all_events.update([(uid, recurrenceid) for recurrenceid in store.get_recurrences(user, uid)]) -# Filter out cancelled events. + # Filter out cancelled events. -cancelled = store.get_cancellations(user) or [] -all_events.difference_update(cancelled) + cancelled = store.get_cancellations(user) or [] + all_events.difference_update(cancelled) -# Obtain event objects. + # Obtain event objects. -objs = [] -for uid, recurrenceid in all_events: - if verbose: - print >>sys.stderr, uid, recurrenceid - event = store.get_event(user, uid, recurrenceid) - if event: - objs.append(Object(event)) + objs = [] + for uid, recurrenceid in all_events: + if verbose: + print >>sys.stderr, uid, recurrenceid + event = store.get_event(user, uid, recurrenceid) + if event: + objs.append(Object(event)) -# Build a free/busy collection for the given user. + # Build a free/busy collection for the given user. -fb = [] -for obj in objs: - attendees = obj.get_value_map("ATTENDEE") - organiser = obj.get_value("ORGANIZER") - recurrenceids = store.get_recurrences(user, obj.get_value("UID")) + fb = [] + for obj in objs: + attendees = obj.get_value_map("ATTENDEE") + organiser = obj.get_value("ORGANIZER") + recurrenceids = store.get_recurrences(user, obj.get_value("UID")) - for attendee, attendee_attr in attendees.items(): + for attendee, attendee_attr in attendees.items(): - # Only consider events where the stated participant actually attends. + # Only consider events where the stated participant actually attends. - if attendee == participant: - partstat = attendee_attr.get("PARTSTAT", "NEEDS-ACTION") + if attendee == participant: + partstat = attendee_attr.get("PARTSTAT", "NEEDS-ACTION") - if partstat not in ("DECLINED", "DELEGATED", "NEEDS-ACTION") or \ - include_needs_action and partstat == "NEEDS-ACTION": + if partstat not in ("DECLINED", "DELEGATED", "NEEDS-ACTION") or \ + include_needs_action and partstat == "NEEDS-ACTION": - get_periods(fb, obj, tzid, window_end, False, recurrenceids) + get_periods(fb, obj, tzid, window_end, False, recurrenceids) - break + break - # Where not attending, retain the affected periods and mark them as - # organising periods. + # Where not attending, retain the affected periods and mark them as + # organising periods. - else: - if organiser == participant: - get_periods(fb, obj, tzid, window_end, True, recurrenceids) + else: + if organiser == participant: + get_periods(fb, obj, tzid, window_end, True, recurrenceids) -fb.sort() + fb.sort() -# Store and publish the free/busy collection. + # Store and publish the free/busy collection. -if store_and_publish: - if user == participant: - store.set_freebusy(user, fb) - publisher.set_freebusy(user, fb) + if store_and_publish: + if user == participant: + store.set_freebusy(user, fb) + publisher.set_freebusy(user, fb) + else: + store.set_freebusy_for_other(user, fb, participant) else: - store.set_freebusy_for_other(user, fb, participant) -else: - for item in fb: - print "\t".join(item.as_tuple(strings_only=True)) + for item in fb: + print "\t".join(item.as_tuple(strings_only=True)) # vim: tabstop=4 expandtab shiftwidth=4