1 #!/usr/bin/env python 2 3 """ 4 Construct free/busy records for a user, either recording that user's own 5 availability schedule or the schedule of another user (using details provided 6 when scheduling events with that user). 7 8 Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> 9 10 This program is free software; you can redistribute it and/or modify it under 11 the terms of the GNU General Public License as published by the Free Software 12 Foundation; either version 3 of the License, or (at your option) any later 13 version. 14 15 This program is distributed in the hope that it will be useful, but WITHOUT 16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 17 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 18 details. 19 20 You should have received a copy of the GNU General Public License along with 21 this program. If not, see <http://www.gnu.org/licenses/>. 22 """ 23 24 from bisect import bisect_left 25 from codecs import getwriter 26 from imiptools.data import get_window_end, Object 27 from imiptools.dates import get_default_timezone 28 from imiptools.profile import Preferences 29 from imip_store import FileStore, FilePublisher 30 import sys 31 32 # Main program. 33 34 if __name__ == "__main__": 35 36 # Interpret the command line arguments. 37 38 try: 39 user = sys.argv[1] 40 args = sys.argv[2:] 41 participant = args and args[0] not in ("-n", "-s", "-v") and args[0] or user 42 store_and_publish = "-s" in args 43 include_needs_action = "-n" in args 44 verbose = "-v" in args 45 except IndexError: 46 print >>sys.stderr, """\ 47 Need a user and an optional participant (if different from the user), 48 along with the -s option if updating the store and the published details. 49 """ 50 sys.exit(1) 51 52 preferences = Preferences(user) 53 tzid = preferences.get("TZID") or get_default_timezone() 54 55 # Get the size of the free/busy window. 56 57 try: 58 window_size = int(preferences.get("window_size")) 59 except (TypeError, ValueError): 60 window_size = 100 61 window_end = get_window_end(tzid, window_size) 62 63 store = FileStore() 64 publisher = FilePublisher() 65 66 # Get identifiers for uncancelled events either from a list of events 67 # providing free/busy periods at the end of the given time window, or from 68 # a list of all events. 69 70 all_events = store.get_freebusy_providers(user, window_end) 71 72 if not all_events: 73 all_events = store.get_active_events(user) 74 fb = [] 75 76 # With providers of additional periods, append to the existing collection. 77 78 else: 79 if user == participant: 80 fb = store.get_freebusy(user) 81 else: 82 fb = store.get_freebusy_for_other(user, participant) 83 84 # Obtain event objects. 85 86 objs = [] 87 for uid, recurrenceid in all_events: 88 if verbose: 89 print >>sys.stderr, uid, recurrenceid 90 event = store.get_event(user, uid, recurrenceid) 91 if event: 92 objs.append(Object(event)) 93 94 # Build a free/busy collection for the given user. 95 96 for obj in objs: 97 partstat = obj.get_participation_status(participant) 98 recurrenceids = not obj.get_recurrenceid() and store.get_recurrences(user, obj.get_uid()) 99 100 if obj.get_participation(partstat, include_needs_action): 101 for p in obj.get_active_periods(recurrenceids, tzid, window_end): 102 fbp = obj.get_freebusy_period(p, partstat == "ORG") 103 i = bisect_left(fb, fbp) 104 if i == len(fb): 105 fb.append(fbp) 106 elif fb[i] != fbp: 107 fb.insert(i, fbp) 108 109 # Store and publish the free/busy collection. 110 111 if store_and_publish: 112 if user == participant: 113 store.set_freebusy(user, fb) 114 publisher.set_freebusy(user, fb) 115 else: 116 store.set_freebusy_for_other(user, fb, participant) 117 else: 118 f = getwriter("utf-8")(sys.stdout) 119 for item in fb: 120 print >>f, "\t".join(item.as_tuple(strings_only=True)) 121 122 # vim: tabstop=4 expandtab shiftwidth=4