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