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