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@652 | 24 | from bisect import bisect_left |
paul@649 | 25 | from codecs import getwriter |
paul@367 | 26 | from imiptools.data import get_window_end, Object |
paul@654 | 27 | from imiptools.dates import get_default_timezone, to_utc_datetime |
paul@654 | 28 | from imiptools.period import insert_period |
paul@291 | 29 | from imiptools.profile import Preferences |
paul@120 | 30 | from imip_store import FileStore, FilePublisher |
paul@120 | 31 | import sys |
paul@120 | 32 | |
paul@377 | 33 | # Main program. |
paul@377 | 34 | |
paul@599 | 35 | if __name__ == "__main__": |
paul@599 | 36 | |
paul@599 | 37 | # Interpret the command line arguments. |
paul@120 | 38 | |
paul@599 | 39 | try: |
paul@599 | 40 | user = sys.argv[1] |
paul@599 | 41 | args = sys.argv[2:] |
paul@654 | 42 | participant = args and args[0] not in ("-n", "-s", "-v", "-r") and args[0] or user |
paul@599 | 43 | store_and_publish = "-s" in args |
paul@599 | 44 | include_needs_action = "-n" in args |
paul@599 | 45 | verbose = "-v" in args |
paul@654 | 46 | reset_updated_list = "-r" in args |
paul@599 | 47 | except IndexError: |
paul@599 | 48 | print >>sys.stderr, """\ |
paul@599 | 49 | Need a user and an optional participant (if different from the user), |
paul@599 | 50 | along with the -s option if updating the store and the published details. |
paul@599 | 51 | """ |
paul@599 | 52 | sys.exit(1) |
paul@291 | 53 | |
paul@599 | 54 | preferences = Preferences(user) |
paul@599 | 55 | tzid = preferences.get("TZID") or get_default_timezone() |
paul@599 | 56 | |
paul@599 | 57 | # Get the size of the free/busy window. |
paul@120 | 58 | |
paul@599 | 59 | try: |
paul@599 | 60 | window_size = int(preferences.get("window_size")) |
paul@599 | 61 | except (TypeError, ValueError): |
paul@599 | 62 | window_size = 100 |
paul@599 | 63 | window_end = get_window_end(tzid, window_size) |
paul@367 | 64 | |
paul@599 | 65 | store = FileStore() |
paul@599 | 66 | publisher = FilePublisher() |
paul@367 | 67 | |
paul@652 | 68 | # Get identifiers for uncancelled events either from a list of events |
paul@652 | 69 | # providing free/busy periods at the end of the given time window, or from |
paul@652 | 70 | # a list of all events. |
paul@652 | 71 | |
paul@654 | 72 | all_events = not reset_updated_list and store.get_freebusy_providers(user, window_end) |
paul@349 | 73 | |
paul@652 | 74 | if not all_events: |
paul@652 | 75 | all_events = store.get_active_events(user) |
paul@652 | 76 | fb = [] |
paul@652 | 77 | |
paul@652 | 78 | # With providers of additional periods, append to the existing collection. |
paul@652 | 79 | |
paul@652 | 80 | else: |
paul@652 | 81 | if user == participant: |
paul@652 | 82 | fb = store.get_freebusy(user) |
paul@652 | 83 | else: |
paul@652 | 84 | fb = store.get_freebusy_for_other(user, participant) |
paul@120 | 85 | |
paul@599 | 86 | # Obtain event objects. |
paul@367 | 87 | |
paul@599 | 88 | objs = [] |
paul@599 | 89 | for uid, recurrenceid in all_events: |
paul@599 | 90 | if verbose: |
paul@599 | 91 | print >>sys.stderr, uid, recurrenceid |
paul@599 | 92 | event = store.get_event(user, uid, recurrenceid) |
paul@599 | 93 | if event: |
paul@599 | 94 | objs.append(Object(event)) |
paul@367 | 95 | |
paul@599 | 96 | # Build a free/busy collection for the given user. |
paul@120 | 97 | |
paul@599 | 98 | for obj in objs: |
paul@648 | 99 | partstat = obj.get_participation_status(participant) |
paul@648 | 100 | recurrenceids = not obj.get_recurrenceid() and store.get_recurrences(user, obj.get_uid()) |
paul@367 | 101 | |
paul@648 | 102 | if obj.get_participation(partstat, include_needs_action): |
paul@648 | 103 | for p in obj.get_active_periods(recurrenceids, tzid, window_end): |
paul@652 | 104 | fbp = obj.get_freebusy_period(p, partstat == "ORG") |
paul@654 | 105 | insert_period(fb, fbp) |
paul@120 | 106 | |
paul@599 | 107 | # Store and publish the free/busy collection. |
paul@367 | 108 | |
paul@599 | 109 | if store_and_publish: |
paul@599 | 110 | if user == participant: |
paul@599 | 111 | store.set_freebusy(user, fb) |
paul@599 | 112 | publisher.set_freebusy(user, fb) |
paul@654 | 113 | store.set_freebusy_providers(user, to_utc_datetime(window_end, tzid), [obj for obj in objs if obj.possibly_active_from(window_end, tzid)]) |
paul@599 | 114 | else: |
paul@599 | 115 | store.set_freebusy_for_other(user, fb, participant) |
paul@395 | 116 | else: |
paul@649 | 117 | f = getwriter("utf-8")(sys.stdout) |
paul@599 | 118 | for item in fb: |
paul@649 | 119 | print >>f, "\t".join(item.as_tuple(strings_only=True)) |
paul@120 | 120 | |
paul@120 | 121 | # vim: tabstop=4 expandtab shiftwidth=4 |