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@430 | 22 | from bisect import bisect_left, bisect_right, insort_left |
paul@529 | 23 | from datetime import date, datetime, timedelta |
paul@529 | 24 | from imiptools.dates import format_datetime, get_datetime, \ |
paul@549 | 25 | get_datetime_attributes, get_start_of_day, \ |
paul@549 | 26 | get_tzid, to_timezone, to_utc_datetime |
paul@48 | 27 | |
paul@458 | 28 | class Period: |
paul@458 | 29 | |
paul@458 | 30 | "A basic period abstraction." |
paul@458 | 31 | |
paul@541 | 32 | def __init__(self, start, end, tzid=None, origin=None): |
paul@541 | 33 | |
paul@541 | 34 | """ |
paul@541 | 35 | Initialise a period with the given 'start' and 'end', having a |
paul@541 | 36 | contextual 'tzid', if specified, and an indicated 'origin'. |
paul@541 | 37 | """ |
paul@541 | 38 | |
paul@529 | 39 | self.start = isinstance(start, date) and start or get_datetime(start) |
paul@529 | 40 | self.end = isinstance(end, date) and end or get_datetime(end) |
paul@541 | 41 | self.tzid = tzid |
paul@528 | 42 | self.origin = origin |
paul@458 | 43 | |
paul@458 | 44 | def as_tuple(self): |
paul@541 | 45 | return self.start, self.end, self.tzid, self.origin |
paul@458 | 46 | |
paul@458 | 47 | def __hash__(self): |
paul@529 | 48 | return hash((self.get_start(), self.get_end())) |
paul@458 | 49 | |
paul@458 | 50 | def __cmp__(self, other): |
paul@541 | 51 | |
paul@541 | 52 | """ |
paul@541 | 53 | Return a comparison result against 'other' using points in time, |
paul@541 | 54 | employing the time zone context to convert dates. |
paul@541 | 55 | """ |
paul@541 | 56 | |
paul@458 | 57 | if isinstance(other, Period): |
paul@541 | 58 | return cmp( |
paul@541 | 59 | (to_utc_datetime(self.get_start(), self.get_tzid()), to_utc_datetime(self.get_end(), self.get_tzid())), |
paul@541 | 60 | (to_utc_datetime(other.get_start(), self.get_tzid()), to_utc_datetime(other.get_end(), self.get_tzid())) |
paul@541 | 61 | ) |
paul@458 | 62 | else: |
paul@458 | 63 | return 1 |
paul@458 | 64 | |
paul@458 | 65 | def get_key(self): |
paul@529 | 66 | return self.get_start(), self.get_end() |
paul@458 | 67 | |
paul@458 | 68 | def __repr__(self): |
paul@541 | 69 | return "Period(%r)" % (self.as_tuple(),) |
paul@458 | 70 | |
paul@528 | 71 | # Datetime metadata methods. |
paul@528 | 72 | |
paul@535 | 73 | def get_start(self): |
paul@535 | 74 | return self.start |
paul@528 | 75 | |
paul@535 | 76 | def get_end(self): |
paul@535 | 77 | return self.end |
paul@528 | 78 | |
paul@541 | 79 | def get_tzid(self): |
paul@541 | 80 | return self.tzid |
paul@541 | 81 | |
paul@528 | 82 | def get_start_item(self): |
paul@528 | 83 | return self.start, get_datetime_attributes(self.start) |
paul@528 | 84 | |
paul@528 | 85 | def get_end_item(self): |
paul@528 | 86 | return self.end, get_datetime_attributes(self.end) |
paul@528 | 87 | |
paul@458 | 88 | class FreeBusyPeriod(Period): |
paul@458 | 89 | |
paul@458 | 90 | "A free/busy record abstraction." |
paul@458 | 91 | |
paul@541 | 92 | def __init__(self, start, end, uid=None, transp=None, recurrenceid=None, summary=None, organiser=None, tzid=None): |
paul@529 | 93 | |
paul@529 | 94 | """ |
paul@541 | 95 | Initialise a free/busy period with the given 'start' and 'end' limits, |
paul@541 | 96 | with an optional 'tzid', plus any 'uid', 'transp', 'recurrenceid', |
paul@541 | 97 | 'summary' and 'organiser' details. |
paul@529 | 98 | """ |
paul@529 | 99 | |
paul@541 | 100 | Period.__init__(self, start, end, tzid) |
paul@458 | 101 | self.uid = uid |
paul@458 | 102 | self.transp = transp |
paul@458 | 103 | self.recurrenceid = recurrenceid |
paul@458 | 104 | self.summary = summary |
paul@458 | 105 | self.organiser = organiser |
paul@458 | 106 | |
paul@458 | 107 | def as_tuple(self): |
paul@529 | 108 | return format_datetime(self.start), format_datetime(self.end), self.uid, self.transp, self.recurrenceid, self.summary, self.organiser |
paul@458 | 109 | |
paul@458 | 110 | def __cmp__(self, other): |
paul@541 | 111 | |
paul@541 | 112 | """ |
paul@541 | 113 | Compare this object to 'other', employing the uid if the periods |
paul@541 | 114 | involved are the same. |
paul@541 | 115 | """ |
paul@541 | 116 | |
paul@541 | 117 | result = Period.__cmp__(self, other) |
paul@541 | 118 | if result == 0 and isinstance(other, FreeBusyPeriod): |
paul@541 | 119 | return cmp(self.uid, other.uid) |
paul@458 | 120 | else: |
paul@541 | 121 | return result |
paul@458 | 122 | |
paul@458 | 123 | def get_key(self): |
paul@541 | 124 | return self.uid, self.recurrenceid, self.get_start() |
paul@458 | 125 | |
paul@458 | 126 | def __repr__(self): |
paul@541 | 127 | return "FreeBusyPeriod(%r)" % (self.as_tuple(),) |
paul@458 | 128 | |
paul@543 | 129 | class RecurringPeriod(Period): |
paul@543 | 130 | |
paul@543 | 131 | "A period with iCalendar attribute and origin information from the object." |
paul@543 | 132 | |
paul@543 | 133 | def __init__(self, start, end, tzid=None, origin=None, start_attr=None, end_attr=None): |
paul@543 | 134 | Period.__init__(self, start, end, tzid, origin) |
paul@543 | 135 | self.start_attr = start_attr |
paul@543 | 136 | self.end_attr = end_attr |
paul@543 | 137 | |
paul@543 | 138 | def get_start_item(self): |
paul@543 | 139 | return self.start, self.start_attr |
paul@543 | 140 | |
paul@543 | 141 | def get_end_item(self): |
paul@543 | 142 | return self.end, self.end_attr |
paul@543 | 143 | |
paul@543 | 144 | def get_tzid(self): |
paul@543 | 145 | return get_tzid(self.start_attr, self.end_attr) or self.tzid |
paul@543 | 146 | |
paul@543 | 147 | def as_tuple(self): |
paul@543 | 148 | return self.start, self.end, self.tzid, self.origin, self.start_attr, self.end_attr |
paul@543 | 149 | |
paul@543 | 150 | def __repr__(self): |
paul@543 | 151 | return "RecurringPeriod(%r)" % (self.as_tuple(),) |
paul@543 | 152 | |
paul@529 | 153 | # Time and period management. |
paul@48 | 154 | |
paul@343 | 155 | def can_schedule(freebusy, periods, uid, recurrenceid): |
paul@221 | 156 | |
paul@221 | 157 | """ |
paul@221 | 158 | Return whether the 'freebusy' list can accommodate the given 'periods' |
paul@343 | 159 | employing the specified 'uid' and 'recurrenceid'. |
paul@221 | 160 | """ |
paul@221 | 161 | |
paul@221 | 162 | for conflict in have_conflict(freebusy, periods, True): |
paul@465 | 163 | if conflict.uid != uid or conflict.recurrenceid != recurrenceid: |
paul@221 | 164 | return False |
paul@221 | 165 | |
paul@221 | 166 | return True |
paul@221 | 167 | |
paul@72 | 168 | def have_conflict(freebusy, periods, get_conflicts=False): |
paul@72 | 169 | |
paul@72 | 170 | """ |
paul@72 | 171 | Return whether any period in 'freebusy' overlaps with the given 'periods', |
paul@72 | 172 | returning a collection of such overlapping periods if 'get_conflicts' is |
paul@72 | 173 | set to a true value. |
paul@72 | 174 | """ |
paul@72 | 175 | |
paul@458 | 176 | conflicts = set() |
paul@458 | 177 | for p in periods: |
paul@458 | 178 | overlapping = period_overlaps(freebusy, p, get_conflicts) |
paul@74 | 179 | if overlapping: |
paul@72 | 180 | if get_conflicts: |
paul@458 | 181 | conflicts.update(overlapping) |
paul@72 | 182 | else: |
paul@72 | 183 | return True |
paul@74 | 184 | |
paul@72 | 185 | if get_conflicts: |
paul@72 | 186 | return conflicts |
paul@72 | 187 | else: |
paul@72 | 188 | return False |
paul@72 | 189 | |
paul@48 | 190 | def insert_period(freebusy, period): |
paul@362 | 191 | |
paul@362 | 192 | "Insert into 'freebusy' the given 'period'." |
paul@362 | 193 | |
paul@48 | 194 | insort_left(freebusy, period) |
paul@48 | 195 | |
paul@343 | 196 | def remove_period(freebusy, uid, recurrenceid=None): |
paul@362 | 197 | |
paul@362 | 198 | """ |
paul@362 | 199 | Remove from 'freebusy' all periods associated with 'uid' and 'recurrenceid' |
paul@362 | 200 | (which if omitted causes the "parent" object's periods to be referenced). |
paul@362 | 201 | """ |
paul@362 | 202 | |
paul@523 | 203 | removed = False |
paul@48 | 204 | i = 0 |
paul@48 | 205 | while i < len(freebusy): |
paul@458 | 206 | fb = freebusy[i] |
paul@458 | 207 | if fb.uid == uid and fb.recurrenceid == recurrenceid: |
paul@48 | 208 | del freebusy[i] |
paul@523 | 209 | removed = True |
paul@48 | 210 | else: |
paul@48 | 211 | i += 1 |
paul@48 | 212 | |
paul@523 | 213 | return removed |
paul@523 | 214 | |
paul@382 | 215 | def remove_additional_periods(freebusy, uid, recurrenceids=None): |
paul@381 | 216 | |
paul@381 | 217 | """ |
paul@381 | 218 | Remove from 'freebusy' all periods associated with 'uid' having a |
paul@381 | 219 | recurrence identifier indicating an additional or modified period. |
paul@382 | 220 | |
paul@382 | 221 | If 'recurrenceids' is specified, remove all periods associated with 'uid' |
paul@382 | 222 | that do not have a recurrence identifier in the given list. |
paul@381 | 223 | """ |
paul@381 | 224 | |
paul@381 | 225 | i = 0 |
paul@381 | 226 | while i < len(freebusy): |
paul@458 | 227 | fb = freebusy[i] |
paul@458 | 228 | if fb.uid == uid and fb.recurrenceid and ( |
paul@382 | 229 | recurrenceids is None or |
paul@458 | 230 | recurrenceids is not None and fb.recurrenceid not in recurrenceids |
paul@382 | 231 | ): |
paul@381 | 232 | del freebusy[i] |
paul@381 | 233 | else: |
paul@381 | 234 | i += 1 |
paul@381 | 235 | |
paul@511 | 236 | def remove_affected_period(freebusy, uid, start): |
paul@362 | 237 | |
paul@362 | 238 | """ |
paul@362 | 239 | Remove from 'freebusy' a period associated with 'uid' that provides an |
paul@511 | 240 | occurrence starting at the given 'start' (provided by a recurrence |
paul@511 | 241 | identifier, converted to a datetime). A recurrence identifier is used to |
paul@511 | 242 | provide an alternative time period whilst also acting as a reference to the |
paul@511 | 243 | originally-defined occurrence. |
paul@362 | 244 | """ |
paul@362 | 245 | |
paul@548 | 246 | found = bisect_left(freebusy, Period(start, start)) |
paul@376 | 247 | while found < len(freebusy): |
paul@458 | 248 | fb = freebusy[found] |
paul@376 | 249 | |
paul@376 | 250 | # Stop looking if the start no longer matches the recurrence identifier. |
paul@376 | 251 | |
paul@529 | 252 | if fb.get_start() != start: |
paul@376 | 253 | return |
paul@376 | 254 | |
paul@376 | 255 | # If the period belongs to the parent object, remove it and return. |
paul@376 | 256 | |
paul@458 | 257 | if not fb.recurrenceid and uid == fb.uid: |
paul@361 | 258 | del freebusy[found] |
paul@376 | 259 | break |
paul@376 | 260 | |
paul@376 | 261 | # Otherwise, keep looking for a matching period. |
paul@376 | 262 | |
paul@376 | 263 | found += 1 |
paul@361 | 264 | |
paul@327 | 265 | def get_overlapping(freebusy, period): |
paul@327 | 266 | |
paul@327 | 267 | """ |
paul@430 | 268 | Return the entries in 'freebusy' providing periods overlapping with |
paul@327 | 269 | 'period'. |
paul@327 | 270 | """ |
paul@327 | 271 | |
paul@430 | 272 | # Find the range of periods potentially overlapping the period in the |
paul@430 | 273 | # free/busy collection. |
paul@327 | 274 | |
paul@529 | 275 | last = bisect_right(freebusy, Period(period.get_end(), period.get_end())) |
paul@430 | 276 | startpoints = freebusy[:last] |
paul@327 | 277 | |
paul@430 | 278 | # Find the range of periods potentially overlapping the period in a version |
paul@430 | 279 | # of the free/busy collection sorted according to end datetimes. |
paul@327 | 280 | |
paul@529 | 281 | endpoints = [(fb.get_end(), fb.get_start(), fb) for fb in startpoints] |
paul@430 | 282 | endpoints.sort() |
paul@529 | 283 | first = bisect_left(endpoints, (period.get_start(),)) |
paul@430 | 284 | endpoints = endpoints[first:] |
paul@327 | 285 | |
paul@430 | 286 | overlapping = set() |
paul@327 | 287 | |
paul@458 | 288 | for end, start, fb in endpoints: |
paul@529 | 289 | if end > period.get_start() and start < period.get_end(): |
paul@458 | 290 | overlapping.add(fb) |
paul@327 | 291 | |
paul@430 | 292 | overlapping = list(overlapping) |
paul@430 | 293 | overlapping.sort() |
paul@430 | 294 | return overlapping |
paul@327 | 295 | |
paul@74 | 296 | def period_overlaps(freebusy, period, get_periods=False): |
paul@72 | 297 | |
paul@72 | 298 | """ |
paul@74 | 299 | Return whether any period in 'freebusy' overlaps with the given 'period', |
paul@74 | 300 | returning a collection of overlapping periods if 'get_periods' is set to a |
paul@74 | 301 | true value. |
paul@72 | 302 | """ |
paul@72 | 303 | |
paul@430 | 304 | overlapping = get_overlapping(freebusy, period) |
paul@74 | 305 | |
paul@74 | 306 | if get_periods: |
paul@430 | 307 | return overlapping |
paul@74 | 308 | else: |
paul@430 | 309 | return len(overlapping) != 0 |
paul@327 | 310 | |
paul@327 | 311 | def remove_overlapping(freebusy, period): |
paul@327 | 312 | |
paul@327 | 313 | "Remove from 'freebusy' all periods overlapping with 'period'." |
paul@327 | 314 | |
paul@437 | 315 | overlapping = get_overlapping(freebusy, period) |
paul@327 | 316 | |
paul@437 | 317 | if overlapping: |
paul@437 | 318 | for fb in overlapping: |
paul@437 | 319 | freebusy.remove(fb) |
paul@327 | 320 | |
paul@327 | 321 | def replace_overlapping(freebusy, period, replacements): |
paul@327 | 322 | |
paul@327 | 323 | """ |
paul@327 | 324 | Replace existing periods in 'freebusy' within the given 'period', using the |
paul@327 | 325 | given 'replacements'. |
paul@327 | 326 | """ |
paul@327 | 327 | |
paul@327 | 328 | remove_overlapping(freebusy, period) |
paul@327 | 329 | for replacement in replacements: |
paul@327 | 330 | insert_period(freebusy, replacement) |
paul@48 | 331 | |
paul@529 | 332 | # Period layout. |
paul@204 | 333 | |
paul@529 | 334 | def get_scale(periods, tzid): |
paul@113 | 335 | |
paul@113 | 336 | """ |
paul@458 | 337 | Return an ordered time scale from the given list of 'periods'. |
paul@153 | 338 | |
paul@162 | 339 | The given 'tzid' is used to make sure that the times are defined according |
paul@162 | 340 | to the chosen time zone. |
paul@162 | 341 | |
paul@162 | 342 | The returned scale is a mapping from time to (starting, ending) tuples, |
paul@458 | 343 | where starting and ending are collections of periods. |
paul@113 | 344 | """ |
paul@113 | 345 | |
paul@113 | 346 | scale = {} |
paul@113 | 347 | |
paul@458 | 348 | for p in periods: |
paul@113 | 349 | |
paul@113 | 350 | # Add a point and this event to the starting list. |
paul@113 | 351 | |
paul@536 | 352 | start = to_timezone(p.get_start(), tzid) |
paul@536 | 353 | if not scale.has_key(start): |
paul@536 | 354 | scale[start] = [], [] |
paul@536 | 355 | scale[start][0].append(p) |
paul@113 | 356 | |
paul@113 | 357 | # Add a point and this event to the ending list. |
paul@113 | 358 | |
paul@536 | 359 | end = to_timezone(p.get_end(), tzid) |
paul@536 | 360 | if not scale.has_key(end): |
paul@536 | 361 | scale[end] = [], [] |
paul@536 | 362 | scale[end][1].append(p) |
paul@113 | 363 | |
paul@113 | 364 | return scale |
paul@113 | 365 | |
paul@455 | 366 | class Point: |
paul@455 | 367 | |
paul@455 | 368 | "A qualified point in time." |
paul@455 | 369 | |
paul@455 | 370 | PRINCIPAL, REPEATED = 0, 1 |
paul@455 | 371 | |
paul@455 | 372 | def __init__(self, point, indicator=None): |
paul@455 | 373 | self.point = point |
paul@455 | 374 | self.indicator = indicator or self.PRINCIPAL |
paul@455 | 375 | |
paul@455 | 376 | def __hash__(self): |
paul@455 | 377 | return hash((self.point, self.indicator)) |
paul@455 | 378 | |
paul@455 | 379 | def __cmp__(self, other): |
paul@455 | 380 | if isinstance(other, Point): |
paul@455 | 381 | return cmp((self.point, self.indicator), (other.point, other.indicator)) |
paul@455 | 382 | elif isinstance(other, datetime): |
paul@455 | 383 | return cmp(self.point, other) |
paul@455 | 384 | else: |
paul@455 | 385 | return 1 |
paul@455 | 386 | |
paul@455 | 387 | def __eq__(self, other): |
paul@455 | 388 | return self.__cmp__(other) == 0 |
paul@455 | 389 | |
paul@455 | 390 | def __ne__(self, other): |
paul@455 | 391 | return not self == other |
paul@455 | 392 | |
paul@455 | 393 | def __lt__(self, other): |
paul@455 | 394 | return self.__cmp__(other) < 0 |
paul@455 | 395 | |
paul@455 | 396 | def __le__(self, other): |
paul@455 | 397 | return self.__cmp__(other) <= 0 |
paul@455 | 398 | |
paul@455 | 399 | def __gt__(self, other): |
paul@455 | 400 | return not self <= other |
paul@455 | 401 | |
paul@455 | 402 | def __ge__(self, other): |
paul@455 | 403 | return not self < other |
paul@455 | 404 | |
paul@455 | 405 | def __repr__(self): |
paul@455 | 406 | return "Point(%r, Point.%s)" % (self.point, self.indicator and "REPEATED" or "PRINCIPAL") |
paul@452 | 407 | |
paul@162 | 408 | def get_slots(scale): |
paul@113 | 409 | |
paul@113 | 410 | """ |
paul@162 | 411 | Return an ordered list of time slots from the given 'scale'. |
paul@113 | 412 | |
paul@452 | 413 | Each slot is a tuple containing details of a point in time for the start of |
paul@458 | 414 | the slot, together with a list of parallel event periods. |
paul@452 | 415 | |
paul@455 | 416 | Each point in time is described as a Point representing the actual point in |
paul@455 | 417 | time together with an indicator of the nature of the point in time (as a |
paul@455 | 418 | principal point in a time scale or as a repeated point used to terminate |
paul@455 | 419 | events occurring for an instant in time). |
paul@113 | 420 | """ |
paul@113 | 421 | |
paul@113 | 422 | slots = [] |
paul@113 | 423 | active = [] |
paul@113 | 424 | |
paul@162 | 425 | points = scale.items() |
paul@162 | 426 | points.sort() |
paul@162 | 427 | |
paul@162 | 428 | for point, (starting, ending) in points: |
paul@449 | 429 | ending = set(ending) |
paul@449 | 430 | instants = ending.intersection(starting) |
paul@113 | 431 | |
paul@113 | 432 | # Discard all active events ending at or before this start time. |
paul@161 | 433 | # Free up the position in the active list. |
paul@113 | 434 | |
paul@449 | 435 | for t in ending.difference(instants): |
paul@113 | 436 | i = active.index(t) |
paul@113 | 437 | active[i] = None |
paul@113 | 438 | |
paul@161 | 439 | # For each event starting at the current point, fill any newly-vacated |
paul@161 | 440 | # position or add to the end of the active list. |
paul@161 | 441 | |
paul@113 | 442 | for t in starting: |
paul@113 | 443 | try: |
paul@113 | 444 | i = active.index(None) |
paul@113 | 445 | active[i] = t |
paul@113 | 446 | except ValueError: |
paul@113 | 447 | active.append(t) |
paul@113 | 448 | |
paul@161 | 449 | # Discard vacant positions from the end of the active list. |
paul@161 | 450 | |
paul@113 | 451 | while active and active[-1] is None: |
paul@113 | 452 | active.pop() |
paul@113 | 453 | |
paul@452 | 454 | # Add an entry for the time point before "instants". |
paul@452 | 455 | |
paul@455 | 456 | slots.append((Point(point), active[:])) |
paul@113 | 457 | |
paul@449 | 458 | # Discard events ending at the same time as they began. |
paul@449 | 459 | |
paul@449 | 460 | if instants: |
paul@449 | 461 | for t in instants: |
paul@449 | 462 | i = active.index(t) |
paul@449 | 463 | active[i] = None |
paul@449 | 464 | |
paul@449 | 465 | # Discard vacant positions from the end of the active list. |
paul@449 | 466 | |
paul@449 | 467 | while active and active[-1] is None: |
paul@449 | 468 | active.pop() |
paul@449 | 469 | |
paul@452 | 470 | # Add another entry for the time point after "instants". |
paul@449 | 471 | |
paul@455 | 472 | slots.append((Point(point, Point.REPEATED), active[:])) |
paul@449 | 473 | |
paul@113 | 474 | return slots |
paul@113 | 475 | |
paul@244 | 476 | def add_day_start_points(slots, tzid): |
paul@153 | 477 | |
paul@153 | 478 | """ |
paul@162 | 479 | Introduce into the 'slots' any day start points required by multi-day |
paul@244 | 480 | periods. The 'tzid' is required to make sure that appropriate time zones |
paul@244 | 481 | are chosen and not necessarily those provided by the existing time points. |
paul@153 | 482 | """ |
paul@153 | 483 | |
paul@162 | 484 | new_slots = [] |
paul@153 | 485 | current_date = None |
paul@200 | 486 | previously_active = [] |
paul@153 | 487 | |
paul@455 | 488 | for point, active in slots: |
paul@455 | 489 | start_of_day = get_start_of_day(point.point, tzid) |
paul@455 | 490 | this_date = point.point.date() |
paul@153 | 491 | |
paul@198 | 492 | # For each new day, add a slot for the start of the day where periods |
paul@198 | 493 | # are active and where no such slot already exists. |
paul@153 | 494 | |
paul@153 | 495 | if this_date != current_date: |
paul@414 | 496 | |
paul@414 | 497 | # Fill in days where events remain active. |
paul@414 | 498 | |
paul@414 | 499 | if current_date: |
paul@414 | 500 | current_date += timedelta(1) |
paul@414 | 501 | while current_date < this_date: |
paul@455 | 502 | new_slots.append((Point(get_start_of_day(current_date, tzid)), previously_active)) |
paul@414 | 503 | current_date += timedelta(1) |
paul@414 | 504 | else: |
paul@414 | 505 | current_date = this_date |
paul@153 | 506 | |
paul@153 | 507 | # Add any continuing periods. |
paul@153 | 508 | |
paul@455 | 509 | if point.point != start_of_day: |
paul@455 | 510 | new_slots.append((Point(start_of_day), previously_active)) |
paul@153 | 511 | |
paul@153 | 512 | # Add the currently active periods at this point in time. |
paul@153 | 513 | |
paul@153 | 514 | previously_active = active |
paul@153 | 515 | |
paul@162 | 516 | for t in new_slots: |
paul@162 | 517 | insort_left(slots, t) |
paul@162 | 518 | |
paul@162 | 519 | def add_slots(slots, points): |
paul@162 | 520 | |
paul@162 | 521 | """ |
paul@162 | 522 | Introduce into the 'slots' entries for those in 'points' that are not |
paul@170 | 523 | already present, propagating active periods from time points preceding |
paul@170 | 524 | those added. |
paul@162 | 525 | """ |
paul@162 | 526 | |
paul@162 | 527 | new_slots = [] |
paul@162 | 528 | |
paul@162 | 529 | for point in points: |
paul@452 | 530 | i = bisect_left(slots, (point,)) # slots is [(point, active)...] |
paul@162 | 531 | if i < len(slots) and slots[i][0] == point: |
paul@162 | 532 | continue |
paul@162 | 533 | |
paul@170 | 534 | new_slots.append((point, i > 0 and slots[i-1][1] or [])) |
paul@162 | 535 | |
paul@162 | 536 | for t in new_slots: |
paul@162 | 537 | insort_left(slots, t) |
paul@162 | 538 | |
paul@162 | 539 | def partition_by_day(slots): |
paul@162 | 540 | |
paul@162 | 541 | """ |
paul@162 | 542 | Return a mapping from dates to time points provided by 'slots'. |
paul@162 | 543 | """ |
paul@162 | 544 | |
paul@162 | 545 | d = {} |
paul@162 | 546 | |
paul@455 | 547 | for point, value in slots: |
paul@455 | 548 | day = point.point.date() |
paul@162 | 549 | if not d.has_key(day): |
paul@162 | 550 | d[day] = [] |
paul@455 | 551 | d[day].append((point, value)) |
paul@162 | 552 | |
paul@162 | 553 | return d |
paul@153 | 554 | |
paul@283 | 555 | def add_empty_days(days, tzid): |
paul@279 | 556 | |
paul@279 | 557 | "Add empty days to 'days' between busy days." |
paul@279 | 558 | |
paul@279 | 559 | last_day = None |
paul@279 | 560 | all_days = days.keys() |
paul@279 | 561 | all_days.sort() |
paul@279 | 562 | |
paul@279 | 563 | for day in all_days: |
paul@279 | 564 | if last_day: |
paul@279 | 565 | empty_day = last_day + timedelta(1) |
paul@279 | 566 | while empty_day < day: |
paul@455 | 567 | days[empty_day] = [(Point(get_start_of_day(empty_day, tzid)), None)] |
paul@279 | 568 | empty_day += timedelta(1) |
paul@279 | 569 | last_day = day |
paul@279 | 570 | |
paul@114 | 571 | def get_spans(slots): |
paul@114 | 572 | |
paul@533 | 573 | "Inspect the given 'slots', returning a mapping of period keys to spans." |
paul@114 | 574 | |
paul@455 | 575 | points = [point for point, active in slots] |
paul@114 | 576 | spans = {} |
paul@114 | 577 | |
paul@449 | 578 | for _point, active in slots: |
paul@458 | 579 | for p in active: |
paul@458 | 580 | if p: |
paul@458 | 581 | key = p.get_key() |
paul@529 | 582 | start_slot = bisect_left(points, p.get_start()) |
paul@529 | 583 | end_slot = bisect_left(points, p.get_end()) |
paul@185 | 584 | spans[key] = end_slot - start_slot |
paul@114 | 585 | |
paul@114 | 586 | return spans |
paul@114 | 587 | |
paul@395 | 588 | def update_freebusy(freebusy, periods, transp, uid, recurrenceid, summary, organiser): |
paul@250 | 589 | |
paul@250 | 590 | """ |
paul@395 | 591 | Update the free/busy details with the given 'periods', 'transp' setting, |
paul@395 | 592 | 'uid' plus 'recurrenceid' and 'summary' and 'organiser' details. |
paul@250 | 593 | """ |
paul@250 | 594 | |
paul@343 | 595 | remove_period(freebusy, uid, recurrenceid) |
paul@250 | 596 | |
paul@458 | 597 | for p in periods: |
paul@529 | 598 | insert_period(freebusy, FreeBusyPeriod(p.get_start(), p.get_end(), uid, transp, recurrenceid, summary, organiser)) |
paul@250 | 599 | |
paul@48 | 600 | # vim: tabstop=4 expandtab shiftwidth=4 |