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@1060 | 8 | Copyright (C) 2014, 2015, 2016 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@815 | 24 | from os.path import split |
paul@815 | 25 | import sys |
paul@815 | 26 | |
paul@815 | 27 | # Find the modules. |
paul@815 | 28 | |
paul@815 | 29 | try: |
paul@815 | 30 | import imiptools |
paul@815 | 31 | except ImportError: |
paul@815 | 32 | parent = split(split(__file__)[0])[0] |
paul@815 | 33 | if split(parent)[1] == "imip-agent": |
paul@815 | 34 | sys.path.append(parent) |
paul@815 | 35 | |
paul@649 | 36 | from codecs import getwriter |
paul@1088 | 37 | from imiptools import config |
paul@748 | 38 | from imiptools.client import Client |
paul@367 | 39 | from imiptools.data import get_window_end, Object |
paul@654 | 40 | from imiptools.dates import get_default_timezone, to_utc_datetime |
paul@1062 | 41 | from imiptools.period import FreeBusyCollection |
paul@1088 | 42 | from imiptools.stores import get_store, get_publisher, get_journal |
paul@120 | 43 | |
paul@1013 | 44 | def make_freebusy(client, participant, store_and_publish, include_needs_action, |
paul@1013 | 45 | reset_updated_list, verbose): |
paul@377 | 46 | |
paul@670 | 47 | """ |
paul@1013 | 48 | Using the given 'client' representing a user, make free/busy details for the |
paul@1013 | 49 | records of the user, generating details for 'participant' if not indicated |
paul@1013 | 50 | as None; otherwise, generating free/busy details concerning the given user. |
paul@599 | 51 | |
paul@670 | 52 | If 'store_and_publish' is set, the stored details will be updated; |
paul@670 | 53 | otherwise, the details will be written to standard output. |
paul@120 | 54 | |
paul@670 | 55 | If 'include_needs_action' is set, details of objects whose participation |
paul@670 | 56 | status is set to "NEEDS-ACTION" for the participant will be included in the |
paul@670 | 57 | details. |
paul@670 | 58 | |
paul@670 | 59 | If 'reset_updated_list' is set, all objects will be inspected for periods; |
paul@670 | 60 | otherwise, only those in the stored free/busy providers file will be |
paul@670 | 61 | inspected. |
paul@670 | 62 | |
paul@670 | 63 | If 'verbose' is set, messages will be written to standard error. |
paul@599 | 64 | """ |
paul@670 | 65 | |
paul@1013 | 66 | user = client.user |
paul@1013 | 67 | store = client.get_store() |
paul@1013 | 68 | publisher = client.get_publisher() |
paul@1013 | 69 | preferences = client.get_preferences() |
paul@1013 | 70 | |
paul@670 | 71 | participant = participant or user |
paul@599 | 72 | tzid = preferences.get("TZID") or get_default_timezone() |
paul@599 | 73 | |
paul@599 | 74 | # Get the size of the free/busy window. |
paul@120 | 75 | |
paul@599 | 76 | try: |
paul@599 | 77 | window_size = int(preferences.get("window_size")) |
paul@599 | 78 | except (TypeError, ValueError): |
paul@599 | 79 | window_size = 100 |
paul@599 | 80 | window_end = get_window_end(tzid, window_size) |
paul@367 | 81 | |
paul@652 | 82 | # Get identifiers for uncancelled events either from a list of events |
paul@652 | 83 | # providing free/busy periods at the end of the given time window, or from |
paul@652 | 84 | # a list of all events. |
paul@652 | 85 | |
paul@654 | 86 | all_events = not reset_updated_list and store.get_freebusy_providers(user, window_end) |
paul@349 | 87 | |
paul@652 | 88 | if not all_events: |
paul@694 | 89 | all_events = store.get_all_events(user) |
paul@1062 | 90 | fb = FreeBusyCollection() |
paul@652 | 91 | |
paul@652 | 92 | # With providers of additional periods, append to the existing collection. |
paul@652 | 93 | |
paul@652 | 94 | else: |
paul@652 | 95 | if user == participant: |
paul@652 | 96 | fb = store.get_freebusy(user) |
paul@652 | 97 | else: |
paul@652 | 98 | fb = store.get_freebusy_for_other(user, participant) |
paul@120 | 99 | |
paul@599 | 100 | # Obtain event objects. |
paul@367 | 101 | |
paul@599 | 102 | objs = [] |
paul@599 | 103 | for uid, recurrenceid in all_events: |
paul@599 | 104 | if verbose: |
paul@599 | 105 | print >>sys.stderr, uid, recurrenceid |
paul@599 | 106 | event = store.get_event(user, uid, recurrenceid) |
paul@599 | 107 | if event: |
paul@599 | 108 | objs.append(Object(event)) |
paul@367 | 109 | |
paul@599 | 110 | # Build a free/busy collection for the given user. |
paul@120 | 111 | |
paul@599 | 112 | for obj in objs: |
paul@648 | 113 | partstat = obj.get_participation_status(participant) |
paul@648 | 114 | recurrenceids = not obj.get_recurrenceid() and store.get_recurrences(user, obj.get_uid()) |
paul@367 | 115 | |
paul@648 | 116 | if obj.get_participation(partstat, include_needs_action): |
paul@648 | 117 | for p in obj.get_active_periods(recurrenceids, tzid, window_end): |
paul@652 | 118 | fbp = obj.get_freebusy_period(p, partstat == "ORG") |
paul@1062 | 119 | fb.insert_period(fbp) |
paul@120 | 120 | |
paul@599 | 121 | # Store and publish the free/busy collection. |
paul@367 | 122 | |
paul@599 | 123 | if store_and_publish: |
paul@599 | 124 | if user == participant: |
paul@599 | 125 | store.set_freebusy(user, fb) |
paul@748 | 126 | |
paul@1013 | 127 | if client.is_sharing() and client.is_publishing(): |
paul@748 | 128 | publisher.set_freebusy(user, fb) |
paul@670 | 129 | |
paul@670 | 130 | # Update the list of objects providing periods on future occasions. |
paul@670 | 131 | |
paul@670 | 132 | store.set_freebusy_providers(user, to_utc_datetime(window_end, tzid), |
paul@670 | 133 | [obj for obj in objs if obj.possibly_active_from(window_end, tzid)]) |
paul@599 | 134 | else: |
paul@599 | 135 | store.set_freebusy_for_other(user, fb, participant) |
paul@670 | 136 | |
paul@670 | 137 | # Alternatively, just write the collection to standard output. |
paul@670 | 138 | |
paul@395 | 139 | else: |
paul@649 | 140 | f = getwriter("utf-8")(sys.stdout) |
paul@599 | 141 | for item in fb: |
paul@649 | 142 | print >>f, "\t".join(item.as_tuple(strings_only=True)) |
paul@120 | 143 | |
paul@670 | 144 | # Main program. |
paul@670 | 145 | |
paul@670 | 146 | if __name__ == "__main__": |
paul@670 | 147 | |
paul@670 | 148 | # Interpret the command line arguments. |
paul@670 | 149 | |
paul@672 | 150 | participants = [] |
paul@672 | 151 | args = [] |
paul@1088 | 152 | store_type = [] |
paul@672 | 153 | store_dir = [] |
paul@672 | 154 | publishing_dir = [] |
paul@1060 | 155 | journal_dir = [] |
paul@672 | 156 | preferences_dir = [] |
paul@672 | 157 | ignored = [] |
paul@672 | 158 | |
paul@672 | 159 | # Collect user details first, switching to other arguments when encountering |
paul@672 | 160 | # switches. |
paul@672 | 161 | |
paul@672 | 162 | l = participants |
paul@672 | 163 | |
paul@672 | 164 | for arg in sys.argv[1:]: |
paul@672 | 165 | if arg in ("-n", "-s", "-v", "-r"): |
paul@672 | 166 | args.append(arg) |
paul@672 | 167 | l = ignored |
paul@1088 | 168 | elif arg == "-T": |
paul@1088 | 169 | l = store_type |
paul@672 | 170 | elif arg == "-S": |
paul@672 | 171 | l = store_dir |
paul@672 | 172 | elif arg == "-P": |
paul@672 | 173 | l = publishing_dir |
paul@1060 | 174 | elif arg == "-j": |
paul@1060 | 175 | l = journal_dir |
paul@672 | 176 | elif arg == "-p": |
paul@672 | 177 | l = preferences_dir |
paul@672 | 178 | else: |
paul@672 | 179 | l.append(arg) |
paul@672 | 180 | |
paul@670 | 181 | try: |
paul@672 | 182 | user = participants[0] |
paul@670 | 183 | except IndexError: |
paul@670 | 184 | print >>sys.stderr, """\ |
paul@815 | 185 | Usage: %s <user> [ <other user> ] <options> |
paul@815 | 186 | |
paul@670 | 187 | Need a user and an optional participant (if different from the user), |
paul@670 | 188 | along with the -s option if updating the store and the published details. |
paul@670 | 189 | Specify -n to include objects with PARTSTAT of NEEDS-ACTION. |
paul@670 | 190 | Specify -r to inspect all objects, not just those expected to provide details. |
paul@670 | 191 | Specify -v for additional messages on standard error. |
paul@1013 | 192 | |
paul@1013 | 193 | General options: |
paul@1013 | 194 | |
paul@1088 | 195 | -j Indicates the journal directory location |
paul@1088 | 196 | -p Indicates the preferences directory location |
paul@1088 | 197 | -P Indicates the publishing directory location |
paul@1088 | 198 | -S Indicates the store directory location |
paul@1088 | 199 | -T Indicates the store type (the configured value if omitted) |
paul@815 | 200 | """ % split(sys.argv[0])[1] |
paul@670 | 201 | sys.exit(1) |
paul@670 | 202 | |
paul@672 | 203 | # Define any other participant of interest plus options. |
paul@672 | 204 | |
paul@672 | 205 | participant = participants[1:] and participants[1] or None |
paul@672 | 206 | store_and_publish = "-s" in args |
paul@672 | 207 | include_needs_action = "-n" in args |
paul@672 | 208 | reset_updated_list = "-r" in args |
paul@672 | 209 | verbose = "-v" in args |
paul@672 | 210 | |
paul@672 | 211 | # Override defaults if indicated. |
paul@672 | 212 | |
paul@1088 | 213 | getvalue = lambda value, default=None: value and value[0] or default |
paul@1088 | 214 | |
paul@1088 | 215 | store_type = getvalue(store_type, config.STORE_TYPE) |
paul@1088 | 216 | store_dir = getvalue(store_dir) |
paul@1088 | 217 | publishing_dir = getvalue(publishing_dir) |
paul@1088 | 218 | journal_dir = getvalue(journal_dir) |
paul@1088 | 219 | preferences_dir = getvalue(preferences_dir) |
paul@672 | 220 | |
paul@672 | 221 | # Obtain store-related objects. |
paul@672 | 222 | |
paul@1088 | 223 | store = get_store(store_type, store_dir) |
paul@1088 | 224 | publisher = get_publisher(publishing_dir) |
paul@1088 | 225 | journal = get_journal(store_type, journal_dir) |
paul@672 | 226 | |
paul@672 | 227 | # Obtain a list of users for processing. |
paul@672 | 228 | |
paul@670 | 229 | if user in ("*", "all"): |
paul@672 | 230 | users = store.get_users() |
paul@670 | 231 | else: |
paul@670 | 232 | users = [user] |
paul@670 | 233 | |
paul@672 | 234 | # Process the given users. |
paul@672 | 235 | |
paul@670 | 236 | for user in users: |
paul@670 | 237 | if verbose: |
paul@670 | 238 | print >>sys.stderr, user |
paul@1013 | 239 | make_freebusy( |
paul@1060 | 240 | Client(user, None, store, publisher, journal, preferences_dir), participant, |
paul@672 | 241 | store_and_publish, include_needs_action, reset_updated_list, verbose) |
paul@670 | 242 | |
paul@120 | 243 | # vim: tabstop=4 expandtab shiftwidth=4 |