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@1215 | 8 | Copyright (C) 2014, 2015, 2016, 2017 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@1140 | 24 | from os.path import abspath, 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@1140 | 32 | parent = abspath(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@1215 | 37 | from imiptools.config import settings |
paul@748 | 38 | from imiptools.client import Client |
paul@1234 | 39 | from imiptools.data import get_window_end |
paul@654 | 40 | from imiptools.dates import get_default_timezone, to_utc_datetime |
paul@1230 | 41 | from imiptools.freebusy import FreeBusyCollection, FreeBusyGroupCollection, \ |
paul@1230 | 42 | FreeBusyGroupPeriod |
paul@1243 | 43 | from imiptools.period import Period |
paul@1088 | 44 | from imiptools.stores import get_store, get_publisher, get_journal |
paul@120 | 45 | |
paul@1196 | 46 | def make_freebusy(client, participants, storage, store_and_publish, |
paul@1196 | 47 | include_needs_action, reset_updated_list, verbose): |
paul@377 | 48 | |
paul@670 | 49 | """ |
paul@1013 | 50 | Using the given 'client' representing a user, make free/busy details for the |
paul@1196 | 51 | records of the user, generating details for 'participants' if not indicated |
paul@1013 | 52 | as None; otherwise, generating free/busy details concerning the given user. |
paul@599 | 53 | |
paul@1196 | 54 | The 'storage' is the specific store or journal object used to access data. |
paul@1196 | 55 | |
paul@670 | 56 | If 'store_and_publish' is set, the stored details will be updated; |
paul@670 | 57 | otherwise, the details will be written to standard output. |
paul@120 | 58 | |
paul@670 | 59 | If 'include_needs_action' is set, details of objects whose participation |
paul@670 | 60 | status is set to "NEEDS-ACTION" for the participant will be included in the |
paul@670 | 61 | details. |
paul@670 | 62 | |
paul@670 | 63 | If 'reset_updated_list' is set, all objects will be inspected for periods; |
paul@670 | 64 | otherwise, only those in the stored free/busy providers file will be |
paul@670 | 65 | inspected. |
paul@670 | 66 | |
paul@670 | 67 | If 'verbose' is set, messages will be written to standard error. |
paul@599 | 68 | """ |
paul@670 | 69 | |
paul@1013 | 70 | user = client.user |
paul@1196 | 71 | journal = client.get_journal() |
paul@1013 | 72 | publisher = client.get_publisher() |
paul@1013 | 73 | preferences = client.get_preferences() |
paul@1243 | 74 | tzid = client.get_tzid() |
paul@120 | 75 | |
paul@1243 | 76 | # Get the start and end of the window. Note that the start is normally the |
paul@1243 | 77 | # current moment in time, but for testing we may choose a specific point in |
paul@1243 | 78 | # time instead. |
paul@1243 | 79 | |
paul@1243 | 80 | window_start = client.get_window_start() |
paul@1243 | 81 | window_end = client.get_window_end() |
paul@367 | 82 | |
paul@1196 | 83 | providers = [] |
paul@1196 | 84 | |
paul@1196 | 85 | # Iterate over participants, with None being a special null participant |
paul@1196 | 86 | # value. |
paul@1196 | 87 | |
paul@1196 | 88 | for participant in participants or [None]: |
paul@1196 | 89 | |
paul@1196 | 90 | # Get identifiers for uncancelled events either from a list of events |
paul@1196 | 91 | # providing free/busy periods at the end of the given time window, or from |
paul@1196 | 92 | # a list of all events. |
paul@652 | 93 | |
paul@1196 | 94 | all_events = not reset_updated_list and storage.get_freebusy_providers(user, window_end) |
paul@349 | 95 | |
paul@1196 | 96 | if not all_events: |
paul@1196 | 97 | all_events = storage.get_all_events(user) |
paul@1196 | 98 | if storage is journal: |
paul@1196 | 99 | fb = FreeBusyGroupCollection() |
paul@1196 | 100 | else: |
paul@1196 | 101 | fb = FreeBusyCollection() |
paul@1196 | 102 | |
paul@1196 | 103 | # With providers of additional periods, append to the existing collection. |
paul@652 | 104 | |
paul@1196 | 105 | else: |
paul@1196 | 106 | if participants is None: |
paul@1196 | 107 | fb = storage.get_freebusy_for_update(user) |
paul@1196 | 108 | else: |
paul@1196 | 109 | fb = storage.get_freebusy_for_other_for_update(user, participant) |
paul@1196 | 110 | |
paul@1243 | 111 | # Remove periods before the window start. |
paul@1243 | 112 | |
paul@1243 | 113 | fb.remove_periods_before(Period(window_start, None)) |
paul@1243 | 114 | |
paul@1196 | 115 | # Obtain event objects. |
paul@652 | 116 | |
paul@1196 | 117 | objs = [] |
paul@1196 | 118 | for uid, recurrenceid in all_events: |
paul@1196 | 119 | if verbose: |
paul@1196 | 120 | print >>sys.stderr, uid, recurrenceid |
paul@1196 | 121 | event = storage.get_event(user, uid, recurrenceid) |
paul@1196 | 122 | if event: |
paul@1232 | 123 | objs.append(event) |
paul@120 | 124 | |
paul@1196 | 125 | # Build a free/busy collection for the given user. |
paul@1196 | 126 | |
paul@1196 | 127 | for obj in objs: |
paul@1196 | 128 | recurrenceids = not obj.get_recurrenceid() and storage.get_recurrences(user, obj.get_uid()) |
paul@1196 | 129 | |
paul@1196 | 130 | # Obtain genuine attendees. |
paul@367 | 131 | |
paul@1196 | 132 | if storage is journal: |
paul@1196 | 133 | attendees = storage.get_delegates(user) |
paul@1196 | 134 | else: |
paul@1196 | 135 | attendees = [participant] |
paul@1196 | 136 | |
paul@1196 | 137 | # Generate records for each attendee (applicable to consolidated |
paul@1196 | 138 | # journal data). |
paul@367 | 139 | |
paul@1196 | 140 | for attendee in attendees: |
paul@1196 | 141 | partstat = obj.get_participation_status(attendee) |
paul@1196 | 142 | |
paul@1196 | 143 | # Only include objects where the attendee actually participates. |
paul@120 | 144 | |
paul@1196 | 145 | if obj.get_participation(partstat, include_needs_action): |
paul@1196 | 146 | |
paul@1196 | 147 | # Add each active period to the collection. |
paul@1196 | 148 | |
paul@1333 | 149 | for p in obj.get_active_periods( |
paul@1243 | 150 | start=window_start, end=window_end): |
paul@1196 | 151 | |
paul@1196 | 152 | # Obtain a suitable period object. |
paul@367 | 153 | |
paul@1196 | 154 | fbp = obj.get_freebusy_period(p, partstat == "ORG") |
paul@1196 | 155 | |
paul@1196 | 156 | if storage is journal: |
paul@1196 | 157 | fbp = FreeBusyGroupPeriod(*fbp.as_tuple(), attendee=attendee) |
paul@120 | 158 | |
paul@1196 | 159 | fb.insert_period(fbp) |
paul@1196 | 160 | |
paul@1196 | 161 | # Store and publish the free/busy collection. |
paul@1196 | 162 | |
paul@1196 | 163 | if store_and_publish: |
paul@367 | 164 | |
paul@1196 | 165 | # Set the user's own free/busy information. |
paul@1196 | 166 | |
paul@1196 | 167 | if participant is None: |
paul@1196 | 168 | storage.set_freebusy(user, fb) |
paul@748 | 169 | |
paul@1196 | 170 | if client.is_sharing() and client.is_publishing(): |
paul@1196 | 171 | publisher.set_freebusy(user, fb) |
paul@1196 | 172 | |
paul@1196 | 173 | # Set free/busy information concerning another user. |
paul@1196 | 174 | |
paul@1196 | 175 | else: |
paul@1196 | 176 | storage.set_freebusy_for_other(user, fb, participant) |
paul@670 | 177 | |
paul@670 | 178 | # Update the list of objects providing periods on future occasions. |
paul@670 | 179 | |
paul@1196 | 180 | if participant is None or storage is journal: |
paul@1276 | 181 | providers += [obj for obj in objs if obj.possibly_active_from(window_end)] |
paul@1196 | 182 | |
paul@1196 | 183 | # Alternatively, just write the collection to standard output. |
paul@670 | 184 | |
paul@1196 | 185 | else: |
paul@1196 | 186 | f = getwriter("utf-8")(sys.stdout) |
paul@1196 | 187 | for item in fb: |
paul@1196 | 188 | print >>f, "\t".join(item.as_tuple(strings_only=True)) |
paul@670 | 189 | |
paul@1196 | 190 | # Update free/busy providers if storing. |
paul@1196 | 191 | |
paul@1196 | 192 | if store_and_publish: |
paul@1196 | 193 | storage.set_freebusy_providers(user, to_utc_datetime(window_end, tzid), providers) |
paul@120 | 194 | |
paul@670 | 195 | # Main program. |
paul@670 | 196 | |
paul@670 | 197 | if __name__ == "__main__": |
paul@670 | 198 | |
paul@670 | 199 | # Interpret the command line arguments. |
paul@670 | 200 | |
paul@672 | 201 | participants = [] |
paul@672 | 202 | args = [] |
paul@1088 | 203 | store_type = [] |
paul@672 | 204 | store_dir = [] |
paul@672 | 205 | publishing_dir = [] |
paul@1060 | 206 | journal_dir = [] |
paul@672 | 207 | preferences_dir = [] |
paul@672 | 208 | ignored = [] |
paul@672 | 209 | |
paul@672 | 210 | # Collect user details first, switching to other arguments when encountering |
paul@672 | 211 | # switches. |
paul@672 | 212 | |
paul@672 | 213 | l = participants |
paul@672 | 214 | |
paul@672 | 215 | for arg in sys.argv[1:]: |
paul@1196 | 216 | if arg in ("-n", "-s", "-v", "-r", "-q"): |
paul@672 | 217 | args.append(arg) |
paul@672 | 218 | l = ignored |
paul@1088 | 219 | elif arg == "-T": |
paul@1088 | 220 | l = store_type |
paul@672 | 221 | elif arg == "-S": |
paul@672 | 222 | l = store_dir |
paul@672 | 223 | elif arg == "-P": |
paul@672 | 224 | l = publishing_dir |
paul@1060 | 225 | elif arg == "-j": |
paul@1060 | 226 | l = journal_dir |
paul@672 | 227 | elif arg == "-p": |
paul@672 | 228 | l = preferences_dir |
paul@672 | 229 | else: |
paul@672 | 230 | l.append(arg) |
paul@672 | 231 | |
paul@670 | 232 | try: |
paul@672 | 233 | user = participants[0] |
paul@670 | 234 | except IndexError: |
paul@670 | 235 | print >>sys.stderr, """\ |
paul@1196 | 236 | Usage: %s <user> [ <other user> ... ] [ <options> ] |
paul@815 | 237 | |
paul@1196 | 238 | Need a user and optional participants (if different from the user), |
paul@670 | 239 | along with the -s option if updating the store and the published details. |
paul@1140 | 240 | |
paul@1140 | 241 | Specific options: |
paul@1140 | 242 | |
paul@1196 | 243 | -q Access quotas in the journal instead of users in the store |
paul@1140 | 244 | -s Update the store and published details (write details to standard output |
paul@1140 | 245 | otherwise) |
paul@1140 | 246 | -n Include objects with PARTSTAT of NEEDS-ACTION |
paul@1140 | 247 | -r Inspect all objects, not just those expected to provide details |
paul@1140 | 248 | -v Show additional messages on standard error |
paul@1013 | 249 | |
paul@1013 | 250 | General options: |
paul@1013 | 251 | |
paul@1088 | 252 | -j Indicates the journal directory location |
paul@1088 | 253 | -p Indicates the preferences directory location |
paul@1088 | 254 | -P Indicates the publishing directory location |
paul@1088 | 255 | -S Indicates the store directory location |
paul@1088 | 256 | -T Indicates the store type (the configured value if omitted) |
paul@815 | 257 | """ % split(sys.argv[0])[1] |
paul@670 | 258 | sys.exit(1) |
paul@670 | 259 | |
paul@672 | 260 | # Define any other participant of interest plus options. |
paul@672 | 261 | |
paul@1196 | 262 | participants = participants[1:] |
paul@1196 | 263 | using_journal = "-q" in args |
paul@672 | 264 | store_and_publish = "-s" in args |
paul@672 | 265 | include_needs_action = "-n" in args |
paul@672 | 266 | reset_updated_list = "-r" in args |
paul@672 | 267 | verbose = "-v" in args |
paul@672 | 268 | |
paul@672 | 269 | # Override defaults if indicated. |
paul@672 | 270 | |
paul@1088 | 271 | getvalue = lambda value, default=None: value and value[0] or default |
paul@1088 | 272 | |
paul@1215 | 273 | store_type = getvalue(store_type, settings["STORE_TYPE"]) |
paul@1088 | 274 | store_dir = getvalue(store_dir) |
paul@1088 | 275 | publishing_dir = getvalue(publishing_dir) |
paul@1088 | 276 | journal_dir = getvalue(journal_dir) |
paul@1088 | 277 | preferences_dir = getvalue(preferences_dir) |
paul@672 | 278 | |
paul@1140 | 279 | # Obtain store-related objects or delegate this to the Client initialiser. |
paul@672 | 280 | |
paul@1338 | 281 | store = store_dir and get_store(store_type, store_dir) or None |
paul@1338 | 282 | publisher = publishing_dir and get_publisher(publishing_dir) or None |
paul@1338 | 283 | journal = journal_dir and get_journal(store_type, journal_dir) or None |
paul@672 | 284 | |
paul@1196 | 285 | # Determine which kind of object will be accessed. |
paul@1196 | 286 | |
paul@1196 | 287 | if using_journal: |
paul@1196 | 288 | storage = journal |
paul@1196 | 289 | else: |
paul@1196 | 290 | storage = store |
paul@1196 | 291 | |
paul@672 | 292 | # Obtain a list of users for processing. |
paul@672 | 293 | |
paul@670 | 294 | if user in ("*", "all"): |
paul@1196 | 295 | users = storage.get_users() |
paul@670 | 296 | else: |
paul@670 | 297 | users = [user] |
paul@670 | 298 | |
paul@1196 | 299 | # Obtain a list of participants for processing. |
paul@1196 | 300 | |
paul@1196 | 301 | if participants and participants[0] in ("*", "all"): |
paul@1196 | 302 | participants = storage.get_freebusy_others(user) |
paul@1196 | 303 | |
paul@1196 | 304 | # Provide a participants list to iterate over even if no specific |
paul@1196 | 305 | # participant is involved. This updates a user's own records, but only for |
paul@1196 | 306 | # the general data store. |
paul@1196 | 307 | |
paul@1196 | 308 | elif not participants: |
paul@1196 | 309 | if not using_journal: |
paul@1196 | 310 | participants = None |
paul@1196 | 311 | else: |
paul@1196 | 312 | print >>sys.stderr, "Participants must be indicated when updating quota records." |
paul@1196 | 313 | sys.exit(1) |
paul@1196 | 314 | |
paul@672 | 315 | # Process the given users. |
paul@672 | 316 | |
paul@670 | 317 | for user in users: |
paul@670 | 318 | if verbose: |
paul@670 | 319 | print >>sys.stderr, user |
paul@1013 | 320 | make_freebusy( |
paul@1196 | 321 | Client(user, None, store, publisher, journal, preferences_dir), |
paul@1196 | 322 | participants, storage, store_and_publish, include_needs_action, |
paul@1196 | 323 | reset_updated_list, verbose) |
paul@670 | 324 | |
paul@120 | 325 | # vim: tabstop=4 expandtab shiftwidth=4 |