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