paul@120 | 1 | #!/usr/bin/env python |
paul@120 | 2 | |
paul@599 | 3 | """ |
paul@599 | 4 | Construct free/busy records for a user, either recording that user's own |
paul@599 | 5 | availability schedule or the schedule of another user (using details provided |
paul@599 | 6 | when scheduling events with that user). |
paul@600 | 7 | |
paul@600 | 8 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@600 | 9 | |
paul@600 | 10 | This program is free software; you can redistribute it and/or modify it under |
paul@600 | 11 | the terms of the GNU General Public License as published by the Free Software |
paul@600 | 12 | Foundation; either version 3 of the License, or (at your option) any later |
paul@600 | 13 | version. |
paul@600 | 14 | |
paul@600 | 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@600 | 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@600 | 17 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@600 | 18 | details. |
paul@600 | 19 | |
paul@600 | 20 | You should have received a copy of the GNU General Public License along with |
paul@600 | 21 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@599 | 22 | """ |
paul@599 | 23 | |
paul@367 | 24 | from imiptools.data import get_window_end, Object |
paul@627 | 25 | from imiptools.dates import get_default_timezone |
paul@563 | 26 | from imiptools.period import FreeBusyPeriod, is_replaced |
paul@291 | 27 | from imiptools.profile import Preferences |
paul@120 | 28 | from imip_store import FileStore, FilePublisher |
paul@120 | 29 | import sys |
paul@120 | 30 | |
paul@395 | 31 | def get_periods(fb, obj, tzid, window_end, only_organiser, recurrenceids): |
paul@377 | 32 | |
paul@599 | 33 | """ |
paul@599 | 34 | Update free/busy details 'fb' with the actual periods associated with the |
paul@599 | 35 | event 'obj'. |
paul@599 | 36 | """ |
paul@377 | 37 | |
paul@563 | 38 | recurrenceid = obj.get_recurrenceid() |
paul@395 | 39 | |
paul@563 | 40 | for p in obj.get_periods(tzid, window_end): |
paul@563 | 41 | if recurrenceid or not is_replaced(p, recurrenceids, tzid): |
paul@481 | 42 | fb.append(FreeBusyPeriod( |
paul@645 | 43 | p.get_start_point(), |
paul@645 | 44 | p.get_end_point(), |
paul@395 | 45 | obj.get_value("UID"), |
paul@395 | 46 | only_organiser and "ORG" or obj.get_value("TRANSP") or "OPAQUE", |
paul@395 | 47 | recurrenceid, |
paul@395 | 48 | obj.get_value("SUMMARY"), |
paul@563 | 49 | obj.get_value("ORGANIZER"), |
paul@563 | 50 | p.get_tzid() |
paul@395 | 51 | )) |
paul@377 | 52 | |
paul@377 | 53 | # Main program. |
paul@377 | 54 | |
paul@599 | 55 | if __name__ == "__main__": |
paul@599 | 56 | |
paul@599 | 57 | # Interpret the command line arguments. |
paul@120 | 58 | |
paul@599 | 59 | try: |
paul@599 | 60 | user = sys.argv[1] |
paul@599 | 61 | args = sys.argv[2:] |
paul@599 | 62 | participant = args and args[0] not in ("-n", "-s", "-v") and args[0] or user |
paul@599 | 63 | store_and_publish = "-s" in args |
paul@599 | 64 | include_needs_action = "-n" in args |
paul@599 | 65 | verbose = "-v" in args |
paul@599 | 66 | except IndexError: |
paul@599 | 67 | print >>sys.stderr, """\ |
paul@599 | 68 | Need a user and an optional participant (if different from the user), |
paul@599 | 69 | along with the -s option if updating the store and the published details. |
paul@599 | 70 | """ |
paul@599 | 71 | sys.exit(1) |
paul@291 | 72 | |
paul@599 | 73 | preferences = Preferences(user) |
paul@599 | 74 | tzid = preferences.get("TZID") or get_default_timezone() |
paul@599 | 75 | |
paul@599 | 76 | # Get the size of the free/busy window. |
paul@120 | 77 | |
paul@599 | 78 | try: |
paul@599 | 79 | window_size = int(preferences.get("window_size")) |
paul@599 | 80 | except (TypeError, ValueError): |
paul@599 | 81 | window_size = 100 |
paul@599 | 82 | window_end = get_window_end(tzid, window_size) |
paul@367 | 83 | |
paul@599 | 84 | store = FileStore() |
paul@599 | 85 | publisher = FilePublisher() |
paul@367 | 86 | |
paul@599 | 87 | # Get all identifiers for events. |
paul@367 | 88 | |
paul@599 | 89 | uids = store.get_events(user) |
paul@349 | 90 | |
paul@599 | 91 | all_events = set() |
paul@599 | 92 | for uid in uids: |
paul@599 | 93 | all_events.add((uid, None)) |
paul@599 | 94 | all_events.update([(uid, recurrenceid) for recurrenceid in store.get_recurrences(user, uid)]) |
paul@367 | 95 | |
paul@599 | 96 | # Filter out cancelled events. |
paul@349 | 97 | |
paul@599 | 98 | cancelled = store.get_cancellations(user) or [] |
paul@599 | 99 | all_events.difference_update(cancelled) |
paul@120 | 100 | |
paul@599 | 101 | # Obtain event objects. |
paul@367 | 102 | |
paul@599 | 103 | objs = [] |
paul@599 | 104 | for uid, recurrenceid in all_events: |
paul@599 | 105 | if verbose: |
paul@599 | 106 | print >>sys.stderr, uid, recurrenceid |
paul@599 | 107 | event = store.get_event(user, uid, recurrenceid) |
paul@599 | 108 | if event: |
paul@599 | 109 | objs.append(Object(event)) |
paul@367 | 110 | |
paul@599 | 111 | # Build a free/busy collection for the given user. |
paul@120 | 112 | |
paul@599 | 113 | fb = [] |
paul@599 | 114 | for obj in objs: |
paul@599 | 115 | attendees = obj.get_value_map("ATTENDEE") |
paul@599 | 116 | organiser = obj.get_value("ORGANIZER") |
paul@599 | 117 | recurrenceids = store.get_recurrences(user, obj.get_value("UID")) |
paul@291 | 118 | |
paul@599 | 119 | for attendee, attendee_attr in attendees.items(): |
paul@367 | 120 | |
paul@599 | 121 | # Only consider events where the stated participant actually attends. |
paul@395 | 122 | |
paul@599 | 123 | if attendee == participant: |
paul@599 | 124 | partstat = attendee_attr.get("PARTSTAT", "NEEDS-ACTION") |
paul@367 | 125 | |
paul@599 | 126 | if partstat not in ("DECLINED", "DELEGATED", "NEEDS-ACTION") or \ |
paul@599 | 127 | include_needs_action and partstat == "NEEDS-ACTION": |
paul@395 | 128 | |
paul@599 | 129 | get_periods(fb, obj, tzid, window_end, False, recurrenceids) |
paul@395 | 130 | |
paul@599 | 131 | break |
paul@367 | 132 | |
paul@599 | 133 | # Where not attending, retain the affected periods and mark them as |
paul@599 | 134 | # organising periods. |
paul@377 | 135 | |
paul@599 | 136 | else: |
paul@599 | 137 | if organiser == participant: |
paul@599 | 138 | get_periods(fb, obj, tzid, window_end, True, recurrenceids) |
paul@120 | 139 | |
paul@599 | 140 | fb.sort() |
paul@120 | 141 | |
paul@599 | 142 | # Store and publish the free/busy collection. |
paul@367 | 143 | |
paul@599 | 144 | if store_and_publish: |
paul@599 | 145 | if user == participant: |
paul@599 | 146 | store.set_freebusy(user, fb) |
paul@599 | 147 | publisher.set_freebusy(user, fb) |
paul@599 | 148 | else: |
paul@599 | 149 | store.set_freebusy_for_other(user, fb, participant) |
paul@395 | 150 | else: |
paul@599 | 151 | for item in fb: |
paul@599 | 152 | print "\t".join(item.as_tuple(strings_only=True)) |
paul@120 | 153 | |
paul@120 | 154 | # vim: tabstop=4 expandtab shiftwidth=4 |