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 27 from imiptools.profile import Preferences 28 from imip_store import FileStore, FilePublisher 29 import sys 30 31 # Main program. 32 33 if __name__ == "__main__": 34 35 # Interpret the command line arguments. 36 37 try: 38 user = sys.argv[1] 39 args = sys.argv[2:] 40 participant = args and args[0] not in ("-n", "-s", "-v") and args[0] or user 41 store_and_publish = "-s" in args 42 include_needs_action = "-n" in args 43 verbose = "-v" in args 44 except IndexError: 45 print >>sys.stderr, """\ 46 Need a user and an optional participant (if different from the user), 47 along with the -s option if updating the store and the published details. 48 """ 49 sys.exit(1) 50 51 preferences = Preferences(user) 52 tzid = preferences.get("TZID") or get_default_timezone() 53 54 # Get the size of the free/busy window. 55 56 try: 57 window_size = int(preferences.get("window_size")) 58 except (TypeError, ValueError): 59 window_size = 100 60 window_end = get_window_end(tzid, window_size) 61 62 store = FileStore() 63 publisher = FilePublisher() 64 65 # Get identifiers for uncancelled events. 66 67 all_events = store.get_active_events(user) 68 69 # Obtain event objects. 70 71 objs = [] 72 for uid, recurrenceid in all_events: 73 if verbose: 74 print >>sys.stderr, uid, recurrenceid 75 event = store.get_event(user, uid, recurrenceid) 76 if event: 77 objs.append(Object(event)) 78 79 # Build a free/busy collection for the given user. 80 81 fb = [] 82 for obj in objs: 83 partstat = obj.get_participation_status(participant) 84 recurrenceids = not obj.get_recurrenceid() and store.get_recurrences(user, obj.get_uid()) 85 86 if obj.get_participation(partstat, include_needs_action): 87 for p in obj.get_active_periods(recurrenceids, tzid, window_end): 88 fb.append(obj.get_freebusy_period(p, partstat == "ORG")) 89 90 fb.sort() 91 92 # Store and publish the free/busy collection. 93 94 if store_and_publish: 95 if user == participant: 96 store.set_freebusy(user, fb) 97 publisher.set_freebusy(user, fb) 98 else: 99 store.set_freebusy_for_other(user, fb, participant) 100 else: 101 f = getwriter("utf-8")(sys.stdout) 102 for item in fb: 103 print >>f, "\t".join(item.as_tuple(strings_only=True)) 104 105 # vim: tabstop=4 expandtab shiftwidth=4