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(store, publisher, preferences, user, participant, 33 store_and_publish, include_needs_action, reset_updated_list, verbose): 34 35 """ 36 Using the given 'store', 'publisher' and 'preferences', make free/busy 37 details for the records of the given 'user', generating details for 38 'participant' if not indicated as None; otherwise, generating free/busy 39 details concerning the given user. 40 41 If 'store_and_publish' is set, the stored details will be updated; 42 otherwise, the details will be written to standard output. 43 44 If 'include_needs_action' is set, details of objects whose participation 45 status is set to "NEEDS-ACTION" for the participant will be included in the 46 details. 47 48 If 'reset_updated_list' is set, all objects will be inspected for periods; 49 otherwise, only those in the stored free/busy providers file will be 50 inspected. 51 52 If 'verbose' is set, messages will be written to standard error. 53 """ 54 55 participant = participant or 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 # 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 = not reset_updated_list and store.get_freebusy_providers(user, window_end) 71 72 if not all_events: 73 all_events = store.get_all_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 insert_period(fb, fbp) 104 105 # Store and publish the free/busy collection. 106 107 if store_and_publish: 108 if user == participant: 109 store.set_freebusy(user, fb) 110 publisher.set_freebusy(user, fb) 111 112 # Update the list of objects providing periods on future occasions. 113 114 store.set_freebusy_providers(user, to_utc_datetime(window_end, tzid), 115 [obj for obj in objs if obj.possibly_active_from(window_end, tzid)]) 116 else: 117 store.set_freebusy_for_other(user, fb, participant) 118 119 # Alternatively, just write the collection to standard output. 120 121 else: 122 f = getwriter("utf-8")(sys.stdout) 123 for item in fb: 124 print >>f, "\t".join(item.as_tuple(strings_only=True)) 125 126 # Main program. 127 128 if __name__ == "__main__": 129 130 # Interpret the command line arguments. 131 132 participants = [] 133 args = [] 134 store_dir = [] 135 publishing_dir = [] 136 preferences_dir = [] 137 ignored = [] 138 139 # Collect user details first, switching to other arguments when encountering 140 # switches. 141 142 l = participants 143 144 for arg in sys.argv[1:]: 145 if arg in ("-n", "-s", "-v", "-r"): 146 args.append(arg) 147 l = ignored 148 elif arg == "-S": 149 l = store_dir 150 elif arg == "-P": 151 l = publishing_dir 152 elif arg == "-p": 153 l = preferences_dir 154 else: 155 l.append(arg) 156 157 try: 158 user = participants[0] 159 except IndexError: 160 print >>sys.stderr, """\ 161 Need a user and an optional participant (if different from the user), 162 along with the -s option if updating the store and the published details. 163 Specify -n to include objects with PARTSTAT of NEEDS-ACTION. 164 Specify -r to inspect all objects, not just those expected to provide details. 165 Specify -v for additional messages on standard error. 166 """ 167 sys.exit(1) 168 169 # Define any other participant of interest plus options. 170 171 participant = participants[1:] and participants[1] or None 172 store_and_publish = "-s" in args 173 include_needs_action = "-n" in args 174 reset_updated_list = "-r" in args 175 verbose = "-v" in args 176 177 # Override defaults if indicated. 178 179 store_dir = store_dir and store_dir[0] or None 180 publishing_dir = publishing_dir and publishing_dir[0] or None 181 preferences_dir = preferences_dir and preferences_dir[0] or None 182 183 # Obtain store-related objects. 184 185 store = FileStore(store_dir) 186 publisher = FilePublisher(publishing_dir) 187 preferences = Preferences(user, preferences_dir) 188 189 # Obtain a list of users for processing. 190 191 if user in ("*", "all"): 192 users = store.get_users() 193 else: 194 users = [user] 195 196 # Process the given users. 197 198 for user in users: 199 if verbose: 200 print >>sys.stderr, user 201 make_freebusy(store, publisher, preferences, user, participant, 202 store_and_publish, include_needs_action, reset_updated_list, verbose) 203 204 # vim: tabstop=4 expandtab shiftwidth=4