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 codecs import getwriter 25 from imiptools.data import get_window_end, Object 26 from imiptools.dates import get_default_timezone, to_utc_datetime 27 from imiptools.period import insert_period 28 from imiptools.profile import Preferences 29 from imip_store import FileStore, FilePublisher 30 import sys 31 32 def make_freebusy(user, participant, store_and_publish, include_needs_action, reset_updated_list, verbose): 33 34 """ 35 Make free/busy details for the records of the given 'user', generating 36 details for 'participant' if not indicated as None; otherwise, generating 37 free/busy details concerning the given user. 38 39 If 'store_and_publish' is set, the stored details will be updated; 40 otherwise, the details will be written to standard output. 41 42 If 'include_needs_action' is set, details of objects whose participation 43 status is set to "NEEDS-ACTION" for the participant will be included in the 44 details. 45 46 If 'reset_updated_list' is set, all objects will be inspected for periods; 47 otherwise, only those in the stored free/busy providers file will be 48 inspected. 49 50 If 'verbose' is set, messages will be written to standard error. 51 """ 52 53 participant = participant or user 54 55 preferences = Preferences(user) 56 tzid = preferences.get("TZID") or get_default_timezone() 57 58 # Get the size of the free/busy window. 59 60 try: 61 window_size = int(preferences.get("window_size")) 62 except (TypeError, ValueError): 63 window_size = 100 64 window_end = get_window_end(tzid, window_size) 65 66 store = FileStore() 67 publisher = FilePublisher() 68 69 # Get identifiers for uncancelled events either from a list of events 70 # providing free/busy periods at the end of the given time window, or from 71 # a list of all events. 72 73 all_events = not reset_updated_list and store.get_freebusy_providers(user, window_end) 74 75 if not all_events: 76 all_events = store.get_active_events(user) 77 fb = [] 78 79 # With providers of additional periods, append to the existing collection. 80 81 else: 82 if user == participant: 83 fb = store.get_freebusy(user) 84 else: 85 fb = store.get_freebusy_for_other(user, participant) 86 87 # Obtain event objects. 88 89 objs = [] 90 for uid, recurrenceid in all_events: 91 if verbose: 92 print >>sys.stderr, uid, recurrenceid 93 event = store.get_event(user, uid, recurrenceid) 94 if event: 95 objs.append(Object(event)) 96 97 # Build a free/busy collection for the given user. 98 99 for obj in objs: 100 partstat = obj.get_participation_status(participant) 101 recurrenceids = not obj.get_recurrenceid() and store.get_recurrences(user, obj.get_uid()) 102 103 if obj.get_participation(partstat, include_needs_action): 104 for p in obj.get_active_periods(recurrenceids, tzid, window_end): 105 fbp = obj.get_freebusy_period(p, partstat == "ORG") 106 insert_period(fb, fbp) 107 108 # Store and publish the free/busy collection. 109 110 if store_and_publish: 111 if user == participant: 112 store.set_freebusy(user, fb) 113 publisher.set_freebusy(user, fb) 114 115 # Update the list of objects providing periods on future occasions. 116 117 store.set_freebusy_providers(user, to_utc_datetime(window_end, tzid), 118 [obj for obj in objs if obj.possibly_active_from(window_end, tzid)]) 119 else: 120 store.set_freebusy_for_other(user, fb, participant) 121 122 # Alternatively, just write the collection to standard output. 123 124 else: 125 f = getwriter("utf-8")(sys.stdout) 126 for item in fb: 127 print >>f, "\t".join(item.as_tuple(strings_only=True)) 128 129 # Main program. 130 131 if __name__ == "__main__": 132 133 # Interpret the command line arguments. 134 135 try: 136 user = sys.argv[1] 137 args = sys.argv[2:] 138 participant = args and args[0] not in ("-n", "-s", "-v", "-r") and args[0] or None 139 store_and_publish = "-s" in args 140 include_needs_action = "-n" in args 141 reset_updated_list = "-r" in args 142 verbose = "-v" in args 143 144 except IndexError: 145 print >>sys.stderr, """\ 146 Need a user and an optional participant (if different from the user), 147 along with the -s option if updating the store and the published details. 148 Specify -n to include objects with PARTSTAT of NEEDS-ACTION. 149 Specify -r to inspect all objects, not just those expected to provide details. 150 Specify -v for additional messages on standard error. 151 """ 152 sys.exit(1) 153 154 if user in ("*", "all"): 155 users = FileStore().get_users() 156 else: 157 users = [user] 158 159 for user in users: 160 if verbose: 161 print >>sys.stderr, user 162 make_freebusy(user, participant, store_and_publish, include_needs_action, reset_updated_list, verbose) 163 164 # vim: tabstop=4 expandtab shiftwidth=4