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