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@250 | 23 | from datetime import datetime, timedelta |
paul@250 | 24 | from imiptools.dates import format_datetime, get_datetime, get_start_of_day, \ |
paul@250 | 25 | to_timezone |
paul@250 | 26 | from vRecurrence import get_parameters, get_rule |
paul@48 | 27 | |
paul@240 | 28 | # Time management with datetime strings. |
paul@48 | 29 | |
paul@221 | 30 | def can_schedule(freebusy, periods, uid): |
paul@221 | 31 | |
paul@221 | 32 | """ |
paul@221 | 33 | Return whether the 'freebusy' list can accommodate the given 'periods' |
paul@221 | 34 | employing the specified 'uid'. |
paul@221 | 35 | """ |
paul@221 | 36 | |
paul@221 | 37 | for conflict in have_conflict(freebusy, periods, True): |
paul@221 | 38 | start, end, found_uid, found_transp = conflict |
paul@221 | 39 | if found_uid != uid: |
paul@221 | 40 | return False |
paul@221 | 41 | |
paul@221 | 42 | return True |
paul@221 | 43 | |
paul@72 | 44 | def have_conflict(freebusy, periods, get_conflicts=False): |
paul@72 | 45 | |
paul@72 | 46 | """ |
paul@72 | 47 | Return whether any period in 'freebusy' overlaps with the given 'periods', |
paul@72 | 48 | returning a collection of such overlapping periods if 'get_conflicts' is |
paul@72 | 49 | set to a true value. |
paul@72 | 50 | """ |
paul@72 | 51 | |
paul@72 | 52 | conflicts = [] |
paul@72 | 53 | for start, end in periods: |
paul@74 | 54 | overlapping = period_overlaps(freebusy, (start, end), get_conflicts) |
paul@74 | 55 | if overlapping: |
paul@72 | 56 | if get_conflicts: |
paul@74 | 57 | conflicts += overlapping |
paul@72 | 58 | else: |
paul@72 | 59 | return True |
paul@74 | 60 | |
paul@72 | 61 | if get_conflicts: |
paul@72 | 62 | return conflicts |
paul@72 | 63 | else: |
paul@72 | 64 | return False |
paul@72 | 65 | |
paul@48 | 66 | def insert_period(freebusy, period): |
paul@48 | 67 | insort_left(freebusy, period) |
paul@48 | 68 | |
paul@48 | 69 | def remove_period(freebusy, uid): |
paul@48 | 70 | i = 0 |
paul@48 | 71 | while i < len(freebusy): |
paul@48 | 72 | t = freebusy[i] |
paul@48 | 73 | if len(t) >= 3 and t[2] == uid: |
paul@48 | 74 | del freebusy[i] |
paul@48 | 75 | else: |
paul@48 | 76 | i += 1 |
paul@48 | 77 | |
paul@74 | 78 | def period_overlaps(freebusy, period, get_periods=False): |
paul@72 | 79 | |
paul@72 | 80 | """ |
paul@74 | 81 | Return whether any period in 'freebusy' overlaps with the given 'period', |
paul@74 | 82 | returning a collection of overlapping periods if 'get_periods' is set to a |
paul@74 | 83 | true value. |
paul@72 | 84 | """ |
paul@72 | 85 | |
paul@48 | 86 | dtstart, dtend = period[:2] |
paul@112 | 87 | found = bisect_left(freebusy, (dtstart, dtend, None, None)) |
paul@74 | 88 | |
paul@74 | 89 | overlapping = [] |
paul@74 | 90 | |
paul@74 | 91 | # Find earlier overlapping periods. |
paul@74 | 92 | |
paul@74 | 93 | i = found |
paul@74 | 94 | |
paul@74 | 95 | while i > 0 and freebusy[i - 1][1] > dtstart: |
paul@74 | 96 | if get_periods: |
paul@74 | 97 | overlapping.insert(0, freebusy[i - 1]) |
paul@74 | 98 | else: |
paul@74 | 99 | return True |
paul@74 | 100 | i -= 1 |
paul@74 | 101 | |
paul@74 | 102 | # Find later overlapping periods. |
paul@74 | 103 | |
paul@74 | 104 | i = found |
paul@74 | 105 | |
paul@74 | 106 | while i < len(freebusy) and (dtend is None or freebusy[i][0] < dtend): |
paul@74 | 107 | if get_periods: |
paul@74 | 108 | overlapping.append(freebusy[i]) |
paul@74 | 109 | else: |
paul@74 | 110 | return True |
paul@74 | 111 | i += 1 |
paul@74 | 112 | |
paul@74 | 113 | if get_periods: |
paul@74 | 114 | return overlapping |
paul@74 | 115 | else: |
paul@74 | 116 | return False |
paul@48 | 117 | |
paul@240 | 118 | # Period layout mostly with datetime objects. |
paul@113 | 119 | |
paul@162 | 120 | def convert_periods(periods, tzid): |
paul@162 | 121 | |
paul@162 | 122 | "Convert 'periods' to use datetime objects employing the given 'tzid'." |
paul@162 | 123 | |
paul@162 | 124 | l = [] |
paul@162 | 125 | |
paul@162 | 126 | for t in periods: |
paul@162 | 127 | start, end = t[:2] |
paul@204 | 128 | |
paul@204 | 129 | # NOTE: This only really works if the datetimes are UTC already. |
paul@232 | 130 | # NOTE: Since the periods should originate from the free/busy data, |
paul@232 | 131 | # NOTE: and since that data should employ UTC times, this should not be |
paul@232 | 132 | # NOTE: an immediate problem. |
paul@204 | 133 | |
paul@232 | 134 | start = get_datetime(start) |
paul@232 | 135 | end = get_datetime(end) |
paul@232 | 136 | |
paul@232 | 137 | start = isinstance(start, datetime) and to_timezone(start, tzid) or get_start_of_day(start, tzid) |
paul@232 | 138 | end = isinstance(end, datetime) and to_timezone(end, tzid) or get_start_of_day(end, tzid) |
paul@232 | 139 | |
paul@162 | 140 | l.append((start, end) + tuple(t[2:])) |
paul@162 | 141 | |
paul@162 | 142 | return l |
paul@162 | 143 | |
paul@162 | 144 | def get_scale(periods): |
paul@113 | 145 | |
paul@113 | 146 | """ |
paul@162 | 147 | Return an ordered time scale from the given list 'periods', with the first |
paul@162 | 148 | two elements of each tuple being start and end times. |
paul@153 | 149 | |
paul@162 | 150 | The given 'tzid' is used to make sure that the times are defined according |
paul@162 | 151 | to the chosen time zone. |
paul@162 | 152 | |
paul@162 | 153 | The returned scale is a mapping from time to (starting, ending) tuples, |
paul@162 | 154 | where starting and ending are collections of tuples from 'periods'. |
paul@113 | 155 | """ |
paul@113 | 156 | |
paul@113 | 157 | scale = {} |
paul@113 | 158 | |
paul@162 | 159 | for t in periods: |
paul@113 | 160 | start, end = t[:2] |
paul@113 | 161 | |
paul@113 | 162 | # Add a point and this event to the starting list. |
paul@113 | 163 | |
paul@113 | 164 | if not scale.has_key(start): |
paul@113 | 165 | scale[start] = [], [] |
paul@113 | 166 | scale[start][0].append(t) |
paul@113 | 167 | |
paul@113 | 168 | # Add a point and this event to the ending list. |
paul@113 | 169 | |
paul@113 | 170 | if not scale.has_key(end): |
paul@113 | 171 | scale[end] = [], [] |
paul@113 | 172 | scale[end][1].append(t) |
paul@113 | 173 | |
paul@113 | 174 | return scale |
paul@113 | 175 | |
paul@162 | 176 | def get_slots(scale): |
paul@113 | 177 | |
paul@113 | 178 | """ |
paul@162 | 179 | Return an ordered list of time slots from the given 'scale'. |
paul@113 | 180 | |
paul@113 | 181 | Each slot is a tuple containing a point in time for the start of the slot, |
paul@113 | 182 | together with a list of parallel event tuples, each tuple containing the |
paul@113 | 183 | original details of an event. |
paul@113 | 184 | """ |
paul@113 | 185 | |
paul@113 | 186 | slots = [] |
paul@113 | 187 | active = [] |
paul@113 | 188 | |
paul@162 | 189 | points = scale.items() |
paul@162 | 190 | points.sort() |
paul@162 | 191 | |
paul@162 | 192 | for point, (starting, ending) in points: |
paul@113 | 193 | |
paul@113 | 194 | # Discard all active events ending at or before this start time. |
paul@161 | 195 | # Free up the position in the active list. |
paul@113 | 196 | |
paul@113 | 197 | for t in ending: |
paul@113 | 198 | i = active.index(t) |
paul@113 | 199 | active[i] = None |
paul@113 | 200 | |
paul@161 | 201 | # For each event starting at the current point, fill any newly-vacated |
paul@161 | 202 | # position or add to the end of the active list. |
paul@161 | 203 | |
paul@113 | 204 | for t in starting: |
paul@113 | 205 | try: |
paul@113 | 206 | i = active.index(None) |
paul@113 | 207 | active[i] = t |
paul@113 | 208 | except ValueError: |
paul@113 | 209 | active.append(t) |
paul@113 | 210 | |
paul@161 | 211 | # Discard vacant positions from the end of the active list. |
paul@161 | 212 | |
paul@113 | 213 | while active and active[-1] is None: |
paul@113 | 214 | active.pop() |
paul@113 | 215 | |
paul@113 | 216 | slots.append((point, active[:])) |
paul@113 | 217 | |
paul@113 | 218 | return slots |
paul@113 | 219 | |
paul@244 | 220 | def add_day_start_points(slots, tzid): |
paul@153 | 221 | |
paul@153 | 222 | """ |
paul@162 | 223 | Introduce into the 'slots' any day start points required by multi-day |
paul@244 | 224 | periods. The 'tzid' is required to make sure that appropriate time zones |
paul@244 | 225 | are chosen and not necessarily those provided by the existing time points. |
paul@153 | 226 | """ |
paul@153 | 227 | |
paul@162 | 228 | new_slots = [] |
paul@153 | 229 | current_date = None |
paul@200 | 230 | previously_active = [] |
paul@153 | 231 | |
paul@153 | 232 | for point, active in slots: |
paul@244 | 233 | start_of_day = get_start_of_day(point, tzid) |
paul@162 | 234 | this_date = point.date() |
paul@153 | 235 | |
paul@198 | 236 | # For each new day, add a slot for the start of the day where periods |
paul@198 | 237 | # are active and where no such slot already exists. |
paul@153 | 238 | |
paul@153 | 239 | if this_date != current_date: |
paul@153 | 240 | current_date = this_date |
paul@153 | 241 | |
paul@153 | 242 | # Add any continuing periods. |
paul@153 | 243 | |
paul@200 | 244 | if point != start_of_day: |
paul@162 | 245 | new_slots.append((start_of_day, previously_active)) |
paul@153 | 246 | |
paul@153 | 247 | # Add the currently active periods at this point in time. |
paul@153 | 248 | |
paul@153 | 249 | previously_active = active |
paul@153 | 250 | |
paul@162 | 251 | for t in new_slots: |
paul@162 | 252 | insort_left(slots, t) |
paul@162 | 253 | |
paul@162 | 254 | def add_slots(slots, points): |
paul@162 | 255 | |
paul@162 | 256 | """ |
paul@162 | 257 | Introduce into the 'slots' entries for those in 'points' that are not |
paul@170 | 258 | already present, propagating active periods from time points preceding |
paul@170 | 259 | those added. |
paul@162 | 260 | """ |
paul@162 | 261 | |
paul@162 | 262 | new_slots = [] |
paul@162 | 263 | |
paul@162 | 264 | for point in points: |
paul@162 | 265 | i = bisect_left(slots, (point, None)) |
paul@162 | 266 | if i < len(slots) and slots[i][0] == point: |
paul@162 | 267 | continue |
paul@162 | 268 | |
paul@170 | 269 | new_slots.append((point, i > 0 and slots[i-1][1] or [])) |
paul@162 | 270 | |
paul@162 | 271 | for t in new_slots: |
paul@162 | 272 | insort_left(slots, t) |
paul@162 | 273 | |
paul@162 | 274 | def partition_by_day(slots): |
paul@162 | 275 | |
paul@162 | 276 | """ |
paul@162 | 277 | Return a mapping from dates to time points provided by 'slots'. |
paul@162 | 278 | """ |
paul@162 | 279 | |
paul@162 | 280 | d = {} |
paul@162 | 281 | |
paul@162 | 282 | for point, value in slots: |
paul@162 | 283 | day = point.date() |
paul@162 | 284 | if not d.has_key(day): |
paul@162 | 285 | d[day] = [] |
paul@162 | 286 | d[day].append((point, value)) |
paul@162 | 287 | |
paul@162 | 288 | return d |
paul@153 | 289 | |
paul@114 | 290 | def get_spans(slots): |
paul@114 | 291 | |
paul@114 | 292 | "Inspect the given 'slots', returning a mapping of event uids to spans." |
paul@114 | 293 | |
paul@114 | 294 | points = [point for point, active in slots] |
paul@114 | 295 | spans = {} |
paul@114 | 296 | |
paul@114 | 297 | for point, active in slots: |
paul@114 | 298 | for t in active: |
paul@185 | 299 | if t and len(t) >= 2: |
paul@185 | 300 | start, end, uid, key = get_freebusy_details(t) |
paul@185 | 301 | |
paul@153 | 302 | try: |
paul@153 | 303 | start_slot = points.index(start) |
paul@153 | 304 | except ValueError: |
paul@153 | 305 | start_slot = 0 |
paul@153 | 306 | try: |
paul@153 | 307 | end_slot = points.index(end) |
paul@153 | 308 | except ValueError: |
paul@153 | 309 | end_slot = len(slots) |
paul@185 | 310 | spans[key] = end_slot - start_slot |
paul@114 | 311 | |
paul@114 | 312 | return spans |
paul@114 | 313 | |
paul@185 | 314 | def get_freebusy_details(t): |
paul@185 | 315 | |
paul@185 | 316 | "Return a tuple of the form (start, end, uid, key) from 't'." |
paul@185 | 317 | |
paul@185 | 318 | # Handle both complete free/busy details... |
paul@185 | 319 | |
paul@185 | 320 | if len(t) > 2: |
paul@185 | 321 | start, end, uid = t[:3] |
paul@185 | 322 | key = uid |
paul@185 | 323 | |
paul@185 | 324 | # ...and published details without specific event details. |
paul@185 | 325 | |
paul@185 | 326 | else: |
paul@185 | 327 | start, end = t[:2] |
paul@185 | 328 | uid = None |
paul@185 | 329 | key = (start, end) |
paul@185 | 330 | |
paul@185 | 331 | return start, end, uid, key |
paul@185 | 332 | |
paul@250 | 333 | # NOTE: Need to expose the 100 day window for recurring events in the |
paul@250 | 334 | # NOTE: configuration. |
paul@250 | 335 | |
paul@250 | 336 | def get_periods(obj, window_size=100): |
paul@250 | 337 | |
paul@250 | 338 | """ |
paul@250 | 339 | Return periods for the given object 'obj', confining materialised periods |
paul@250 | 340 | to the given 'window_size' in days starting from the present moment. |
paul@250 | 341 | """ |
paul@250 | 342 | |
paul@250 | 343 | dtstart = obj.get_utc_datetime("DTSTART") |
paul@250 | 344 | dtend = obj.get_utc_datetime("DTEND") |
paul@250 | 345 | |
paul@250 | 346 | # NOTE: Need also DURATION support. |
paul@250 | 347 | |
paul@250 | 348 | duration = dtend - dtstart |
paul@250 | 349 | |
paul@250 | 350 | # Recurrence rules create multiple instances to be checked. |
paul@250 | 351 | # Conflicts may only be assessed within a period defined by policy |
paul@250 | 352 | # for the agent, with instances outside that period being considered |
paul@250 | 353 | # unchecked. |
paul@250 | 354 | |
paul@250 | 355 | window_end = datetime.now() + timedelta(window_size) |
paul@250 | 356 | |
paul@250 | 357 | # NOTE: Need also RDATE and EXDATE support. |
paul@250 | 358 | |
paul@250 | 359 | rrule = obj.get_value("RRULE") |
paul@250 | 360 | |
paul@250 | 361 | if rrule: |
paul@250 | 362 | selector = get_rule(dtstart, rrule) |
paul@250 | 363 | parameters = get_parameters(rrule) |
paul@250 | 364 | periods = [] |
paul@250 | 365 | for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")): |
paul@250 | 366 | start = datetime(*start, tzinfo=timezone("UTC")) |
paul@250 | 367 | end = start + duration |
paul@250 | 368 | periods.append((format_datetime(start), format_datetime(end))) |
paul@250 | 369 | else: |
paul@250 | 370 | periods = [(format_datetime(dtstart), format_datetime(dtend))] |
paul@250 | 371 | |
paul@250 | 372 | return periods |
paul@250 | 373 | |
paul@250 | 374 | def remove_from_freebusy(freebusy, attendee, uid, store): |
paul@250 | 375 | |
paul@250 | 376 | """ |
paul@250 | 377 | For the given 'attendee', remove periods from 'freebusy' that are associated |
paul@250 | 378 | with 'uid' in the 'store'. |
paul@250 | 379 | """ |
paul@250 | 380 | |
paul@250 | 381 | remove_period(freebusy, uid) |
paul@250 | 382 | store.set_freebusy(attendee, freebusy) |
paul@250 | 383 | |
paul@250 | 384 | def remove_from_freebusy_for_other(freebusy, user, other, uid, store): |
paul@250 | 385 | |
paul@250 | 386 | """ |
paul@250 | 387 | For the given 'user', remove for the 'other' party periods from 'freebusy' |
paul@250 | 388 | that are associated with 'uid' in the 'store'. |
paul@250 | 389 | """ |
paul@250 | 390 | |
paul@250 | 391 | remove_period(freebusy, uid) |
paul@250 | 392 | store.set_freebusy_for_other(user, freebusy, other) |
paul@250 | 393 | |
paul@250 | 394 | def _update_freebusy(freebusy, periods, transp, uid): |
paul@250 | 395 | |
paul@250 | 396 | """ |
paul@250 | 397 | Update the free/busy details with the given 'periods', 'transp' setting and |
paul@250 | 398 | 'uid'. |
paul@250 | 399 | """ |
paul@250 | 400 | |
paul@250 | 401 | remove_period(freebusy, uid) |
paul@250 | 402 | |
paul@250 | 403 | for start, end in periods: |
paul@250 | 404 | insert_period(freebusy, (start, end, uid, transp)) |
paul@250 | 405 | |
paul@250 | 406 | def update_freebusy(freebusy, attendee, periods, transp, uid, store): |
paul@250 | 407 | |
paul@250 | 408 | """ |
paul@250 | 409 | For the given 'attendee', update the free/busy details with the given |
paul@250 | 410 | 'periods', 'transp' setting and 'uid' in the 'store'. |
paul@250 | 411 | """ |
paul@250 | 412 | |
paul@250 | 413 | _update_freebusy(freebusy, periods, transp, uid) |
paul@250 | 414 | store.set_freebusy(attendee, freebusy) |
paul@250 | 415 | |
paul@250 | 416 | def update_freebusy_for_other(freebusy, user, other, periods, transp, uid, store): |
paul@250 | 417 | |
paul@250 | 418 | """ |
paul@250 | 419 | For the given 'user', update the free/busy details of 'other' with the given |
paul@250 | 420 | 'periods', 'transp' setting and 'uid' in the 'store'. |
paul@250 | 421 | """ |
paul@250 | 422 | |
paul@250 | 423 | _update_freebusy(freebusy, periods, transp, uid) |
paul@250 | 424 | store.set_freebusy_for_other(user, freebusy, other) |
paul@250 | 425 | |
paul@250 | 426 | |
paul@48 | 427 | # vim: tabstop=4 expandtab shiftwidth=4 |