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@939 | 24 | from imiptools.dates import check_permitted_values, correct_datetime, \ |
paul@939 | 25 | format_datetime, get_datetime, \ |
paul@563 | 26 | get_datetime_attributes, \ |
paul@563 | 27 | get_recurrence_start, get_recurrence_start_point, \ |
paul@563 | 28 | get_start_of_day, \ |
paul@563 | 29 | get_tzid, \ |
paul@563 | 30 | to_timezone, to_utc_datetime |
paul@48 | 31 | |
paul@924 | 32 | def ifnone(x, y): |
paul@924 | 33 | if x is None: return y |
paul@924 | 34 | else: return x |
paul@924 | 35 | |
paul@874 | 36 | class Comparable: |
paul@874 | 37 | |
paul@874 | 38 | "A date/datetime wrapper that allows comparisons with other types." |
paul@874 | 39 | |
paul@874 | 40 | def __init__(self, dt): |
paul@874 | 41 | self.dt = dt |
paul@874 | 42 | |
paul@874 | 43 | def __cmp__(self, other): |
paul@874 | 44 | dt = None |
paul@874 | 45 | odt = None |
paul@874 | 46 | |
paul@874 | 47 | # Find any dates/datetimes. |
paul@874 | 48 | |
paul@874 | 49 | if isinstance(self.dt, date): |
paul@874 | 50 | dt = self.dt |
paul@874 | 51 | if isinstance(other, date): |
paul@874 | 52 | odt = other |
paul@874 | 53 | elif isinstance(other, Comparable): |
paul@874 | 54 | if isinstance(other.dt, date): |
paul@874 | 55 | odt = other.dt |
paul@874 | 56 | else: |
paul@874 | 57 | other = other.dt |
paul@874 | 58 | |
paul@874 | 59 | if dt and odt: |
paul@874 | 60 | return cmp(dt, odt) |
paul@874 | 61 | elif dt: |
paul@874 | 62 | return other.__rcmp__(dt) |
paul@874 | 63 | elif odt: |
paul@874 | 64 | return self.dt.__cmp__(odt) |
paul@874 | 65 | else: |
paul@874 | 66 | return self.dt.__cmp__(other) |
paul@874 | 67 | |
paul@874 | 68 | class PointInTime: |
paul@874 | 69 | |
paul@874 | 70 | "A base class for special values." |
paul@874 | 71 | |
paul@874 | 72 | pass |
paul@874 | 73 | |
paul@874 | 74 | class StartOfTime(PointInTime): |
paul@874 | 75 | |
paul@874 | 76 | "A special value that compares earlier than other values." |
paul@874 | 77 | |
paul@874 | 78 | def __cmp__(self, other): |
paul@874 | 79 | if isinstance(other, StartOfTime): |
paul@874 | 80 | return 0 |
paul@874 | 81 | else: |
paul@874 | 82 | return -1 |
paul@874 | 83 | |
paul@874 | 84 | def __rcmp__(self, other): |
paul@874 | 85 | return -self.__cmp__(other) |
paul@874 | 86 | |
paul@887 | 87 | def __nonzero__(self): |
paul@887 | 88 | return False |
paul@887 | 89 | |
paul@874 | 90 | class EndOfTime(PointInTime): |
paul@874 | 91 | |
paul@874 | 92 | "A special value that compares later than other values." |
paul@874 | 93 | |
paul@874 | 94 | def __cmp__(self, other): |
paul@874 | 95 | if isinstance(other, EndOfTime): |
paul@874 | 96 | return 0 |
paul@874 | 97 | else: |
paul@874 | 98 | return 1 |
paul@874 | 99 | |
paul@874 | 100 | def __rcmp__(self, other): |
paul@874 | 101 | return -self.__cmp__(other) |
paul@874 | 102 | |
paul@887 | 103 | def __nonzero__(self): |
paul@887 | 104 | return False |
paul@887 | 105 | |
paul@646 | 106 | class PeriodBase: |
paul@458 | 107 | |
paul@458 | 108 | "A basic period abstraction." |
paul@458 | 109 | |
paul@646 | 110 | def as_tuple(self): |
paul@646 | 111 | return self.start, self.end |
paul@646 | 112 | |
paul@646 | 113 | def __hash__(self): |
paul@646 | 114 | return hash((self.get_start(), self.get_end())) |
paul@646 | 115 | |
paul@646 | 116 | def __cmp__(self, other): |
paul@646 | 117 | |
paul@646 | 118 | "Return a comparison result against 'other' using points in time." |
paul@646 | 119 | |
paul@646 | 120 | if isinstance(other, PeriodBase): |
paul@646 | 121 | return cmp( |
paul@924 | 122 | (Comparable(ifnone(self.get_start_point(), StartOfTime())), Comparable(ifnone(self.get_end_point(), EndOfTime()))), |
paul@924 | 123 | (Comparable(ifnone(other.get_start_point(), StartOfTime())), Comparable(ifnone(other.get_end_point(), EndOfTime()))) |
paul@646 | 124 | ) |
paul@646 | 125 | else: |
paul@646 | 126 | return 1 |
paul@646 | 127 | |
paul@874 | 128 | def overlaps(self, other): |
paul@924 | 129 | return Comparable(ifnone(self.get_end_point(), EndOfTime())) > Comparable(ifnone(other.get_start_point(), StartOfTime())) and \ |
paul@924 | 130 | Comparable(ifnone(self.get_start_point(), StartOfTime())) < Comparable(ifnone(other.get_end_point(), EndOfTime())) |
paul@874 | 131 | |
paul@646 | 132 | def get_key(self): |
paul@646 | 133 | return self.get_start(), self.get_end() |
paul@646 | 134 | |
paul@646 | 135 | # Datetime and metadata methods. |
paul@646 | 136 | |
paul@646 | 137 | def get_start(self): |
paul@646 | 138 | return self.start |
paul@646 | 139 | |
paul@646 | 140 | def get_end(self): |
paul@646 | 141 | return self.end |
paul@646 | 142 | |
paul@646 | 143 | def get_start_attr(self): |
paul@646 | 144 | return get_datetime_attributes(self.start, self.tzid) |
paul@646 | 145 | |
paul@646 | 146 | def get_end_attr(self): |
paul@646 | 147 | return get_datetime_attributes(self.end, self.tzid) |
paul@646 | 148 | |
paul@646 | 149 | def get_start_item(self): |
paul@646 | 150 | return self.get_start(), self.get_start_attr() |
paul@646 | 151 | |
paul@646 | 152 | def get_end_item(self): |
paul@646 | 153 | return self.get_end(), self.get_end_attr() |
paul@646 | 154 | |
paul@646 | 155 | def get_start_point(self): |
paul@646 | 156 | return self.start |
paul@646 | 157 | |
paul@646 | 158 | def get_end_point(self): |
paul@646 | 159 | return self.end |
paul@646 | 160 | |
paul@659 | 161 | def get_duration(self): |
paul@659 | 162 | return self.get_end_point() - self.get_start_point() |
paul@659 | 163 | |
paul@646 | 164 | class Period(PeriodBase): |
paul@646 | 165 | |
paul@646 | 166 | "A simple period abstraction." |
paul@646 | 167 | |
paul@541 | 168 | def __init__(self, start, end, tzid=None, origin=None): |
paul@541 | 169 | |
paul@541 | 170 | """ |
paul@541 | 171 | Initialise a period with the given 'start' and 'end', having a |
paul@541 | 172 | contextual 'tzid', if specified, and an indicated 'origin'. |
paul@620 | 173 | |
paul@620 | 174 | All metadata from the start and end points are derived from the supplied |
paul@620 | 175 | dates/datetimes. |
paul@541 | 176 | """ |
paul@541 | 177 | |
paul@924 | 178 | if isinstance(start, (date, PointInTime)): self.start = start |
paul@924 | 179 | else: self.start = get_datetime(start) or StartOfTime() |
paul@924 | 180 | if isinstance(end, (date, PointInTime)): self.end = end |
paul@924 | 181 | else: self.end = get_datetime(end) or EndOfTime() |
paul@541 | 182 | self.tzid = tzid |
paul@528 | 183 | self.origin = origin |
paul@458 | 184 | |
paul@458 | 185 | def as_tuple(self): |
paul@541 | 186 | return self.start, self.end, self.tzid, self.origin |
paul@458 | 187 | |
paul@458 | 188 | def __repr__(self): |
paul@630 | 189 | return "Period%r" % (self.as_tuple(),) |
paul@458 | 190 | |
paul@646 | 191 | # Datetime and metadata methods. |
paul@528 | 192 | |
paul@541 | 193 | def get_tzid(self): |
paul@620 | 194 | return get_tzid(self.get_start_attr(), self.get_end_attr()) or self.tzid |
paul@620 | 195 | |
paul@557 | 196 | def get_start_point(self): |
paul@874 | 197 | start = self.get_start() |
paul@924 | 198 | if isinstance(start, PointInTime): return start |
paul@924 | 199 | else: return to_utc_datetime(start, self.get_tzid()) |
paul@557 | 200 | |
paul@557 | 201 | def get_end_point(self): |
paul@874 | 202 | end = self.get_end() |
paul@924 | 203 | if isinstance(end, PointInTime): return end |
paul@924 | 204 | else: return to_utc_datetime(end, self.get_tzid()) |
paul@557 | 205 | |
paul@633 | 206 | # Period and event recurrence logic. |
paul@633 | 207 | |
paul@633 | 208 | def is_replaced(self, recurrenceids): |
paul@633 | 209 | |
paul@633 | 210 | """ |
paul@633 | 211 | Return whether this period refers to one of the 'recurrenceids'. |
paul@633 | 212 | The 'recurrenceids' should be normalised to UTC datetimes according to |
paul@633 | 213 | time zone information provided by their objects or be floating dates or |
paul@633 | 214 | datetimes requiring conversion using contextual time zone information. |
paul@633 | 215 | """ |
paul@633 | 216 | |
paul@633 | 217 | for recurrenceid in recurrenceids: |
paul@647 | 218 | if self.is_affected(recurrenceid): |
paul@633 | 219 | return recurrenceid |
paul@633 | 220 | return None |
paul@633 | 221 | |
paul@633 | 222 | def is_affected(self, recurrenceid): |
paul@633 | 223 | |
paul@633 | 224 | """ |
paul@633 | 225 | Return whether this period refers to 'recurrenceid'. The 'recurrenceid' |
paul@633 | 226 | should be normalised to UTC datetimes according to time zone information |
paul@633 | 227 | provided by their objects. Otherwise, this period's contextual time zone |
paul@633 | 228 | information is used to convert any date or floating datetime |
paul@633 | 229 | representation to a point in time. |
paul@633 | 230 | """ |
paul@633 | 231 | |
paul@633 | 232 | if not recurrenceid: |
paul@633 | 233 | return None |
paul@633 | 234 | d = get_recurrence_start(recurrenceid) |
paul@633 | 235 | dt = get_recurrence_start_point(recurrenceid, self.tzid) |
paul@633 | 236 | if self.get_start() == d or self.get_start_point() == dt: |
paul@633 | 237 | return recurrenceid |
paul@633 | 238 | return None |
paul@633 | 239 | |
paul@939 | 240 | # Value correction methods. |
paul@939 | 241 | |
paul@939 | 242 | def get_corrected(self, permitted_values): |
paul@939 | 243 | |
paul@939 | 244 | "Return a corrected version of this period." |
paul@939 | 245 | |
paul@939 | 246 | start = self.get_start() |
paul@939 | 247 | end = self.get_end() |
paul@939 | 248 | start_errors = check_permitted_values(start, permitted_values) |
paul@939 | 249 | end_errors = check_permitted_values(end, permitted_values) |
paul@939 | 250 | |
paul@939 | 251 | if not (start_errors or end_errors): |
paul@939 | 252 | return self |
paul@939 | 253 | |
paul@939 | 254 | if start_errors: |
paul@939 | 255 | start = correct_datetime(start, permitted_values) |
paul@939 | 256 | if end_errors: |
paul@939 | 257 | end = correct_datetime(end, permitted_values) |
paul@939 | 258 | |
paul@939 | 259 | return self.make_corrected(start, end) |
paul@939 | 260 | |
paul@939 | 261 | def make_corrected(self, start, end): |
paul@939 | 262 | return self.__class__(start, end, self.tzid, self.origin) |
paul@939 | 263 | |
paul@646 | 264 | class FreeBusyPeriod(PeriodBase): |
paul@458 | 265 | |
paul@458 | 266 | "A free/busy record abstraction." |
paul@458 | 267 | |
paul@710 | 268 | def __init__(self, start, end, uid=None, transp=None, recurrenceid=None, summary=None, organiser=None, expires=None): |
paul@529 | 269 | |
paul@529 | 270 | """ |
paul@643 | 271 | Initialise a free/busy period with the given 'start' and 'end' points, |
paul@646 | 272 | plus any 'uid', 'transp', 'recurrenceid', 'summary' and 'organiser' |
paul@646 | 273 | details. |
paul@710 | 274 | |
paul@710 | 275 | An additional 'expires' parameter can be used to indicate an expiry |
paul@710 | 276 | datetime in conjunction with free/busy offers made when countering |
paul@710 | 277 | event proposals. |
paul@529 | 278 | """ |
paul@529 | 279 | |
paul@646 | 280 | self.start = isinstance(start, datetime) and start or get_datetime(start) |
paul@646 | 281 | self.end = isinstance(end, datetime) and end or get_datetime(end) |
paul@458 | 282 | self.uid = uid |
paul@458 | 283 | self.transp = transp |
paul@458 | 284 | self.recurrenceid = recurrenceid |
paul@458 | 285 | self.summary = summary |
paul@458 | 286 | self.organiser = organiser |
paul@710 | 287 | self.expires = expires |
paul@458 | 288 | |
paul@563 | 289 | def as_tuple(self, strings_only=False): |
paul@563 | 290 | |
paul@563 | 291 | """ |
paul@563 | 292 | Return the initialisation parameter tuple, converting false value |
paul@563 | 293 | parameters to strings if 'strings_only' is set to a true value. |
paul@563 | 294 | """ |
paul@563 | 295 | |
paul@563 | 296 | null = lambda x: (strings_only and [""] or [x])[0] |
paul@563 | 297 | return ( |
paul@630 | 298 | strings_only and format_datetime(self.get_start_point()) or self.start, |
paul@630 | 299 | strings_only and format_datetime(self.get_end_point()) or self.end, |
paul@563 | 300 | self.uid or null(self.uid), |
paul@563 | 301 | self.transp or strings_only and "OPAQUE" or None, |
paul@563 | 302 | self.recurrenceid or null(self.recurrenceid), |
paul@563 | 303 | self.summary or null(self.summary), |
paul@710 | 304 | self.organiser or null(self.organiser), |
paul@710 | 305 | self.expires or null(self.expires) |
paul@563 | 306 | ) |
paul@458 | 307 | |
paul@458 | 308 | def __cmp__(self, other): |
paul@541 | 309 | |
paul@541 | 310 | """ |
paul@541 | 311 | Compare this object to 'other', employing the uid if the periods |
paul@541 | 312 | involved are the same. |
paul@541 | 313 | """ |
paul@541 | 314 | |
paul@646 | 315 | result = PeriodBase.__cmp__(self, other) |
paul@541 | 316 | if result == 0 and isinstance(other, FreeBusyPeriod): |
paul@653 | 317 | return cmp((self.uid, self.recurrenceid), (other.uid, other.recurrenceid)) |
paul@458 | 318 | else: |
paul@541 | 319 | return result |
paul@458 | 320 | |
paul@458 | 321 | def get_key(self): |
paul@541 | 322 | return self.uid, self.recurrenceid, self.get_start() |
paul@458 | 323 | |
paul@458 | 324 | def __repr__(self): |
paul@630 | 325 | return "FreeBusyPeriod%r" % (self.as_tuple(),) |
paul@458 | 326 | |
paul@679 | 327 | # Period and event recurrence logic. |
paul@679 | 328 | |
paul@679 | 329 | def is_replaced(self, recurrences): |
paul@679 | 330 | |
paul@679 | 331 | """ |
paul@679 | 332 | Return whether this period refers to one of the 'recurrences'. |
paul@679 | 333 | The 'recurrences' must be UTC datetimes corresponding to the start of |
paul@679 | 334 | the period described by a recurrence. |
paul@679 | 335 | """ |
paul@679 | 336 | |
paul@679 | 337 | for recurrence in recurrences: |
paul@679 | 338 | if self.is_affected(recurrence): |
paul@679 | 339 | return True |
paul@679 | 340 | return False |
paul@679 | 341 | |
paul@679 | 342 | def is_affected(self, recurrence): |
paul@679 | 343 | |
paul@679 | 344 | """ |
paul@679 | 345 | Return whether this period refers to 'recurrence'. The 'recurrence' must |
paul@679 | 346 | be a UTC datetime corresponding to the start of the period described by |
paul@679 | 347 | a recurrence. |
paul@679 | 348 | """ |
paul@679 | 349 | |
paul@679 | 350 | return recurrence and self.get_start_point() == recurrence |
paul@679 | 351 | |
paul@543 | 352 | class RecurringPeriod(Period): |
paul@543 | 353 | |
paul@620 | 354 | """ |
paul@620 | 355 | A period with iCalendar metadata attributes and origin information from an |
paul@620 | 356 | object. |
paul@620 | 357 | """ |
paul@543 | 358 | |
paul@543 | 359 | def __init__(self, start, end, tzid=None, origin=None, start_attr=None, end_attr=None): |
paul@543 | 360 | Period.__init__(self, start, end, tzid, origin) |
paul@543 | 361 | self.start_attr = start_attr |
paul@543 | 362 | self.end_attr = end_attr |
paul@543 | 363 | |
paul@620 | 364 | def get_start_attr(self): |
paul@620 | 365 | return self.start_attr |
paul@543 | 366 | |
paul@620 | 367 | def get_end_attr(self): |
paul@620 | 368 | return self.end_attr |
paul@543 | 369 | |
paul@543 | 370 | def as_tuple(self): |
paul@543 | 371 | return self.start, self.end, self.tzid, self.origin, self.start_attr, self.end_attr |
paul@543 | 372 | |
paul@543 | 373 | def __repr__(self): |
paul@630 | 374 | return "RecurringPeriod%r" % (self.as_tuple(),) |
paul@543 | 375 | |
paul@939 | 376 | def make_corrected(self, start, end): |
paul@939 | 377 | return self.__class__(start, end, self.tzid, self.origin, self.get_start_attr(), self.get_end_attr()) |
paul@939 | 378 | |
paul@529 | 379 | # Time and period management. |
paul@48 | 380 | |
paul@343 | 381 | def can_schedule(freebusy, periods, uid, recurrenceid): |
paul@221 | 382 | |
paul@221 | 383 | """ |
paul@221 | 384 | Return whether the 'freebusy' list can accommodate the given 'periods' |
paul@343 | 385 | employing the specified 'uid' and 'recurrenceid'. |
paul@221 | 386 | """ |
paul@221 | 387 | |
paul@221 | 388 | for conflict in have_conflict(freebusy, periods, True): |
paul@465 | 389 | if conflict.uid != uid or conflict.recurrenceid != recurrenceid: |
paul@221 | 390 | return False |
paul@221 | 391 | |
paul@221 | 392 | return True |
paul@221 | 393 | |
paul@72 | 394 | def have_conflict(freebusy, periods, get_conflicts=False): |
paul@72 | 395 | |
paul@72 | 396 | """ |
paul@72 | 397 | Return whether any period in 'freebusy' overlaps with the given 'periods', |
paul@72 | 398 | returning a collection of such overlapping periods if 'get_conflicts' is |
paul@72 | 399 | set to a true value. |
paul@72 | 400 | """ |
paul@72 | 401 | |
paul@458 | 402 | conflicts = set() |
paul@458 | 403 | for p in periods: |
paul@458 | 404 | overlapping = period_overlaps(freebusy, p, get_conflicts) |
paul@74 | 405 | if overlapping: |
paul@72 | 406 | if get_conflicts: |
paul@458 | 407 | conflicts.update(overlapping) |
paul@72 | 408 | else: |
paul@72 | 409 | return True |
paul@74 | 410 | |
paul@72 | 411 | if get_conflicts: |
paul@72 | 412 | return conflicts |
paul@72 | 413 | else: |
paul@72 | 414 | return False |
paul@72 | 415 | |
paul@48 | 416 | def insert_period(freebusy, period): |
paul@362 | 417 | |
paul@362 | 418 | "Insert into 'freebusy' the given 'period'." |
paul@362 | 419 | |
paul@653 | 420 | i = bisect_left(freebusy, period) |
paul@653 | 421 | if i == len(freebusy): |
paul@653 | 422 | freebusy.append(period) |
paul@653 | 423 | elif freebusy[i] != period: |
paul@653 | 424 | freebusy.insert(i, period) |
paul@48 | 425 | |
paul@343 | 426 | def remove_period(freebusy, uid, recurrenceid=None): |
paul@362 | 427 | |
paul@362 | 428 | """ |
paul@362 | 429 | Remove from 'freebusy' all periods associated with 'uid' and 'recurrenceid' |
paul@362 | 430 | (which if omitted causes the "parent" object's periods to be referenced). |
paul@362 | 431 | """ |
paul@362 | 432 | |
paul@523 | 433 | removed = False |
paul@48 | 434 | i = 0 |
paul@48 | 435 | while i < len(freebusy): |
paul@458 | 436 | fb = freebusy[i] |
paul@458 | 437 | if fb.uid == uid and fb.recurrenceid == recurrenceid: |
paul@48 | 438 | del freebusy[i] |
paul@523 | 439 | removed = True |
paul@48 | 440 | else: |
paul@48 | 441 | i += 1 |
paul@48 | 442 | |
paul@523 | 443 | return removed |
paul@523 | 444 | |
paul@382 | 445 | def remove_additional_periods(freebusy, uid, recurrenceids=None): |
paul@381 | 446 | |
paul@381 | 447 | """ |
paul@381 | 448 | Remove from 'freebusy' all periods associated with 'uid' having a |
paul@381 | 449 | recurrence identifier indicating an additional or modified period. |
paul@382 | 450 | |
paul@382 | 451 | If 'recurrenceids' is specified, remove all periods associated with 'uid' |
paul@382 | 452 | that do not have a recurrence identifier in the given list. |
paul@381 | 453 | """ |
paul@381 | 454 | |
paul@381 | 455 | i = 0 |
paul@381 | 456 | while i < len(freebusy): |
paul@458 | 457 | fb = freebusy[i] |
paul@458 | 458 | if fb.uid == uid and fb.recurrenceid and ( |
paul@382 | 459 | recurrenceids is None or |
paul@458 | 460 | recurrenceids is not None and fb.recurrenceid not in recurrenceids |
paul@382 | 461 | ): |
paul@381 | 462 | del freebusy[i] |
paul@381 | 463 | else: |
paul@381 | 464 | i += 1 |
paul@381 | 465 | |
paul@511 | 466 | def remove_affected_period(freebusy, uid, start): |
paul@362 | 467 | |
paul@362 | 468 | """ |
paul@362 | 469 | Remove from 'freebusy' a period associated with 'uid' that provides an |
paul@511 | 470 | occurrence starting at the given 'start' (provided by a recurrence |
paul@511 | 471 | identifier, converted to a datetime). A recurrence identifier is used to |
paul@511 | 472 | provide an alternative time period whilst also acting as a reference to the |
paul@511 | 473 | originally-defined occurrence. |
paul@362 | 474 | """ |
paul@362 | 475 | |
paul@558 | 476 | search = Period(start, start) |
paul@558 | 477 | found = bisect_left(freebusy, search) |
paul@558 | 478 | |
paul@376 | 479 | while found < len(freebusy): |
paul@458 | 480 | fb = freebusy[found] |
paul@376 | 481 | |
paul@376 | 482 | # Stop looking if the start no longer matches the recurrence identifier. |
paul@376 | 483 | |
paul@558 | 484 | if fb.get_start_point() != search.get_start_point(): |
paul@376 | 485 | return |
paul@376 | 486 | |
paul@376 | 487 | # If the period belongs to the parent object, remove it and return. |
paul@376 | 488 | |
paul@458 | 489 | if not fb.recurrenceid and uid == fb.uid: |
paul@361 | 490 | del freebusy[found] |
paul@376 | 491 | break |
paul@376 | 492 | |
paul@376 | 493 | # Otherwise, keep looking for a matching period. |
paul@376 | 494 | |
paul@376 | 495 | found += 1 |
paul@361 | 496 | |
paul@658 | 497 | def periods_from(freebusy, period): |
paul@658 | 498 | |
paul@658 | 499 | "Return the entries in 'freebusy' at or after 'period'." |
paul@658 | 500 | |
paul@658 | 501 | first = bisect_left(freebusy, period) |
paul@658 | 502 | return freebusy[first:] |
paul@658 | 503 | |
paul@658 | 504 | def periods_until(freebusy, period): |
paul@658 | 505 | |
paul@658 | 506 | "Return the entries in 'freebusy' before 'period'." |
paul@658 | 507 | |
paul@658 | 508 | last = bisect_right(freebusy, Period(period.get_end(), period.get_end(), period.get_tzid())) |
paul@658 | 509 | return freebusy[:last] |
paul@658 | 510 | |
paul@327 | 511 | def get_overlapping(freebusy, period): |
paul@327 | 512 | |
paul@327 | 513 | """ |
paul@430 | 514 | Return the entries in 'freebusy' providing periods overlapping with |
paul@327 | 515 | 'period'. |
paul@327 | 516 | """ |
paul@327 | 517 | |
paul@430 | 518 | # Find the range of periods potentially overlapping the period in the |
paul@430 | 519 | # free/busy collection. |
paul@327 | 520 | |
paul@658 | 521 | startpoints = periods_until(freebusy, period) |
paul@327 | 522 | |
paul@430 | 523 | # Find the range of periods potentially overlapping the period in a version |
paul@430 | 524 | # of the free/busy collection sorted according to end datetimes. |
paul@327 | 525 | |
paul@874 | 526 | endpoints = [(Period(fb.get_end_point(), fb.get_end_point()), fb) for fb in startpoints] |
paul@430 | 527 | endpoints.sort() |
paul@874 | 528 | first = bisect_left(endpoints, (Period(period.get_start_point(), period.get_start_point()),)) |
paul@430 | 529 | endpoints = endpoints[first:] |
paul@327 | 530 | |
paul@430 | 531 | overlapping = set() |
paul@327 | 532 | |
paul@874 | 533 | for p, fb in endpoints: |
paul@874 | 534 | if fb.overlaps(period): |
paul@458 | 535 | overlapping.add(fb) |
paul@327 | 536 | |
paul@430 | 537 | overlapping = list(overlapping) |
paul@430 | 538 | overlapping.sort() |
paul@430 | 539 | return overlapping |
paul@327 | 540 | |
paul@74 | 541 | def period_overlaps(freebusy, period, get_periods=False): |
paul@72 | 542 | |
paul@72 | 543 | """ |
paul@74 | 544 | Return whether any period in 'freebusy' overlaps with the given 'period', |
paul@74 | 545 | returning a collection of overlapping periods if 'get_periods' is set to a |
paul@74 | 546 | true value. |
paul@72 | 547 | """ |
paul@72 | 548 | |
paul@430 | 549 | overlapping = get_overlapping(freebusy, period) |
paul@74 | 550 | |
paul@74 | 551 | if get_periods: |
paul@430 | 552 | return overlapping |
paul@74 | 553 | else: |
paul@430 | 554 | return len(overlapping) != 0 |
paul@327 | 555 | |
paul@327 | 556 | def remove_overlapping(freebusy, period): |
paul@327 | 557 | |
paul@327 | 558 | "Remove from 'freebusy' all periods overlapping with 'period'." |
paul@327 | 559 | |
paul@437 | 560 | overlapping = get_overlapping(freebusy, period) |
paul@327 | 561 | |
paul@437 | 562 | if overlapping: |
paul@437 | 563 | for fb in overlapping: |
paul@437 | 564 | freebusy.remove(fb) |
paul@327 | 565 | |
paul@327 | 566 | def replace_overlapping(freebusy, period, replacements): |
paul@327 | 567 | |
paul@327 | 568 | """ |
paul@327 | 569 | Replace existing periods in 'freebusy' within the given 'period', using the |
paul@327 | 570 | given 'replacements'. |
paul@327 | 571 | """ |
paul@327 | 572 | |
paul@327 | 573 | remove_overlapping(freebusy, period) |
paul@327 | 574 | for replacement in replacements: |
paul@327 | 575 | insert_period(freebusy, replacement) |
paul@48 | 576 | |
paul@658 | 577 | def coalesce_freebusy(freebusy): |
paul@658 | 578 | |
paul@658 | 579 | "Coalesce the periods in 'freebusy'." |
paul@658 | 580 | |
paul@658 | 581 | if not freebusy: |
paul@658 | 582 | return freebusy |
paul@658 | 583 | |
paul@658 | 584 | fb = [] |
paul@658 | 585 | start = freebusy[0].get_start_point() |
paul@658 | 586 | end = freebusy[0].get_end_point() |
paul@658 | 587 | |
paul@658 | 588 | for period in freebusy[1:]: |
paul@658 | 589 | if period.get_start_point() > end: |
paul@658 | 590 | fb.append(FreeBusyPeriod(start, end)) |
paul@658 | 591 | start = period.get_start_point() |
paul@658 | 592 | end = period.get_end_point() |
paul@658 | 593 | else: |
paul@658 | 594 | end = max(end, period.get_end_point()) |
paul@658 | 595 | |
paul@658 | 596 | fb.append(FreeBusyPeriod(start, end)) |
paul@658 | 597 | return fb |
paul@658 | 598 | |
paul@658 | 599 | def invert_freebusy(freebusy): |
paul@658 | 600 | |
paul@658 | 601 | "Return the free periods from 'freebusy'." |
paul@658 | 602 | |
paul@658 | 603 | if not freebusy: |
paul@658 | 604 | return None |
paul@658 | 605 | |
paul@658 | 606 | fb = coalesce_freebusy(freebusy) |
paul@658 | 607 | free = [] |
paul@658 | 608 | start = fb[0].get_end_point() |
paul@658 | 609 | |
paul@658 | 610 | for period in fb[1:]: |
paul@658 | 611 | free.append(FreeBusyPeriod(start, period.get_start_point())) |
paul@658 | 612 | start = period.get_end_point() |
paul@658 | 613 | |
paul@658 | 614 | return free |
paul@658 | 615 | |
paul@529 | 616 | # Period layout. |
paul@204 | 617 | |
paul@884 | 618 | def get_scale(periods, tzid, view_period=None): |
paul@113 | 619 | |
paul@113 | 620 | """ |
paul@925 | 621 | Return a time scale from the given list of 'periods'. |
paul@153 | 622 | |
paul@162 | 623 | The given 'tzid' is used to make sure that the times are defined according |
paul@162 | 624 | to the chosen time zone. |
paul@162 | 625 | |
paul@884 | 626 | An optional 'view_period' is used to constrain the scale to the given |
paul@884 | 627 | period. |
paul@884 | 628 | |
paul@162 | 629 | The returned scale is a mapping from time to (starting, ending) tuples, |
paul@458 | 630 | where starting and ending are collections of periods. |
paul@113 | 631 | """ |
paul@113 | 632 | |
paul@113 | 633 | scale = {} |
paul@884 | 634 | view_start = view_period and to_timezone(view_period.get_start_point(), tzid) or None |
paul@884 | 635 | view_end = view_period and to_timezone(view_period.get_end_point(), tzid) or None |
paul@113 | 636 | |
paul@458 | 637 | for p in periods: |
paul@113 | 638 | |
paul@113 | 639 | # Add a point and this event to the starting list. |
paul@113 | 640 | |
paul@536 | 641 | start = to_timezone(p.get_start(), tzid) |
paul@884 | 642 | start = view_start and max(start, view_start) or start |
paul@536 | 643 | if not scale.has_key(start): |
paul@536 | 644 | scale[start] = [], [] |
paul@536 | 645 | scale[start][0].append(p) |
paul@113 | 646 | |
paul@113 | 647 | # Add a point and this event to the ending list. |
paul@113 | 648 | |
paul@536 | 649 | end = to_timezone(p.get_end(), tzid) |
paul@931 | 650 | end = view_end and min(end, view_end) or end |
paul@931 | 651 | if not scale.has_key(end): |
paul@931 | 652 | scale[end] = [], [] |
paul@931 | 653 | scale[end][1].append(p) |
paul@113 | 654 | |
paul@113 | 655 | return scale |
paul@113 | 656 | |
paul@455 | 657 | class Point: |
paul@455 | 658 | |
paul@455 | 659 | "A qualified point in time." |
paul@455 | 660 | |
paul@455 | 661 | PRINCIPAL, REPEATED = 0, 1 |
paul@455 | 662 | |
paul@455 | 663 | def __init__(self, point, indicator=None): |
paul@455 | 664 | self.point = point |
paul@455 | 665 | self.indicator = indicator or self.PRINCIPAL |
paul@455 | 666 | |
paul@455 | 667 | def __hash__(self): |
paul@455 | 668 | return hash((self.point, self.indicator)) |
paul@455 | 669 | |
paul@455 | 670 | def __cmp__(self, other): |
paul@455 | 671 | if isinstance(other, Point): |
paul@455 | 672 | return cmp((self.point, self.indicator), (other.point, other.indicator)) |
paul@455 | 673 | elif isinstance(other, datetime): |
paul@455 | 674 | return cmp(self.point, other) |
paul@455 | 675 | else: |
paul@455 | 676 | return 1 |
paul@455 | 677 | |
paul@455 | 678 | def __eq__(self, other): |
paul@455 | 679 | return self.__cmp__(other) == 0 |
paul@455 | 680 | |
paul@455 | 681 | def __ne__(self, other): |
paul@455 | 682 | return not self == other |
paul@455 | 683 | |
paul@455 | 684 | def __lt__(self, other): |
paul@455 | 685 | return self.__cmp__(other) < 0 |
paul@455 | 686 | |
paul@455 | 687 | def __le__(self, other): |
paul@455 | 688 | return self.__cmp__(other) <= 0 |
paul@455 | 689 | |
paul@455 | 690 | def __gt__(self, other): |
paul@455 | 691 | return not self <= other |
paul@455 | 692 | |
paul@455 | 693 | def __ge__(self, other): |
paul@455 | 694 | return not self < other |
paul@455 | 695 | |
paul@455 | 696 | def __repr__(self): |
paul@455 | 697 | return "Point(%r, Point.%s)" % (self.point, self.indicator and "REPEATED" or "PRINCIPAL") |
paul@452 | 698 | |
paul@162 | 699 | def get_slots(scale): |
paul@113 | 700 | |
paul@113 | 701 | """ |
paul@162 | 702 | Return an ordered list of time slots from the given 'scale'. |
paul@113 | 703 | |
paul@452 | 704 | Each slot is a tuple containing details of a point in time for the start of |
paul@458 | 705 | the slot, together with a list of parallel event periods. |
paul@452 | 706 | |
paul@455 | 707 | Each point in time is described as a Point representing the actual point in |
paul@455 | 708 | time together with an indicator of the nature of the point in time (as a |
paul@455 | 709 | principal point in a time scale or as a repeated point used to terminate |
paul@455 | 710 | events occurring for an instant in time). |
paul@113 | 711 | """ |
paul@113 | 712 | |
paul@113 | 713 | slots = [] |
paul@113 | 714 | active = [] |
paul@113 | 715 | |
paul@162 | 716 | points = scale.items() |
paul@162 | 717 | points.sort() |
paul@162 | 718 | |
paul@162 | 719 | for point, (starting, ending) in points: |
paul@449 | 720 | ending = set(ending) |
paul@449 | 721 | instants = ending.intersection(starting) |
paul@113 | 722 | |
paul@113 | 723 | # Discard all active events ending at or before this start time. |
paul@161 | 724 | # Free up the position in the active list. |
paul@113 | 725 | |
paul@449 | 726 | for t in ending.difference(instants): |
paul@113 | 727 | i = active.index(t) |
paul@113 | 728 | active[i] = None |
paul@113 | 729 | |
paul@161 | 730 | # For each event starting at the current point, fill any newly-vacated |
paul@161 | 731 | # position or add to the end of the active list. |
paul@161 | 732 | |
paul@113 | 733 | for t in starting: |
paul@113 | 734 | try: |
paul@113 | 735 | i = active.index(None) |
paul@113 | 736 | active[i] = t |
paul@113 | 737 | except ValueError: |
paul@113 | 738 | active.append(t) |
paul@113 | 739 | |
paul@161 | 740 | # Discard vacant positions from the end of the active list. |
paul@161 | 741 | |
paul@113 | 742 | while active and active[-1] is None: |
paul@113 | 743 | active.pop() |
paul@113 | 744 | |
paul@452 | 745 | # Add an entry for the time point before "instants". |
paul@452 | 746 | |
paul@455 | 747 | slots.append((Point(point), active[:])) |
paul@113 | 748 | |
paul@449 | 749 | # Discard events ending at the same time as they began. |
paul@449 | 750 | |
paul@449 | 751 | if instants: |
paul@449 | 752 | for t in instants: |
paul@449 | 753 | i = active.index(t) |
paul@449 | 754 | active[i] = None |
paul@449 | 755 | |
paul@449 | 756 | # Discard vacant positions from the end of the active list. |
paul@449 | 757 | |
paul@449 | 758 | while active and active[-1] is None: |
paul@449 | 759 | active.pop() |
paul@449 | 760 | |
paul@452 | 761 | # Add another entry for the time point after "instants". |
paul@449 | 762 | |
paul@455 | 763 | slots.append((Point(point, Point.REPEATED), active[:])) |
paul@449 | 764 | |
paul@113 | 765 | return slots |
paul@113 | 766 | |
paul@244 | 767 | def add_day_start_points(slots, tzid): |
paul@153 | 768 | |
paul@153 | 769 | """ |
paul@162 | 770 | Introduce into the 'slots' any day start points required by multi-day |
paul@244 | 771 | periods. The 'tzid' is required to make sure that appropriate time zones |
paul@244 | 772 | are chosen and not necessarily those provided by the existing time points. |
paul@153 | 773 | """ |
paul@153 | 774 | |
paul@162 | 775 | new_slots = [] |
paul@153 | 776 | current_date = None |
paul@200 | 777 | previously_active = [] |
paul@153 | 778 | |
paul@455 | 779 | for point, active in slots: |
paul@455 | 780 | start_of_day = get_start_of_day(point.point, tzid) |
paul@455 | 781 | this_date = point.point.date() |
paul@153 | 782 | |
paul@198 | 783 | # For each new day, add a slot for the start of the day where periods |
paul@198 | 784 | # are active and where no such slot already exists. |
paul@153 | 785 | |
paul@153 | 786 | if this_date != current_date: |
paul@414 | 787 | |
paul@414 | 788 | # Fill in days where events remain active. |
paul@414 | 789 | |
paul@414 | 790 | if current_date: |
paul@414 | 791 | current_date += timedelta(1) |
paul@414 | 792 | while current_date < this_date: |
paul@455 | 793 | new_slots.append((Point(get_start_of_day(current_date, tzid)), previously_active)) |
paul@414 | 794 | current_date += timedelta(1) |
paul@414 | 795 | else: |
paul@414 | 796 | current_date = this_date |
paul@153 | 797 | |
paul@153 | 798 | # Add any continuing periods. |
paul@153 | 799 | |
paul@455 | 800 | if point.point != start_of_day: |
paul@455 | 801 | new_slots.append((Point(start_of_day), previously_active)) |
paul@153 | 802 | |
paul@153 | 803 | # Add the currently active periods at this point in time. |
paul@153 | 804 | |
paul@153 | 805 | previously_active = active |
paul@153 | 806 | |
paul@162 | 807 | for t in new_slots: |
paul@162 | 808 | insort_left(slots, t) |
paul@162 | 809 | |
paul@931 | 810 | def remove_end_slot(slots, view_period): |
paul@931 | 811 | |
paul@931 | 812 | """ |
paul@931 | 813 | Remove from 'slots' any slot situated at the end of the given 'view_period'. |
paul@931 | 814 | """ |
paul@931 | 815 | |
paul@931 | 816 | end = view_period.get_end_point() |
paul@931 | 817 | if not end or not slots: |
paul@931 | 818 | return |
paul@931 | 819 | i = bisect_left(slots, (Point(end), None)) |
paul@931 | 820 | if i < len(slots): |
paul@931 | 821 | del slots[i:] |
paul@931 | 822 | |
paul@162 | 823 | def add_slots(slots, points): |
paul@162 | 824 | |
paul@162 | 825 | """ |
paul@162 | 826 | Introduce into the 'slots' entries for those in 'points' that are not |
paul@170 | 827 | already present, propagating active periods from time points preceding |
paul@170 | 828 | those added. |
paul@162 | 829 | """ |
paul@162 | 830 | |
paul@162 | 831 | new_slots = [] |
paul@162 | 832 | |
paul@162 | 833 | for point in points: |
paul@452 | 834 | i = bisect_left(slots, (point,)) # slots is [(point, active)...] |
paul@162 | 835 | if i < len(slots) and slots[i][0] == point: |
paul@162 | 836 | continue |
paul@162 | 837 | |
paul@170 | 838 | new_slots.append((point, i > 0 and slots[i-1][1] or [])) |
paul@162 | 839 | |
paul@162 | 840 | for t in new_slots: |
paul@162 | 841 | insort_left(slots, t) |
paul@162 | 842 | |
paul@162 | 843 | def partition_by_day(slots): |
paul@162 | 844 | |
paul@162 | 845 | """ |
paul@162 | 846 | Return a mapping from dates to time points provided by 'slots'. |
paul@162 | 847 | """ |
paul@162 | 848 | |
paul@162 | 849 | d = {} |
paul@162 | 850 | |
paul@455 | 851 | for point, value in slots: |
paul@455 | 852 | day = point.point.date() |
paul@162 | 853 | if not d.has_key(day): |
paul@162 | 854 | d[day] = [] |
paul@455 | 855 | d[day].append((point, value)) |
paul@162 | 856 | |
paul@162 | 857 | return d |
paul@153 | 858 | |
paul@876 | 859 | def add_empty_days(days, tzid, start=None, end=None): |
paul@279 | 860 | |
paul@876 | 861 | """ |
paul@876 | 862 | Add empty days to 'days' between busy days, and optionally from the given |
paul@876 | 863 | 'start' day and until the given 'end' day. |
paul@876 | 864 | """ |
paul@279 | 865 | |
paul@888 | 866 | last_day = start - timedelta(1) |
paul@279 | 867 | all_days = days.keys() |
paul@279 | 868 | all_days.sort() |
paul@279 | 869 | |
paul@279 | 870 | for day in all_days: |
paul@279 | 871 | if last_day: |
paul@279 | 872 | empty_day = last_day + timedelta(1) |
paul@279 | 873 | while empty_day < day: |
paul@455 | 874 | days[empty_day] = [(Point(get_start_of_day(empty_day, tzid)), None)] |
paul@279 | 875 | empty_day += timedelta(1) |
paul@876 | 876 | last_day = day |
paul@876 | 877 | |
paul@876 | 878 | if end: |
paul@876 | 879 | empty_day = last_day + timedelta(1) |
paul@876 | 880 | while empty_day < end: |
paul@876 | 881 | days[empty_day] = [(Point(get_start_of_day(empty_day, tzid)), None)] |
paul@876 | 882 | empty_day += timedelta(1) |
paul@279 | 883 | |
paul@114 | 884 | def get_spans(slots): |
paul@114 | 885 | |
paul@533 | 886 | "Inspect the given 'slots', returning a mapping of period keys to spans." |
paul@114 | 887 | |
paul@455 | 888 | points = [point for point, active in slots] |
paul@114 | 889 | spans = {} |
paul@114 | 890 | |
paul@449 | 891 | for _point, active in slots: |
paul@458 | 892 | for p in active: |
paul@458 | 893 | if p: |
paul@458 | 894 | key = p.get_key() |
paul@529 | 895 | start_slot = bisect_left(points, p.get_start()) |
paul@529 | 896 | end_slot = bisect_left(points, p.get_end()) |
paul@185 | 897 | spans[key] = end_slot - start_slot |
paul@114 | 898 | |
paul@114 | 899 | return spans |
paul@114 | 900 | |
paul@740 | 901 | def update_freebusy(freebusy, periods, transp, uid, recurrenceid, summary, organiser, expires=None): |
paul@250 | 902 | |
paul@250 | 903 | """ |
paul@395 | 904 | Update the free/busy details with the given 'periods', 'transp' setting, |
paul@395 | 905 | 'uid' plus 'recurrenceid' and 'summary' and 'organiser' details. |
paul@740 | 906 | |
paul@740 | 907 | An optional 'expires' datetime string indicates the expiry time of any |
paul@740 | 908 | free/busy offer. |
paul@250 | 909 | """ |
paul@250 | 910 | |
paul@343 | 911 | remove_period(freebusy, uid, recurrenceid) |
paul@250 | 912 | |
paul@458 | 913 | for p in periods: |
paul@740 | 914 | insert_period(freebusy, FreeBusyPeriod(p.get_start_point(), p.get_end_point(), uid, transp, recurrenceid, summary, organiser, expires)) |
paul@250 | 915 | |
paul@48 | 916 | # vim: tabstop=4 expandtab shiftwidth=4 |