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