paul@48 | 1 | #!/usr/bin/env python |
paul@48 | 2 | |
paul@146 | 3 | """ |
paul@146 | 4 | Managing and presenting periods of time. |
paul@146 | 5 | |
paul@146 | 6 | Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> |
paul@146 | 7 | |
paul@146 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@146 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@146 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@146 | 11 | version. |
paul@146 | 12 | |
paul@146 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@146 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@146 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@146 | 16 | details. |
paul@146 | 17 | |
paul@146 | 18 | You should have received a copy of the GNU General Public License along with |
paul@146 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@146 | 20 | """ |
paul@146 | 21 | |
paul@48 | 22 | from bisect import bisect_left, insort_left |
paul@279 | 23 | from datetime import datetime, timedelta |
paul@256 | 24 | from imiptools.dates import get_datetime, get_start_of_day, to_timezone |
paul@48 | 25 | |
paul@291 | 26 | # Time management with datetime strings in the UTC time zone. |
paul@48 | 27 | |
paul@221 | 28 | def can_schedule(freebusy, periods, uid): |
paul@221 | 29 | |
paul@221 | 30 | """ |
paul@221 | 31 | Return whether the 'freebusy' list can accommodate the given 'periods' |
paul@221 | 32 | employing the specified 'uid'. |
paul@221 | 33 | """ |
paul@221 | 34 | |
paul@221 | 35 | for conflict in have_conflict(freebusy, periods, True): |
paul@221 | 36 | start, end, found_uid, found_transp = conflict |
paul@221 | 37 | if found_uid != uid: |
paul@221 | 38 | return False |
paul@221 | 39 | |
paul@221 | 40 | return True |
paul@221 | 41 | |
paul@72 | 42 | def have_conflict(freebusy, periods, get_conflicts=False): |
paul@72 | 43 | |
paul@72 | 44 | """ |
paul@72 | 45 | Return whether any period in 'freebusy' overlaps with the given 'periods', |
paul@72 | 46 | returning a collection of such overlapping periods if 'get_conflicts' is |
paul@72 | 47 | set to a true value. |
paul@72 | 48 | """ |
paul@72 | 49 | |
paul@72 | 50 | conflicts = [] |
paul@72 | 51 | for start, end in periods: |
paul@74 | 52 | overlapping = period_overlaps(freebusy, (start, end), get_conflicts) |
paul@74 | 53 | if overlapping: |
paul@72 | 54 | if get_conflicts: |
paul@74 | 55 | conflicts += overlapping |
paul@72 | 56 | else: |
paul@72 | 57 | return True |
paul@74 | 58 | |
paul@72 | 59 | if get_conflicts: |
paul@72 | 60 | return conflicts |
paul@72 | 61 | else: |
paul@72 | 62 | return False |
paul@72 | 63 | |
paul@48 | 64 | def insert_period(freebusy, period): |
paul@48 | 65 | insort_left(freebusy, period) |
paul@48 | 66 | |
paul@48 | 67 | def remove_period(freebusy, uid): |
paul@48 | 68 | i = 0 |
paul@48 | 69 | while i < len(freebusy): |
paul@48 | 70 | t = freebusy[i] |
paul@48 | 71 | if len(t) >= 3 and t[2] == uid: |
paul@48 | 72 | del freebusy[i] |
paul@48 | 73 | else: |
paul@48 | 74 | i += 1 |
paul@48 | 75 | |
paul@74 | 76 | def period_overlaps(freebusy, period, get_periods=False): |
paul@72 | 77 | |
paul@72 | 78 | """ |
paul@74 | 79 | Return whether any period in 'freebusy' overlaps with the given 'period', |
paul@74 | 80 | returning a collection of overlapping periods if 'get_periods' is set to a |
paul@74 | 81 | true value. |
paul@72 | 82 | """ |
paul@72 | 83 | |
paul@48 | 84 | dtstart, dtend = period[:2] |
paul@112 | 85 | found = bisect_left(freebusy, (dtstart, dtend, None, None)) |
paul@74 | 86 | |
paul@74 | 87 | overlapping = [] |
paul@74 | 88 | |
paul@74 | 89 | # Find earlier overlapping periods. |
paul@74 | 90 | |
paul@74 | 91 | i = found |
paul@74 | 92 | |
paul@74 | 93 | while i > 0 and freebusy[i - 1][1] > dtstart: |
paul@74 | 94 | if get_periods: |
paul@74 | 95 | overlapping.insert(0, freebusy[i - 1]) |
paul@74 | 96 | else: |
paul@74 | 97 | return True |
paul@74 | 98 | i -= 1 |
paul@74 | 99 | |
paul@74 | 100 | # Find later overlapping periods. |
paul@74 | 101 | |
paul@74 | 102 | i = found |
paul@74 | 103 | |
paul@74 | 104 | while i < len(freebusy) and (dtend is None or freebusy[i][0] < dtend): |
paul@74 | 105 | if get_periods: |
paul@74 | 106 | overlapping.append(freebusy[i]) |
paul@74 | 107 | else: |
paul@74 | 108 | return True |
paul@74 | 109 | i += 1 |
paul@74 | 110 | |
paul@74 | 111 | if get_periods: |
paul@74 | 112 | return overlapping |
paul@74 | 113 | else: |
paul@74 | 114 | return False |
paul@48 | 115 | |
paul@240 | 116 | # Period layout mostly with datetime objects. |
paul@113 | 117 | |
paul@162 | 118 | def convert_periods(periods, tzid): |
paul@162 | 119 | |
paul@162 | 120 | "Convert 'periods' to use datetime objects employing the given 'tzid'." |
paul@162 | 121 | |
paul@162 | 122 | l = [] |
paul@162 | 123 | |
paul@162 | 124 | for t in periods: |
paul@162 | 125 | start, end = t[:2] |
paul@204 | 126 | |
paul@204 | 127 | # NOTE: This only really works if the datetimes are UTC already. |
paul@232 | 128 | # NOTE: Since the periods should originate from the free/busy data, |
paul@232 | 129 | # NOTE: and since that data should employ UTC times, this should not be |
paul@232 | 130 | # NOTE: an immediate problem. |
paul@204 | 131 | |
paul@232 | 132 | start = get_datetime(start) |
paul@232 | 133 | end = get_datetime(end) |
paul@232 | 134 | |
paul@232 | 135 | start = isinstance(start, datetime) and to_timezone(start, tzid) or get_start_of_day(start, tzid) |
paul@232 | 136 | end = isinstance(end, datetime) and to_timezone(end, tzid) or get_start_of_day(end, tzid) |
paul@232 | 137 | |
paul@162 | 138 | l.append((start, end) + tuple(t[2:])) |
paul@162 | 139 | |
paul@162 | 140 | return l |
paul@162 | 141 | |
paul@162 | 142 | def get_scale(periods): |
paul@113 | 143 | |
paul@113 | 144 | """ |
paul@162 | 145 | Return an ordered time scale from the given list 'periods', with the first |
paul@162 | 146 | two elements of each tuple being start and end times. |
paul@153 | 147 | |
paul@162 | 148 | The given 'tzid' is used to make sure that the times are defined according |
paul@162 | 149 | to the chosen time zone. |
paul@162 | 150 | |
paul@162 | 151 | The returned scale is a mapping from time to (starting, ending) tuples, |
paul@162 | 152 | where starting and ending are collections of tuples from 'periods'. |
paul@113 | 153 | """ |
paul@113 | 154 | |
paul@113 | 155 | scale = {} |
paul@113 | 156 | |
paul@162 | 157 | for t in periods: |
paul@113 | 158 | start, end = t[:2] |
paul@113 | 159 | |
paul@113 | 160 | # Add a point and this event to the starting list. |
paul@113 | 161 | |
paul@113 | 162 | if not scale.has_key(start): |
paul@113 | 163 | scale[start] = [], [] |
paul@113 | 164 | scale[start][0].append(t) |
paul@113 | 165 | |
paul@113 | 166 | # Add a point and this event to the ending list. |
paul@113 | 167 | |
paul@113 | 168 | if not scale.has_key(end): |
paul@113 | 169 | scale[end] = [], [] |
paul@113 | 170 | scale[end][1].append(t) |
paul@113 | 171 | |
paul@113 | 172 | return scale |
paul@113 | 173 | |
paul@162 | 174 | def get_slots(scale): |
paul@113 | 175 | |
paul@113 | 176 | """ |
paul@162 | 177 | Return an ordered list of time slots from the given 'scale'. |
paul@113 | 178 | |
paul@113 | 179 | Each slot is a tuple containing a point in time for the start of the slot, |
paul@113 | 180 | together with a list of parallel event tuples, each tuple containing the |
paul@113 | 181 | original details of an event. |
paul@113 | 182 | """ |
paul@113 | 183 | |
paul@113 | 184 | slots = [] |
paul@113 | 185 | active = [] |
paul@113 | 186 | |
paul@162 | 187 | points = scale.items() |
paul@162 | 188 | points.sort() |
paul@162 | 189 | |
paul@162 | 190 | for point, (starting, ending) in points: |
paul@113 | 191 | |
paul@113 | 192 | # Discard all active events ending at or before this start time. |
paul@161 | 193 | # Free up the position in the active list. |
paul@113 | 194 | |
paul@113 | 195 | for t in ending: |
paul@113 | 196 | i = active.index(t) |
paul@113 | 197 | active[i] = None |
paul@113 | 198 | |
paul@161 | 199 | # For each event starting at the current point, fill any newly-vacated |
paul@161 | 200 | # position or add to the end of the active list. |
paul@161 | 201 | |
paul@113 | 202 | for t in starting: |
paul@113 | 203 | try: |
paul@113 | 204 | i = active.index(None) |
paul@113 | 205 | active[i] = t |
paul@113 | 206 | except ValueError: |
paul@113 | 207 | active.append(t) |
paul@113 | 208 | |
paul@161 | 209 | # Discard vacant positions from the end of the active list. |
paul@161 | 210 | |
paul@113 | 211 | while active and active[-1] is None: |
paul@113 | 212 | active.pop() |
paul@113 | 213 | |
paul@113 | 214 | slots.append((point, active[:])) |
paul@113 | 215 | |
paul@113 | 216 | return slots |
paul@113 | 217 | |
paul@244 | 218 | def add_day_start_points(slots, tzid): |
paul@153 | 219 | |
paul@153 | 220 | """ |
paul@162 | 221 | Introduce into the 'slots' any day start points required by multi-day |
paul@244 | 222 | periods. The 'tzid' is required to make sure that appropriate time zones |
paul@244 | 223 | are chosen and not necessarily those provided by the existing time points. |
paul@153 | 224 | """ |
paul@153 | 225 | |
paul@162 | 226 | new_slots = [] |
paul@153 | 227 | current_date = None |
paul@200 | 228 | previously_active = [] |
paul@153 | 229 | |
paul@153 | 230 | for point, active in slots: |
paul@244 | 231 | start_of_day = get_start_of_day(point, tzid) |
paul@162 | 232 | this_date = point.date() |
paul@153 | 233 | |
paul@198 | 234 | # For each new day, add a slot for the start of the day where periods |
paul@198 | 235 | # are active and where no such slot already exists. |
paul@153 | 236 | |
paul@153 | 237 | if this_date != current_date: |
paul@153 | 238 | current_date = this_date |
paul@153 | 239 | |
paul@153 | 240 | # Add any continuing periods. |
paul@153 | 241 | |
paul@200 | 242 | if point != start_of_day: |
paul@162 | 243 | new_slots.append((start_of_day, previously_active)) |
paul@153 | 244 | |
paul@153 | 245 | # Add the currently active periods at this point in time. |
paul@153 | 246 | |
paul@153 | 247 | previously_active = active |
paul@153 | 248 | |
paul@162 | 249 | for t in new_slots: |
paul@162 | 250 | insort_left(slots, t) |
paul@162 | 251 | |
paul@162 | 252 | def add_slots(slots, points): |
paul@162 | 253 | |
paul@162 | 254 | """ |
paul@162 | 255 | Introduce into the 'slots' entries for those in 'points' that are not |
paul@170 | 256 | already present, propagating active periods from time points preceding |
paul@170 | 257 | those added. |
paul@162 | 258 | """ |
paul@162 | 259 | |
paul@162 | 260 | new_slots = [] |
paul@162 | 261 | |
paul@162 | 262 | for point in points: |
paul@162 | 263 | i = bisect_left(slots, (point, None)) |
paul@162 | 264 | if i < len(slots) and slots[i][0] == point: |
paul@162 | 265 | continue |
paul@162 | 266 | |
paul@170 | 267 | new_slots.append((point, i > 0 and slots[i-1][1] or [])) |
paul@162 | 268 | |
paul@162 | 269 | for t in new_slots: |
paul@162 | 270 | insort_left(slots, t) |
paul@162 | 271 | |
paul@162 | 272 | def partition_by_day(slots): |
paul@162 | 273 | |
paul@162 | 274 | """ |
paul@162 | 275 | Return a mapping from dates to time points provided by 'slots'. |
paul@162 | 276 | """ |
paul@162 | 277 | |
paul@162 | 278 | d = {} |
paul@162 | 279 | |
paul@162 | 280 | for point, value in slots: |
paul@162 | 281 | day = point.date() |
paul@162 | 282 | if not d.has_key(day): |
paul@162 | 283 | d[day] = [] |
paul@162 | 284 | d[day].append((point, value)) |
paul@162 | 285 | |
paul@162 | 286 | return d |
paul@153 | 287 | |
paul@283 | 288 | def add_empty_days(days, tzid): |
paul@279 | 289 | |
paul@279 | 290 | "Add empty days to 'days' between busy days." |
paul@279 | 291 | |
paul@279 | 292 | last_day = None |
paul@279 | 293 | all_days = days.keys() |
paul@279 | 294 | all_days.sort() |
paul@279 | 295 | |
paul@279 | 296 | for day in all_days: |
paul@279 | 297 | if last_day: |
paul@279 | 298 | empty_day = last_day + timedelta(1) |
paul@279 | 299 | while empty_day < day: |
paul@279 | 300 | days[empty_day] = [(get_start_of_day(empty_day, tzid), None)] |
paul@279 | 301 | empty_day += timedelta(1) |
paul@279 | 302 | last_day = day |
paul@279 | 303 | |
paul@114 | 304 | def get_spans(slots): |
paul@114 | 305 | |
paul@114 | 306 | "Inspect the given 'slots', returning a mapping of event uids to spans." |
paul@114 | 307 | |
paul@114 | 308 | points = [point for point, active in slots] |
paul@114 | 309 | spans = {} |
paul@114 | 310 | |
paul@114 | 311 | for point, active in slots: |
paul@114 | 312 | for t in active: |
paul@185 | 313 | if t and len(t) >= 2: |
paul@185 | 314 | start, end, uid, key = get_freebusy_details(t) |
paul@185 | 315 | |
paul@153 | 316 | try: |
paul@153 | 317 | start_slot = points.index(start) |
paul@153 | 318 | except ValueError: |
paul@153 | 319 | start_slot = 0 |
paul@153 | 320 | try: |
paul@153 | 321 | end_slot = points.index(end) |
paul@153 | 322 | except ValueError: |
paul@153 | 323 | end_slot = len(slots) |
paul@185 | 324 | spans[key] = end_slot - start_slot |
paul@114 | 325 | |
paul@114 | 326 | return spans |
paul@114 | 327 | |
paul@185 | 328 | def get_freebusy_details(t): |
paul@185 | 329 | |
paul@185 | 330 | "Return a tuple of the form (start, end, uid, key) from 't'." |
paul@185 | 331 | |
paul@185 | 332 | # Handle both complete free/busy details... |
paul@185 | 333 | |
paul@185 | 334 | if len(t) > 2: |
paul@185 | 335 | start, end, uid = t[:3] |
paul@185 | 336 | key = uid |
paul@185 | 337 | |
paul@185 | 338 | # ...and published details without specific event details. |
paul@185 | 339 | |
paul@185 | 340 | else: |
paul@185 | 341 | start, end = t[:2] |
paul@185 | 342 | uid = None |
paul@185 | 343 | key = (start, end) |
paul@185 | 344 | |
paul@185 | 345 | return start, end, uid, key |
paul@185 | 346 | |
paul@250 | 347 | def remove_from_freebusy(freebusy, attendee, uid, store): |
paul@250 | 348 | |
paul@250 | 349 | """ |
paul@250 | 350 | For the given 'attendee', remove periods from 'freebusy' that are associated |
paul@250 | 351 | with 'uid' in the 'store'. |
paul@250 | 352 | """ |
paul@250 | 353 | |
paul@250 | 354 | remove_period(freebusy, uid) |
paul@250 | 355 | store.set_freebusy(attendee, freebusy) |
paul@250 | 356 | |
paul@250 | 357 | def remove_from_freebusy_for_other(freebusy, user, other, uid, store): |
paul@250 | 358 | |
paul@250 | 359 | """ |
paul@250 | 360 | For the given 'user', remove for the 'other' party periods from 'freebusy' |
paul@250 | 361 | that are associated with 'uid' in the 'store'. |
paul@250 | 362 | """ |
paul@250 | 363 | |
paul@250 | 364 | remove_period(freebusy, uid) |
paul@250 | 365 | store.set_freebusy_for_other(user, freebusy, other) |
paul@250 | 366 | |
paul@250 | 367 | def _update_freebusy(freebusy, periods, transp, uid): |
paul@250 | 368 | |
paul@250 | 369 | """ |
paul@250 | 370 | Update the free/busy details with the given 'periods', 'transp' setting and |
paul@250 | 371 | 'uid'. |
paul@250 | 372 | """ |
paul@250 | 373 | |
paul@250 | 374 | remove_period(freebusy, uid) |
paul@250 | 375 | |
paul@250 | 376 | for start, end in periods: |
paul@250 | 377 | insert_period(freebusy, (start, end, uid, transp)) |
paul@250 | 378 | |
paul@250 | 379 | def update_freebusy(freebusy, attendee, periods, transp, uid, store): |
paul@250 | 380 | |
paul@250 | 381 | """ |
paul@250 | 382 | For the given 'attendee', update the free/busy details with the given |
paul@250 | 383 | 'periods', 'transp' setting and 'uid' in the 'store'. |
paul@250 | 384 | """ |
paul@250 | 385 | |
paul@250 | 386 | _update_freebusy(freebusy, periods, transp, uid) |
paul@250 | 387 | store.set_freebusy(attendee, freebusy) |
paul@250 | 388 | |
paul@250 | 389 | def update_freebusy_for_other(freebusy, user, other, periods, transp, uid, store): |
paul@250 | 390 | |
paul@250 | 391 | """ |
paul@250 | 392 | For the given 'user', update the free/busy details of 'other' with the given |
paul@250 | 393 | 'periods', 'transp' setting and 'uid' in the 'store'. |
paul@250 | 394 | """ |
paul@250 | 395 | |
paul@250 | 396 | _update_freebusy(freebusy, periods, transp, uid) |
paul@250 | 397 | store.set_freebusy_for_other(user, freebusy, other) |
paul@250 | 398 | |
paul@48 | 399 | # vim: tabstop=4 expandtab shiftwidth=4 |