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