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@232 | 23 | from datetime import datetime |
paul@153 | 24 | from imiptools.dates import get_datetime, get_start_of_day, to_timezone |
paul@48 | 25 | |
paul@48 | 26 | # Time management. |
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@113 | 116 | # Period layout. |
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@162 | 218 | def add_day_start_points(slots): |
paul@153 | 219 | |
paul@153 | 220 | """ |
paul@162 | 221 | Introduce into the 'slots' any day start points required by multi-day |
paul@162 | 222 | periods. |
paul@153 | 223 | """ |
paul@153 | 224 | |
paul@162 | 225 | new_slots = [] |
paul@153 | 226 | current_date = None |
paul@200 | 227 | previously_active = [] |
paul@153 | 228 | |
paul@153 | 229 | for point, active in slots: |
paul@162 | 230 | start_of_day = get_start_of_day(point) |
paul@162 | 231 | this_date = point.date() |
paul@153 | 232 | |
paul@198 | 233 | # For each new day, add a slot for the start of the day where periods |
paul@198 | 234 | # are active and where no such slot already exists. |
paul@153 | 235 | |
paul@153 | 236 | if this_date != current_date: |
paul@153 | 237 | current_date = this_date |
paul@153 | 238 | |
paul@153 | 239 | # Add any continuing periods. |
paul@153 | 240 | |
paul@200 | 241 | if point != start_of_day: |
paul@162 | 242 | new_slots.append((start_of_day, previously_active)) |
paul@153 | 243 | |
paul@153 | 244 | # Add the currently active periods at this point in time. |
paul@153 | 245 | |
paul@153 | 246 | previously_active = active |
paul@153 | 247 | |
paul@162 | 248 | for t in new_slots: |
paul@162 | 249 | insort_left(slots, t) |
paul@162 | 250 | |
paul@162 | 251 | def add_slots(slots, points): |
paul@162 | 252 | |
paul@162 | 253 | """ |
paul@162 | 254 | Introduce into the 'slots' entries for those in 'points' that are not |
paul@170 | 255 | already present, propagating active periods from time points preceding |
paul@170 | 256 | those added. |
paul@162 | 257 | """ |
paul@162 | 258 | |
paul@162 | 259 | new_slots = [] |
paul@162 | 260 | |
paul@162 | 261 | for point in points: |
paul@162 | 262 | i = bisect_left(slots, (point, None)) |
paul@162 | 263 | if i < len(slots) and slots[i][0] == point: |
paul@162 | 264 | continue |
paul@162 | 265 | |
paul@170 | 266 | new_slots.append((point, i > 0 and slots[i-1][1] or [])) |
paul@162 | 267 | |
paul@162 | 268 | for t in new_slots: |
paul@162 | 269 | insort_left(slots, t) |
paul@162 | 270 | |
paul@162 | 271 | def partition_by_day(slots): |
paul@162 | 272 | |
paul@162 | 273 | """ |
paul@162 | 274 | Return a mapping from dates to time points provided by 'slots'. |
paul@162 | 275 | """ |
paul@162 | 276 | |
paul@162 | 277 | d = {} |
paul@162 | 278 | |
paul@162 | 279 | for point, value in slots: |
paul@162 | 280 | day = point.date() |
paul@162 | 281 | if not d.has_key(day): |
paul@162 | 282 | d[day] = [] |
paul@162 | 283 | d[day].append((point, value)) |
paul@162 | 284 | |
paul@162 | 285 | return d |
paul@153 | 286 | |
paul@114 | 287 | def get_spans(slots): |
paul@114 | 288 | |
paul@114 | 289 | "Inspect the given 'slots', returning a mapping of event uids to spans." |
paul@114 | 290 | |
paul@114 | 291 | points = [point for point, active in slots] |
paul@114 | 292 | spans = {} |
paul@114 | 293 | |
paul@114 | 294 | for point, active in slots: |
paul@114 | 295 | for t in active: |
paul@185 | 296 | if t and len(t) >= 2: |
paul@185 | 297 | start, end, uid, key = get_freebusy_details(t) |
paul@185 | 298 | |
paul@153 | 299 | try: |
paul@153 | 300 | start_slot = points.index(start) |
paul@153 | 301 | except ValueError: |
paul@153 | 302 | start_slot = 0 |
paul@153 | 303 | try: |
paul@153 | 304 | end_slot = points.index(end) |
paul@153 | 305 | except ValueError: |
paul@153 | 306 | end_slot = len(slots) |
paul@185 | 307 | spans[key] = end_slot - start_slot |
paul@114 | 308 | |
paul@114 | 309 | return spans |
paul@114 | 310 | |
paul@185 | 311 | def get_freebusy_details(t): |
paul@185 | 312 | |
paul@185 | 313 | "Return a tuple of the form (start, end, uid, key) from 't'." |
paul@185 | 314 | |
paul@185 | 315 | # Handle both complete free/busy details... |
paul@185 | 316 | |
paul@185 | 317 | if len(t) > 2: |
paul@185 | 318 | start, end, uid = t[:3] |
paul@185 | 319 | key = uid |
paul@185 | 320 | |
paul@185 | 321 | # ...and published details without specific event details. |
paul@185 | 322 | |
paul@185 | 323 | else: |
paul@185 | 324 | start, end = t[:2] |
paul@185 | 325 | uid = None |
paul@185 | 326 | key = (start, end) |
paul@185 | 327 | |
paul@185 | 328 | return start, end, uid, key |
paul@185 | 329 | |
paul@48 | 330 | # vim: tabstop=4 expandtab shiftwidth=4 |