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@1043 | 6 | Copyright (C) 2014, 2015, 2016 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@944 | 90 | def __hash__(self): |
paul@944 | 91 | return 0 |
paul@944 | 92 | |
paul@874 | 93 | class EndOfTime(PointInTime): |
paul@874 | 94 | |
paul@874 | 95 | "A special value that compares later than other values." |
paul@874 | 96 | |
paul@874 | 97 | def __cmp__(self, other): |
paul@874 | 98 | if isinstance(other, EndOfTime): |
paul@874 | 99 | return 0 |
paul@874 | 100 | else: |
paul@874 | 101 | return 1 |
paul@874 | 102 | |
paul@874 | 103 | def __rcmp__(self, other): |
paul@874 | 104 | return -self.__cmp__(other) |
paul@874 | 105 | |
paul@887 | 106 | def __nonzero__(self): |
paul@887 | 107 | return False |
paul@887 | 108 | |
paul@944 | 109 | def __hash__(self): |
paul@944 | 110 | return 0 |
paul@944 | 111 | |
paul@944 | 112 | class Endless: |
paul@944 | 113 | |
paul@944 | 114 | "A special value indicating an endless period." |
paul@944 | 115 | |
paul@944 | 116 | def __cmp__(self, other): |
paul@944 | 117 | if isinstance(other, Endless): |
paul@944 | 118 | return 0 |
paul@944 | 119 | else: |
paul@944 | 120 | return 1 |
paul@944 | 121 | |
paul@944 | 122 | def __rcmp__(self, other): |
paul@944 | 123 | return -self.__cmp__(other) |
paul@944 | 124 | |
paul@944 | 125 | def __nonzero__(self): |
paul@944 | 126 | return True |
paul@944 | 127 | |
paul@646 | 128 | class PeriodBase: |
paul@458 | 129 | |
paul@458 | 130 | "A basic period abstraction." |
paul@458 | 131 | |
paul@944 | 132 | def __init__(self, start, end): |
paul@944 | 133 | if isinstance(start, (date, PointInTime)): |
paul@944 | 134 | self.start = start |
paul@944 | 135 | else: |
paul@944 | 136 | self.start = get_datetime(start) or StartOfTime() |
paul@944 | 137 | if isinstance(end, (date, PointInTime)): |
paul@944 | 138 | self.end = end |
paul@944 | 139 | else: |
paul@944 | 140 | self.end = get_datetime(end) or EndOfTime() |
paul@944 | 141 | |
paul@646 | 142 | def as_tuple(self): |
paul@646 | 143 | return self.start, self.end |
paul@646 | 144 | |
paul@646 | 145 | def __hash__(self): |
paul@646 | 146 | return hash((self.get_start(), self.get_end())) |
paul@646 | 147 | |
paul@646 | 148 | def __cmp__(self, other): |
paul@646 | 149 | |
paul@646 | 150 | "Return a comparison result against 'other' using points in time." |
paul@646 | 151 | |
paul@646 | 152 | if isinstance(other, PeriodBase): |
paul@646 | 153 | return cmp( |
paul@924 | 154 | (Comparable(ifnone(self.get_start_point(), StartOfTime())), Comparable(ifnone(self.get_end_point(), EndOfTime()))), |
paul@924 | 155 | (Comparable(ifnone(other.get_start_point(), StartOfTime())), Comparable(ifnone(other.get_end_point(), EndOfTime()))) |
paul@646 | 156 | ) |
paul@646 | 157 | else: |
paul@646 | 158 | return 1 |
paul@646 | 159 | |
paul@874 | 160 | def overlaps(self, other): |
paul@924 | 161 | return Comparable(ifnone(self.get_end_point(), EndOfTime())) > Comparable(ifnone(other.get_start_point(), StartOfTime())) and \ |
paul@924 | 162 | Comparable(ifnone(self.get_start_point(), StartOfTime())) < Comparable(ifnone(other.get_end_point(), EndOfTime())) |
paul@874 | 163 | |
paul@941 | 164 | def within(self, other): |
paul@941 | 165 | return Comparable(ifnone(self.get_start_point(), StartOfTime())) >= Comparable(ifnone(other.get_start_point(), StartOfTime())) and \ |
paul@941 | 166 | Comparable(ifnone(self.get_end_point(), EndOfTime())) <= Comparable(ifnone(other.get_end_point(), EndOfTime())) |
paul@941 | 167 | |
paul@944 | 168 | def common(self, other): |
paul@944 | 169 | start = max(Comparable(ifnone(self.get_start_point(), StartOfTime())), Comparable(ifnone(other.get_start_point(), StartOfTime()))) |
paul@944 | 170 | end = min(Comparable(ifnone(self.get_end_point(), EndOfTime())), Comparable(ifnone(other.get_end_point(), EndOfTime()))) |
paul@944 | 171 | if start <= end: |
paul@944 | 172 | return self.make_corrected(start.dt, end.dt) |
paul@944 | 173 | else: |
paul@944 | 174 | return None |
paul@944 | 175 | |
paul@646 | 176 | def get_key(self): |
paul@646 | 177 | return self.get_start(), self.get_end() |
paul@646 | 178 | |
paul@646 | 179 | # Datetime and metadata methods. |
paul@646 | 180 | |
paul@646 | 181 | def get_start(self): |
paul@646 | 182 | return self.start |
paul@646 | 183 | |
paul@646 | 184 | def get_end(self): |
paul@646 | 185 | return self.end |
paul@646 | 186 | |
paul@646 | 187 | def get_start_attr(self): |
paul@646 | 188 | return get_datetime_attributes(self.start, self.tzid) |
paul@646 | 189 | |
paul@646 | 190 | def get_end_attr(self): |
paul@646 | 191 | return get_datetime_attributes(self.end, self.tzid) |
paul@646 | 192 | |
paul@646 | 193 | def get_start_item(self): |
paul@646 | 194 | return self.get_start(), self.get_start_attr() |
paul@646 | 195 | |
paul@646 | 196 | def get_end_item(self): |
paul@646 | 197 | return self.get_end(), self.get_end_attr() |
paul@646 | 198 | |
paul@646 | 199 | def get_start_point(self): |
paul@646 | 200 | return self.start |
paul@646 | 201 | |
paul@646 | 202 | def get_end_point(self): |
paul@646 | 203 | return self.end |
paul@646 | 204 | |
paul@659 | 205 | def get_duration(self): |
paul@944 | 206 | start = self.get_start_point() |
paul@944 | 207 | end = self.get_end_point() |
paul@944 | 208 | if start and end: |
paul@944 | 209 | return end - start |
paul@944 | 210 | else: |
paul@944 | 211 | return Endless() |
paul@659 | 212 | |
paul@646 | 213 | class Period(PeriodBase): |
paul@646 | 214 | |
paul@646 | 215 | "A simple period abstraction." |
paul@646 | 216 | |
paul@541 | 217 | def __init__(self, start, end, tzid=None, origin=None): |
paul@541 | 218 | |
paul@541 | 219 | """ |
paul@541 | 220 | Initialise a period with the given 'start' and 'end', having a |
paul@541 | 221 | contextual 'tzid', if specified, and an indicated 'origin'. |
paul@620 | 222 | |
paul@620 | 223 | All metadata from the start and end points are derived from the supplied |
paul@620 | 224 | dates/datetimes. |
paul@541 | 225 | """ |
paul@541 | 226 | |
paul@944 | 227 | PeriodBase.__init__(self, start, end) |
paul@541 | 228 | self.tzid = tzid |
paul@528 | 229 | self.origin = origin |
paul@458 | 230 | |
paul@458 | 231 | def as_tuple(self): |
paul@541 | 232 | return self.start, self.end, self.tzid, self.origin |
paul@458 | 233 | |
paul@458 | 234 | def __repr__(self): |
paul@630 | 235 | return "Period%r" % (self.as_tuple(),) |
paul@458 | 236 | |
paul@646 | 237 | # Datetime and metadata methods. |
paul@528 | 238 | |
paul@541 | 239 | def get_tzid(self): |
paul@620 | 240 | return get_tzid(self.get_start_attr(), self.get_end_attr()) or self.tzid |
paul@620 | 241 | |
paul@557 | 242 | def get_start_point(self): |
paul@874 | 243 | start = self.get_start() |
paul@924 | 244 | if isinstance(start, PointInTime): return start |
paul@924 | 245 | else: return to_utc_datetime(start, self.get_tzid()) |
paul@557 | 246 | |
paul@557 | 247 | def get_end_point(self): |
paul@874 | 248 | end = self.get_end() |
paul@924 | 249 | if isinstance(end, PointInTime): return end |
paul@924 | 250 | else: return to_utc_datetime(end, self.get_tzid()) |
paul@557 | 251 | |
paul@633 | 252 | # Period and event recurrence logic. |
paul@633 | 253 | |
paul@633 | 254 | def is_replaced(self, recurrenceids): |
paul@633 | 255 | |
paul@633 | 256 | """ |
paul@633 | 257 | Return whether this period refers to one of the 'recurrenceids'. |
paul@633 | 258 | The 'recurrenceids' should be normalised to UTC datetimes according to |
paul@633 | 259 | time zone information provided by their objects or be floating dates or |
paul@633 | 260 | datetimes requiring conversion using contextual time zone information. |
paul@633 | 261 | """ |
paul@633 | 262 | |
paul@633 | 263 | for recurrenceid in recurrenceids: |
paul@647 | 264 | if self.is_affected(recurrenceid): |
paul@633 | 265 | return recurrenceid |
paul@633 | 266 | return None |
paul@633 | 267 | |
paul@633 | 268 | def is_affected(self, recurrenceid): |
paul@633 | 269 | |
paul@633 | 270 | """ |
paul@633 | 271 | Return whether this period refers to 'recurrenceid'. The 'recurrenceid' |
paul@633 | 272 | should be normalised to UTC datetimes according to time zone information |
paul@633 | 273 | provided by their objects. Otherwise, this period's contextual time zone |
paul@633 | 274 | information is used to convert any date or floating datetime |
paul@633 | 275 | representation to a point in time. |
paul@633 | 276 | """ |
paul@633 | 277 | |
paul@633 | 278 | if not recurrenceid: |
paul@633 | 279 | return None |
paul@633 | 280 | d = get_recurrence_start(recurrenceid) |
paul@633 | 281 | dt = get_recurrence_start_point(recurrenceid, self.tzid) |
paul@633 | 282 | if self.get_start() == d or self.get_start_point() == dt: |
paul@633 | 283 | return recurrenceid |
paul@633 | 284 | return None |
paul@633 | 285 | |
paul@939 | 286 | # Value correction methods. |
paul@939 | 287 | |
paul@941 | 288 | def with_duration(self, duration): |
paul@939 | 289 | |
paul@941 | 290 | """ |
paul@941 | 291 | Return a version of this period with the same start point but with the |
paul@941 | 292 | given 'duration'. |
paul@941 | 293 | """ |
paul@941 | 294 | |
paul@941 | 295 | return self.make_corrected(self.get_start(), self.get_start() + duration) |
paul@941 | 296 | |
paul@941 | 297 | def check_permitted(self, permitted_values): |
paul@941 | 298 | |
paul@941 | 299 | "Check the period against the given 'permitted_values'." |
paul@939 | 300 | |
paul@939 | 301 | start = self.get_start() |
paul@939 | 302 | end = self.get_end() |
paul@939 | 303 | start_errors = check_permitted_values(start, permitted_values) |
paul@939 | 304 | end_errors = check_permitted_values(end, permitted_values) |
paul@939 | 305 | |
paul@939 | 306 | if not (start_errors or end_errors): |
paul@941 | 307 | return None |
paul@941 | 308 | |
paul@941 | 309 | return start_errors, end_errors |
paul@941 | 310 | |
paul@941 | 311 | def get_corrected(self, permitted_values): |
paul@941 | 312 | |
paul@941 | 313 | "Return a corrected version of this period." |
paul@941 | 314 | |
paul@941 | 315 | errors = self.check_permitted(permitted_values) |
paul@941 | 316 | |
paul@941 | 317 | if not errors: |
paul@939 | 318 | return self |
paul@939 | 319 | |
paul@941 | 320 | start_errors, end_errors = errors |
paul@941 | 321 | |
paul@952 | 322 | start = self.get_start() |
paul@952 | 323 | end = self.get_end() |
paul@952 | 324 | |
paul@939 | 325 | if start_errors: |
paul@939 | 326 | start = correct_datetime(start, permitted_values) |
paul@939 | 327 | if end_errors: |
paul@939 | 328 | end = correct_datetime(end, permitted_values) |
paul@939 | 329 | |
paul@939 | 330 | return self.make_corrected(start, end) |
paul@939 | 331 | |
paul@939 | 332 | def make_corrected(self, start, end): |
paul@939 | 333 | return self.__class__(start, end, self.tzid, self.origin) |
paul@939 | 334 | |
paul@646 | 335 | class FreeBusyPeriod(PeriodBase): |
paul@458 | 336 | |
paul@458 | 337 | "A free/busy record abstraction." |
paul@458 | 338 | |
paul@710 | 339 | def __init__(self, start, end, uid=None, transp=None, recurrenceid=None, summary=None, organiser=None, expires=None): |
paul@529 | 340 | |
paul@529 | 341 | """ |
paul@643 | 342 | Initialise a free/busy period with the given 'start' and 'end' points, |
paul@646 | 343 | plus any 'uid', 'transp', 'recurrenceid', 'summary' and 'organiser' |
paul@646 | 344 | details. |
paul@710 | 345 | |
paul@710 | 346 | An additional 'expires' parameter can be used to indicate an expiry |
paul@710 | 347 | datetime in conjunction with free/busy offers made when countering |
paul@710 | 348 | event proposals. |
paul@529 | 349 | """ |
paul@529 | 350 | |
paul@944 | 351 | PeriodBase.__init__(self, start, end) |
paul@458 | 352 | self.uid = uid |
paul@1066 | 353 | self.transp = transp or None |
paul@1066 | 354 | self.recurrenceid = recurrenceid or None |
paul@1066 | 355 | self.summary = summary or None |
paul@1066 | 356 | self.organiser = organiser or None |
paul@1066 | 357 | self.expires = expires or None |
paul@458 | 358 | |
paul@1074 | 359 | def as_tuple(self, strings_only=False, string_datetimes=False): |
paul@563 | 360 | |
paul@563 | 361 | """ |
paul@1074 | 362 | Return the initialisation parameter tuple, converting datetimes and |
paul@1074 | 363 | false value parameters to strings if 'strings_only' is set to a true |
paul@1074 | 364 | value. Otherwise, if 'string_datetimes' is set to a true value, only the |
paul@1074 | 365 | datetime values are converted to strings. |
paul@563 | 366 | """ |
paul@563 | 367 | |
paul@563 | 368 | null = lambda x: (strings_only and [""] or [x])[0] |
paul@563 | 369 | return ( |
paul@1074 | 370 | (strings_only or string_datetimes) and format_datetime(self.get_start_point()) or self.start, |
paul@1074 | 371 | (strings_only or string_datetimes) and format_datetime(self.get_end_point()) or self.end, |
paul@563 | 372 | self.uid or null(self.uid), |
paul@563 | 373 | self.transp or strings_only and "OPAQUE" or None, |
paul@563 | 374 | self.recurrenceid or null(self.recurrenceid), |
paul@563 | 375 | self.summary or null(self.summary), |
paul@710 | 376 | self.organiser or null(self.organiser), |
paul@710 | 377 | self.expires or null(self.expires) |
paul@563 | 378 | ) |
paul@458 | 379 | |
paul@458 | 380 | def __cmp__(self, other): |
paul@541 | 381 | |
paul@541 | 382 | """ |
paul@541 | 383 | Compare this object to 'other', employing the uid if the periods |
paul@541 | 384 | involved are the same. |
paul@541 | 385 | """ |
paul@541 | 386 | |
paul@646 | 387 | result = PeriodBase.__cmp__(self, other) |
paul@541 | 388 | if result == 0 and isinstance(other, FreeBusyPeriod): |
paul@653 | 389 | return cmp((self.uid, self.recurrenceid), (other.uid, other.recurrenceid)) |
paul@458 | 390 | else: |
paul@541 | 391 | return result |
paul@458 | 392 | |
paul@458 | 393 | def get_key(self): |
paul@541 | 394 | return self.uid, self.recurrenceid, self.get_start() |
paul@458 | 395 | |
paul@458 | 396 | def __repr__(self): |
paul@630 | 397 | return "FreeBusyPeriod%r" % (self.as_tuple(),) |
paul@458 | 398 | |
paul@944 | 399 | def get_tzid(self): |
paul@944 | 400 | return "UTC" |
paul@944 | 401 | |
paul@679 | 402 | # Period and event recurrence logic. |
paul@679 | 403 | |
paul@679 | 404 | def is_replaced(self, recurrences): |
paul@679 | 405 | |
paul@679 | 406 | """ |
paul@679 | 407 | Return whether this period refers to one of the 'recurrences'. |
paul@679 | 408 | The 'recurrences' must be UTC datetimes corresponding to the start of |
paul@679 | 409 | the period described by a recurrence. |
paul@679 | 410 | """ |
paul@679 | 411 | |
paul@679 | 412 | for recurrence in recurrences: |
paul@679 | 413 | if self.is_affected(recurrence): |
paul@679 | 414 | return True |
paul@679 | 415 | return False |
paul@679 | 416 | |
paul@679 | 417 | def is_affected(self, recurrence): |
paul@679 | 418 | |
paul@679 | 419 | """ |
paul@679 | 420 | Return whether this period refers to 'recurrence'. The 'recurrence' must |
paul@679 | 421 | be a UTC datetime corresponding to the start of the period described by |
paul@679 | 422 | a recurrence. |
paul@679 | 423 | """ |
paul@679 | 424 | |
paul@679 | 425 | return recurrence and self.get_start_point() == recurrence |
paul@679 | 426 | |
paul@944 | 427 | # Value correction methods. |
paul@944 | 428 | |
paul@944 | 429 | def make_corrected(self, start, end): |
paul@944 | 430 | return self.__class__(start, end) |
paul@944 | 431 | |
paul@543 | 432 | class RecurringPeriod(Period): |
paul@543 | 433 | |
paul@620 | 434 | """ |
paul@620 | 435 | A period with iCalendar metadata attributes and origin information from an |
paul@620 | 436 | object. |
paul@620 | 437 | """ |
paul@543 | 438 | |
paul@543 | 439 | def __init__(self, start, end, tzid=None, origin=None, start_attr=None, end_attr=None): |
paul@543 | 440 | Period.__init__(self, start, end, tzid, origin) |
paul@543 | 441 | self.start_attr = start_attr |
paul@543 | 442 | self.end_attr = end_attr |
paul@543 | 443 | |
paul@620 | 444 | def get_start_attr(self): |
paul@620 | 445 | return self.start_attr |
paul@543 | 446 | |
paul@620 | 447 | def get_end_attr(self): |
paul@620 | 448 | return self.end_attr |
paul@543 | 449 | |
paul@543 | 450 | def as_tuple(self): |
paul@543 | 451 | return self.start, self.end, self.tzid, self.origin, self.start_attr, self.end_attr |
paul@543 | 452 | |
paul@543 | 453 | def __repr__(self): |
paul@630 | 454 | return "RecurringPeriod%r" % (self.as_tuple(),) |
paul@543 | 455 | |
paul@939 | 456 | def make_corrected(self, start, end): |
paul@939 | 457 | return self.__class__(start, end, self.tzid, self.origin, self.get_start_attr(), self.get_end_attr()) |
paul@939 | 458 | |
paul@1063 | 459 | class FreeBusyCollectionBase: |
paul@1062 | 460 | |
paul@1063 | 461 | "Common operations on free/busy period collections." |
paul@1062 | 462 | |
paul@1071 | 463 | def __init__(self, mutable=True): |
paul@1071 | 464 | self.mutable = mutable |
paul@1071 | 465 | |
paul@1071 | 466 | def _check_mutable(self): |
paul@1071 | 467 | if not self.mutable: |
paul@1071 | 468 | raise TypeError, "Cannot mutate this collection." |
paul@1071 | 469 | |
paul@1071 | 470 | def copy(self): |
paul@1071 | 471 | |
paul@1071 | 472 | "Make an independent mutable copy of the collection." |
paul@1071 | 473 | |
paul@1071 | 474 | return FreeBusyCollection(list(self), True) |
paul@1071 | 475 | |
paul@1062 | 476 | # List emulation methods. |
paul@48 | 477 | |
paul@1062 | 478 | def __iadd__(self, other): |
paul@1062 | 479 | for period in other: |
paul@1062 | 480 | self.insert_period(period) |
paul@1062 | 481 | return self |
paul@1062 | 482 | |
paul@1062 | 483 | # Operations. |
paul@221 | 484 | |
paul@1062 | 485 | def can_schedule(self, periods, uid, recurrenceid): |
paul@1062 | 486 | |
paul@1062 | 487 | """ |
paul@1062 | 488 | Return whether the collection can accommodate the given 'periods' |
paul@1062 | 489 | employing the specified 'uid' and 'recurrenceid'. |
paul@1062 | 490 | """ |
paul@221 | 491 | |
paul@1062 | 492 | for conflict in self.have_conflict(periods, True): |
paul@1062 | 493 | if conflict.uid != uid or conflict.recurrenceid != recurrenceid: |
paul@1062 | 494 | return False |
paul@1062 | 495 | |
paul@1062 | 496 | return True |
paul@1062 | 497 | |
paul@1062 | 498 | def have_conflict(self, periods, get_conflicts=False): |
paul@221 | 499 | |
paul@1062 | 500 | """ |
paul@1062 | 501 | Return whether any period in the collection overlaps with the given |
paul@1062 | 502 | 'periods', returning a collection of such overlapping periods if |
paul@1062 | 503 | 'get_conflicts' is set to a true value. |
paul@1062 | 504 | """ |
paul@1062 | 505 | |
paul@1062 | 506 | conflicts = set() |
paul@1062 | 507 | for p in periods: |
paul@1062 | 508 | overlapping = self.period_overlaps(p, get_conflicts) |
paul@1062 | 509 | if overlapping: |
paul@1062 | 510 | if get_conflicts: |
paul@1062 | 511 | conflicts.update(overlapping) |
paul@1062 | 512 | else: |
paul@1062 | 513 | return True |
paul@1062 | 514 | |
paul@1062 | 515 | if get_conflicts: |
paul@1062 | 516 | return conflicts |
paul@1062 | 517 | else: |
paul@221 | 518 | return False |
paul@221 | 519 | |
paul@1063 | 520 | def period_overlaps(self, period, get_periods=False): |
paul@1063 | 521 | |
paul@1063 | 522 | """ |
paul@1063 | 523 | Return whether any period in the collection overlaps with the given |
paul@1063 | 524 | 'period', returning a collection of overlapping periods if 'get_periods' |
paul@1063 | 525 | is set to a true value. |
paul@1063 | 526 | """ |
paul@1063 | 527 | |
paul@1063 | 528 | overlapping = self.get_overlapping(period) |
paul@1063 | 529 | |
paul@1063 | 530 | if get_periods: |
paul@1063 | 531 | return overlapping |
paul@1063 | 532 | else: |
paul@1063 | 533 | return len(overlapping) != 0 |
paul@1063 | 534 | |
paul@1063 | 535 | def replace_overlapping(self, period, replacements): |
paul@1063 | 536 | |
paul@1063 | 537 | """ |
paul@1063 | 538 | Replace existing periods in the collection within the given 'period', |
paul@1063 | 539 | using the given 'replacements'. |
paul@1063 | 540 | """ |
paul@1063 | 541 | |
paul@1071 | 542 | self._check_mutable() |
paul@1071 | 543 | |
paul@1063 | 544 | self.remove_overlapping(period) |
paul@1063 | 545 | for replacement in replacements: |
paul@1063 | 546 | self.insert_period(replacement) |
paul@1063 | 547 | |
paul@1063 | 548 | def coalesce_freebusy(self): |
paul@1063 | 549 | |
paul@1063 | 550 | "Coalesce the periods in the collection, returning a new collection." |
paul@1063 | 551 | |
paul@1063 | 552 | if not self: |
paul@1063 | 553 | return FreeBusyCollection() |
paul@1063 | 554 | |
paul@1063 | 555 | fb = [] |
paul@1063 | 556 | |
paul@1063 | 557 | it = iter(self) |
paul@1063 | 558 | period = it.next() |
paul@1063 | 559 | |
paul@1063 | 560 | start = period.get_start_point() |
paul@1063 | 561 | end = period.get_end_point() |
paul@1063 | 562 | |
paul@1063 | 563 | try: |
paul@1063 | 564 | while True: |
paul@1063 | 565 | period = it.next() |
paul@1063 | 566 | if period.get_start_point() > end: |
paul@1063 | 567 | fb.append(FreeBusyPeriod(start, end)) |
paul@1063 | 568 | start = period.get_start_point() |
paul@1063 | 569 | end = period.get_end_point() |
paul@1063 | 570 | else: |
paul@1063 | 571 | end = max(end, period.get_end_point()) |
paul@1063 | 572 | except StopIteration: |
paul@1063 | 573 | pass |
paul@1063 | 574 | |
paul@1063 | 575 | fb.append(FreeBusyPeriod(start, end)) |
paul@1063 | 576 | return FreeBusyCollection(fb) |
paul@1063 | 577 | |
paul@1063 | 578 | def invert_freebusy(self): |
paul@1063 | 579 | |
paul@1063 | 580 | "Return the free periods from the collection as a new collection." |
paul@1063 | 581 | |
paul@1063 | 582 | if not self: |
paul@1063 | 583 | return FreeBusyCollection([FreeBusyPeriod(None, None)]) |
paul@1063 | 584 | |
paul@1063 | 585 | # Coalesce periods that overlap or are adjacent. |
paul@1063 | 586 | |
paul@1063 | 587 | fb = self.coalesce_freebusy() |
paul@1063 | 588 | free = [] |
paul@1063 | 589 | |
paul@1063 | 590 | # Add a start-of-time period if appropriate. |
paul@1063 | 591 | |
paul@1063 | 592 | first = fb[0].get_start_point() |
paul@1063 | 593 | if first: |
paul@1063 | 594 | free.append(FreeBusyPeriod(None, first)) |
paul@1063 | 595 | |
paul@1063 | 596 | start = fb[0].get_end_point() |
paul@1063 | 597 | |
paul@1063 | 598 | for period in fb[1:]: |
paul@1063 | 599 | free.append(FreeBusyPeriod(start, period.get_start_point())) |
paul@1063 | 600 | start = period.get_end_point() |
paul@1063 | 601 | |
paul@1063 | 602 | # Add an end-of-time period if appropriate. |
paul@1063 | 603 | |
paul@1063 | 604 | if start: |
paul@1063 | 605 | free.append(FreeBusyPeriod(start, None)) |
paul@1063 | 606 | |
paul@1063 | 607 | return FreeBusyCollection(free) |
paul@1063 | 608 | |
paul@1063 | 609 | def update_freebusy(self, periods, transp, uid, recurrenceid, summary, organiser, expires=None): |
paul@1063 | 610 | |
paul@1063 | 611 | """ |
paul@1063 | 612 | Update the free/busy details with the given 'periods', 'transp' setting, |
paul@1063 | 613 | 'uid' plus 'recurrenceid' and 'summary' and 'organiser' details. |
paul@1063 | 614 | |
paul@1063 | 615 | An optional 'expires' datetime string indicates the expiry time of any |
paul@1063 | 616 | free/busy offer. |
paul@1063 | 617 | """ |
paul@1063 | 618 | |
paul@1071 | 619 | self._check_mutable() |
paul@1071 | 620 | |
paul@1063 | 621 | self.remove_event_periods(uid, recurrenceid) |
paul@1063 | 622 | |
paul@1063 | 623 | for p in periods: |
paul@1063 | 624 | self.insert_period(FreeBusyPeriod(p.get_start_point(), p.get_end_point(), uid, transp, recurrenceid, summary, organiser, expires)) |
paul@1063 | 625 | |
paul@1063 | 626 | class FreeBusyCollection(FreeBusyCollectionBase): |
paul@1063 | 627 | |
paul@1063 | 628 | "An abstraction for a collection of free/busy periods." |
paul@1063 | 629 | |
paul@1071 | 630 | def __init__(self, periods=None, mutable=True): |
paul@1063 | 631 | |
paul@1063 | 632 | """ |
paul@1063 | 633 | Initialise the collection with the given list of 'periods', or start an |
paul@1063 | 634 | empty collection if no list is given. |
paul@1063 | 635 | """ |
paul@1063 | 636 | |
paul@1071 | 637 | FreeBusyCollectionBase.__init__(self, mutable) |
paul@1063 | 638 | self.periods = periods or [] |
paul@1063 | 639 | |
paul@1063 | 640 | # List emulation methods. |
paul@1063 | 641 | |
paul@1063 | 642 | def __nonzero__(self): |
paul@1063 | 643 | return bool(self.periods) |
paul@1063 | 644 | |
paul@1063 | 645 | def __iter__(self): |
paul@1063 | 646 | return iter(self.periods) |
paul@1063 | 647 | |
paul@1063 | 648 | def __len__(self): |
paul@1063 | 649 | return len(self.periods) |
paul@1063 | 650 | |
paul@1063 | 651 | def __getitem__(self, i): |
paul@1063 | 652 | return self.periods[i] |
paul@1063 | 653 | |
paul@1063 | 654 | # Operations. |
paul@1063 | 655 | |
paul@1062 | 656 | def insert_period(self, period): |
paul@221 | 657 | |
paul@1062 | 658 | "Insert the given 'period' into the collection." |
paul@72 | 659 | |
paul@1071 | 660 | self._check_mutable() |
paul@1071 | 661 | |
paul@1062 | 662 | i = bisect_left(self.periods, period) |
paul@1062 | 663 | if i == len(self.periods): |
paul@1062 | 664 | self.periods.append(period) |
paul@1062 | 665 | elif self.periods[i] != period: |
paul@1062 | 666 | self.periods.insert(i, period) |
paul@1062 | 667 | |
paul@1062 | 668 | def remove_periods(self, periods): |
paul@72 | 669 | |
paul@1062 | 670 | "Remove the given 'periods' from the collection." |
paul@221 | 671 | |
paul@1071 | 672 | self._check_mutable() |
paul@1071 | 673 | |
paul@1062 | 674 | for period in periods: |
paul@1062 | 675 | i = bisect_left(self.periods, period) |
paul@1062 | 676 | if i < len(self.periods) and self.periods[i] == period: |
paul@1062 | 677 | del self.periods[i] |
paul@74 | 678 | |
paul@1062 | 679 | def remove_event_periods(self, uid, recurrenceid=None): |
paul@72 | 680 | |
paul@1062 | 681 | """ |
paul@1062 | 682 | Remove from the collection all periods associated with 'uid' and |
paul@1062 | 683 | 'recurrenceid' (which if omitted causes the "parent" object's periods to |
paul@1062 | 684 | be referenced). |
paul@362 | 685 | |
paul@1062 | 686 | Return the removed periods. |
paul@1062 | 687 | """ |
paul@957 | 688 | |
paul@1071 | 689 | self._check_mutable() |
paul@1071 | 690 | |
paul@1062 | 691 | removed = [] |
paul@1062 | 692 | i = 0 |
paul@1062 | 693 | while i < len(self.periods): |
paul@1062 | 694 | fb = self.periods[i] |
paul@1062 | 695 | if fb.uid == uid and fb.recurrenceid == recurrenceid: |
paul@1062 | 696 | removed.append(self.periods[i]) |
paul@1062 | 697 | del self.periods[i] |
paul@1062 | 698 | else: |
paul@1062 | 699 | i += 1 |
paul@362 | 700 | |
paul@1062 | 701 | return removed |
paul@957 | 702 | |
paul@1062 | 703 | def remove_additional_periods(self, uid, recurrenceids=None): |
paul@362 | 704 | |
paul@1062 | 705 | """ |
paul@1062 | 706 | Remove from the collection all periods associated with 'uid' having a |
paul@1062 | 707 | recurrence identifier indicating an additional or modified period. |
paul@48 | 708 | |
paul@1062 | 709 | If 'recurrenceids' is specified, remove all periods associated with |
paul@1062 | 710 | 'uid' that do not have a recurrence identifier in the given list. |
paul@1062 | 711 | |
paul@1062 | 712 | Return the removed periods. |
paul@1062 | 713 | """ |
paul@523 | 714 | |
paul@1071 | 715 | self._check_mutable() |
paul@1071 | 716 | |
paul@1062 | 717 | removed = [] |
paul@1062 | 718 | i = 0 |
paul@1062 | 719 | while i < len(self.periods): |
paul@1062 | 720 | fb = self.periods[i] |
paul@1062 | 721 | if fb.uid == uid and fb.recurrenceid and ( |
paul@1062 | 722 | recurrenceids is None or |
paul@1062 | 723 | recurrenceids is not None and fb.recurrenceid not in recurrenceids |
paul@1062 | 724 | ): |
paul@1062 | 725 | removed.append(self.periods[i]) |
paul@1062 | 726 | del self.periods[i] |
paul@1062 | 727 | else: |
paul@1062 | 728 | i += 1 |
paul@382 | 729 | |
paul@1062 | 730 | return removed |
paul@1043 | 731 | |
paul@1062 | 732 | def remove_affected_period(self, uid, start): |
paul@381 | 733 | |
paul@1062 | 734 | """ |
paul@1062 | 735 | Remove from the collection the period associated with 'uid' that |
paul@1062 | 736 | provides an occurrence starting at the given 'start' (provided by a |
paul@1062 | 737 | recurrence identifier, converted to a datetime). A recurrence identifier |
paul@1062 | 738 | is used to provide an alternative time period whilst also acting as a |
paul@1062 | 739 | reference to the originally-defined occurrence. |
paul@1062 | 740 | |
paul@1062 | 741 | Return any removed period in a list. |
paul@1062 | 742 | """ |
paul@381 | 743 | |
paul@1071 | 744 | self._check_mutable() |
paul@1071 | 745 | |
paul@1062 | 746 | removed = [] |
paul@1062 | 747 | |
paul@1062 | 748 | search = Period(start, start) |
paul@1062 | 749 | found = bisect_left(self.periods, search) |
paul@1043 | 750 | |
paul@1062 | 751 | while found < len(self.periods): |
paul@1062 | 752 | fb = self.periods[found] |
paul@1062 | 753 | |
paul@1062 | 754 | # Stop looking if the start no longer matches the recurrence identifier. |
paul@362 | 755 | |
paul@1062 | 756 | if fb.get_start_point() != search.get_start_point(): |
paul@1062 | 757 | break |
paul@1062 | 758 | |
paul@1062 | 759 | # If the period belongs to the parent object, remove it and return. |
paul@1043 | 760 | |
paul@1062 | 761 | if not fb.recurrenceid and uid == fb.uid: |
paul@1062 | 762 | removed.append(self.periods[found]) |
paul@1062 | 763 | del self.periods[found] |
paul@1062 | 764 | break |
paul@1043 | 765 | |
paul@1062 | 766 | # Otherwise, keep looking for a matching period. |
paul@1062 | 767 | |
paul@1062 | 768 | found += 1 |
paul@558 | 769 | |
paul@1062 | 770 | return removed |
paul@1062 | 771 | |
paul@1062 | 772 | def periods_from(self, period): |
paul@376 | 773 | |
paul@1062 | 774 | "Return the entries in the collection at or after 'period'." |
paul@376 | 775 | |
paul@1062 | 776 | first = bisect_left(self.periods, period) |
paul@1062 | 777 | return self.periods[first:] |
paul@376 | 778 | |
paul@1062 | 779 | def periods_until(self, period): |
paul@1062 | 780 | |
paul@1062 | 781 | "Return the entries in the collection before 'period'." |
paul@376 | 782 | |
paul@1062 | 783 | last = bisect_right(self.periods, Period(period.get_end(), period.get_end(), period.get_tzid())) |
paul@1062 | 784 | return self.periods[:last] |
paul@1062 | 785 | |
paul@1062 | 786 | def get_overlapping(self, period): |
paul@376 | 787 | |
paul@1062 | 788 | """ |
paul@1062 | 789 | Return the entries in the collection providing periods overlapping with |
paul@1062 | 790 | 'period'. |
paul@1062 | 791 | """ |
paul@658 | 792 | |
paul@1062 | 793 | # Find the range of periods potentially overlapping the period in the |
paul@1062 | 794 | # free/busy collection. |
paul@658 | 795 | |
paul@1062 | 796 | startpoints = self.periods_until(period) |
paul@658 | 797 | |
paul@1062 | 798 | # Find the range of periods potentially overlapping the period in a version |
paul@1062 | 799 | # of the free/busy collection sorted according to end datetimes. |
paul@658 | 800 | |
paul@1062 | 801 | endpoints = [(Period(fb.get_end_point(), fb.get_end_point()), fb) for fb in startpoints] |
paul@1062 | 802 | endpoints.sort() |
paul@1062 | 803 | first = bisect_left(endpoints, (Period(period.get_start_point(), period.get_start_point()),)) |
paul@1062 | 804 | endpoints = endpoints[first:] |
paul@658 | 805 | |
paul@1062 | 806 | overlapping = set() |
paul@658 | 807 | |
paul@1062 | 808 | for p, fb in endpoints: |
paul@1062 | 809 | if fb.overlaps(period): |
paul@1062 | 810 | overlapping.add(fb) |
paul@327 | 811 | |
paul@1062 | 812 | overlapping = list(overlapping) |
paul@1062 | 813 | overlapping.sort() |
paul@1062 | 814 | return overlapping |
paul@327 | 815 | |
paul@1062 | 816 | def remove_overlapping(self, period): |
paul@327 | 817 | |
paul@1062 | 818 | "Remove all periods overlapping with 'period' from the collection." |
paul@1062 | 819 | |
paul@1071 | 820 | self._check_mutable() |
paul@1071 | 821 | |
paul@1062 | 822 | overlapping = self.get_overlapping(period) |
paul@72 | 823 | |
paul@1062 | 824 | if overlapping: |
paul@1062 | 825 | for fb in overlapping: |
paul@1062 | 826 | self.periods.remove(fb) |
paul@72 | 827 | |
paul@1063 | 828 | class FreeBusyDatabaseCollection(FreeBusyCollectionBase): |
paul@72 | 829 | |
paul@72 | 830 | """ |
paul@1063 | 831 | An abstraction for a collection of free/busy periods stored in a database |
paul@1063 | 832 | system. |
paul@362 | 833 | """ |
paul@362 | 834 | |
paul@1074 | 835 | period_columns = ["start", "end", "uid", "transp", "recurrenceid", "summary", "organiser", "expires"] |
paul@1074 | 836 | |
paul@1074 | 837 | def __init__(self, cursor, table_name, column_names=None, filter_values=None, mutable=True): |
paul@1043 | 838 | |
paul@1062 | 839 | """ |
paul@1074 | 840 | Initialise the collection with the given 'cursor' and with the |
paul@1074 | 841 | 'table_name', 'column_names' and 'filter_values' configuring the |
paul@1074 | 842 | selection of data. |
paul@1063 | 843 | """ |
paul@1063 | 844 | |
paul@1071 | 845 | FreeBusyCollectionBase.__init__(self, mutable) |
paul@1063 | 846 | self.cursor = cursor |
paul@1063 | 847 | self.table_name = table_name |
paul@1074 | 848 | self.column_names = column_names |
paul@1074 | 849 | self.filter_values = filter_values |
paul@558 | 850 | |
paul@1063 | 851 | # Special database-related operations. |
paul@376 | 852 | |
paul@1074 | 853 | def get_condition(self, columns=None, values=None): |
paul@1074 | 854 | |
paul@1074 | 855 | """ |
paul@1074 | 856 | Return a condition clause featuring the given 'columns' and 'values' |
paul@1074 | 857 | together with any conditions provided when initialising this class. |
paul@1074 | 858 | """ |
paul@1074 | 859 | |
paul@1074 | 860 | c = list(self.column_names or []) + list(columns or []) |
paul@1074 | 861 | v = list(self.filter_values or []) + list(values or []) |
paul@1074 | 862 | return "where %s" % " and ".join([("%s = ?" % s) for s in c]), tuple(v) |
paul@1074 | 863 | |
paul@1074 | 864 | def get_values(self, values=None): |
paul@1074 | 865 | |
paul@1074 | 866 | """ |
paul@1074 | 867 | Return the given 'values' combined with any values provided when |
paul@1074 | 868 | initialising this class. |
paul@1074 | 869 | """ |
paul@1074 | 870 | |
paul@1074 | 871 | v = list(self.filter_values or []) + list(values or []) |
paul@1074 | 872 | return self.placeholders(v), tuple(v) |
paul@1074 | 873 | |
paul@1063 | 874 | def placeholders(self, values): |
paul@1063 | 875 | return ", ".join(["?"] * len(values)) |
paul@376 | 876 | |
paul@1063 | 877 | def initialise(self): |
paul@1063 | 878 | |
paul@1063 | 879 | "Create the database table required to hold the collection." |
paul@376 | 880 | |
paul@1074 | 881 | columns = """, |
paul@1074 | 882 | """.join([("%s varchar not null" % column) for column in self.column_names or []]) |
paul@1074 | 883 | |
paul@1063 | 884 | query = """\ |
paul@1063 | 885 | create table %(table)s ( |
paul@1074 | 886 | %(columns)s |
paul@1063 | 887 | start varchar not null, |
paul@1063 | 888 | end varchar not null, |
paul@1063 | 889 | uid varchar, |
paul@1063 | 890 | transp varchar, |
paul@1063 | 891 | recurrenceid varchar, |
paul@1063 | 892 | summary varchar, |
paul@1063 | 893 | organiser varchar, |
paul@1063 | 894 | expires varchar |
paul@1074 | 895 | )""" % { |
paul@1074 | 896 | "table" : self.table_name, |
paul@1074 | 897 | "columns" : columns and "%s," % columns or "" |
paul@1074 | 898 | } |
paul@376 | 899 | |
paul@1063 | 900 | self.cursor.execute(query) |
paul@376 | 901 | |
paul@1063 | 902 | # List emulation methods. |
paul@1043 | 903 | |
paul@1063 | 904 | def __nonzero__(self): |
paul@1073 | 905 | return len(self) and True or False |
paul@658 | 906 | |
paul@1063 | 907 | def __iter__(self): |
paul@1074 | 908 | condition, values = self.get_condition() |
paul@1074 | 909 | query = "select %(columns)s from %(table)s %(condition)s" % { |
paul@1074 | 910 | "columns" : ", ".join(self.period_columns), |
paul@1074 | 911 | "table" : self.table_name, |
paul@1074 | 912 | "condition" : condition |
paul@1074 | 913 | } |
paul@1074 | 914 | self.cursor.execute(query, values) |
paul@1063 | 915 | return iter(map(lambda t: FreeBusyPeriod(*t), self.cursor.fetchall())) |
paul@658 | 916 | |
paul@1063 | 917 | def __len__(self): |
paul@1074 | 918 | condition, values = self.get_condition() |
paul@1074 | 919 | query = "select count(*) from %(table)s %(condition)s" % { |
paul@1074 | 920 | "table" : self.table_name, |
paul@1074 | 921 | "condition" : condition |
paul@1074 | 922 | } |
paul@1074 | 923 | self.cursor.execute(query, values) |
paul@1073 | 924 | result = self.cursor.fetchone() |
paul@1073 | 925 | return result and result[0] or 0 |
paul@1063 | 926 | |
paul@1063 | 927 | def __getitem__(self, i): |
paul@1063 | 928 | return list(iter(self))[i] |
paul@658 | 929 | |
paul@1063 | 930 | # Operations. |
paul@658 | 931 | |
paul@1063 | 932 | def insert_period(self, period): |
paul@1063 | 933 | |
paul@1063 | 934 | "Insert the given 'period' into the collection." |
paul@658 | 935 | |
paul@1071 | 936 | self._check_mutable() |
paul@1071 | 937 | |
paul@1074 | 938 | placeholders, values = self.get_values(period.as_tuple(string_datetimes=True)) |
paul@1063 | 939 | query = "insert into %(table)s values (%(columns)s)" % { |
paul@1063 | 940 | "table" : self.table_name, |
paul@1074 | 941 | "columns" : placeholders |
paul@1063 | 942 | } |
paul@1063 | 943 | self.cursor.execute(query, values) |
paul@658 | 944 | |
paul@1063 | 945 | def remove_periods(self, periods): |
paul@1063 | 946 | |
paul@1063 | 947 | "Remove the given 'periods' from the collection." |
paul@327 | 948 | |
paul@1071 | 949 | self._check_mutable() |
paul@1071 | 950 | |
paul@1063 | 951 | for period in periods: |
paul@1074 | 952 | condition, values = self.get_condition( |
paul@1074 | 953 | self.period_columns, period.as_tuple(string_datetimes=True)) |
paul@1074 | 954 | query = "delete from %(table)s %(condition)s" % { |
paul@1074 | 955 | "table" : self.table_name, |
paul@1074 | 956 | "condition" : condition |
paul@1074 | 957 | } |
paul@1063 | 958 | self.cursor.execute(query, values) |
paul@327 | 959 | |
paul@1063 | 960 | def remove_event_periods(self, uid, recurrenceid=None): |
paul@327 | 961 | |
paul@1063 | 962 | """ |
paul@1063 | 963 | Remove from the collection all periods associated with 'uid' and |
paul@1063 | 964 | 'recurrenceid' (which if omitted causes the "parent" object's periods to |
paul@1063 | 965 | be referenced). |
paul@327 | 966 | |
paul@1063 | 967 | Return the removed periods. |
paul@1062 | 968 | """ |
paul@327 | 969 | |
paul@1071 | 970 | self._check_mutable() |
paul@1071 | 971 | |
paul@1063 | 972 | if recurrenceid: |
paul@1074 | 973 | condition, values = self.get_condition(["uid", "recurrenceid"], [uid, recurrenceid]) |
paul@1063 | 974 | else: |
paul@1074 | 975 | condition, values = self.get_condition(["uid"], [uid]) |
paul@327 | 976 | |
paul@1074 | 977 | query = "select %(columns)s from %(table)s %(condition)s" % { |
paul@1074 | 978 | "columns" : ", ".join(self.period_columns), |
paul@1063 | 979 | "table" : self.table_name, |
paul@1063 | 980 | "condition" : condition |
paul@1063 | 981 | } |
paul@1063 | 982 | self.cursor.execute(query, values) |
paul@1063 | 983 | removed = self.cursor.fetchall() |
paul@72 | 984 | |
paul@1063 | 985 | query = "delete from %(table)s %(condition)s" % { |
paul@1063 | 986 | "table" : self.table_name, |
paul@1063 | 987 | "condition" : condition |
paul@1063 | 988 | } |
paul@1063 | 989 | self.cursor.execute(query, values) |
paul@327 | 990 | |
paul@1063 | 991 | return map(lambda t: FreeBusyPeriod(*t), removed) |
paul@1063 | 992 | |
paul@1063 | 993 | def remove_additional_periods(self, uid, recurrenceids=None): |
paul@658 | 994 | |
paul@1063 | 995 | """ |
paul@1063 | 996 | Remove from the collection all periods associated with 'uid' having a |
paul@1063 | 997 | recurrence identifier indicating an additional or modified period. |
paul@72 | 998 | |
paul@1063 | 999 | If 'recurrenceids' is specified, remove all periods associated with |
paul@1063 | 1000 | 'uid' that do not have a recurrence identifier in the given list. |
paul@658 | 1001 | |
paul@1063 | 1002 | Return the removed periods. |
paul@1063 | 1003 | """ |
paul@74 | 1004 | |
paul@1071 | 1005 | self._check_mutable() |
paul@1071 | 1006 | |
paul@1063 | 1007 | if recurrenceids is None: |
paul@1074 | 1008 | condition, values = self.get_condition(["uid"], [uid]) |
paul@1074 | 1009 | extra = "recurrenceid is not null" |
paul@1063 | 1010 | else: |
paul@1074 | 1011 | condition, values = self.get_condition(["uid"], [uid]) |
paul@1074 | 1012 | extra = "recurrenceid is not null and recurrenceid not in ?" |
paul@1074 | 1013 | values = values + (recurrenceid,) |
paul@327 | 1014 | |
paul@1074 | 1015 | query = "select %(columns)s from %(table)s %(condition)s and %(extra)s" % { |
paul@1074 | 1016 | "columns" : ", ".join(self.period_columns), |
paul@1063 | 1017 | "table" : self.table_name, |
paul@1074 | 1018 | "condition" : condition, |
paul@1074 | 1019 | "extra" : extra |
paul@1063 | 1020 | } |
paul@1063 | 1021 | self.cursor.execute(query, values) |
paul@1063 | 1022 | removed = self.cursor.fetchall() |
paul@327 | 1023 | |
paul@1074 | 1024 | query = "delete from %(table)s %(condition)s and %(extra)s" % { |
paul@1063 | 1025 | "table" : self.table_name, |
paul@1074 | 1026 | "condition" : condition, |
paul@1074 | 1027 | "extra" : extra |
paul@1063 | 1028 | } |
paul@1063 | 1029 | self.cursor.execute(query, values) |
paul@327 | 1030 | |
paul@1063 | 1031 | return map(lambda t: FreeBusyPeriod(*t), removed) |
paul@327 | 1032 | |
paul@1063 | 1033 | def remove_affected_period(self, uid, start): |
paul@327 | 1034 | |
paul@1062 | 1035 | """ |
paul@1063 | 1036 | Remove from the collection the period associated with 'uid' that |
paul@1063 | 1037 | provides an occurrence starting at the given 'start' (provided by a |
paul@1063 | 1038 | recurrence identifier, converted to a datetime). A recurrence identifier |
paul@1063 | 1039 | is used to provide an alternative time period whilst also acting as a |
paul@1063 | 1040 | reference to the originally-defined occurrence. |
paul@658 | 1041 | |
paul@1063 | 1042 | Return any removed period in a list. |
paul@1062 | 1043 | """ |
paul@658 | 1044 | |
paul@1071 | 1045 | self._check_mutable() |
paul@1071 | 1046 | |
paul@1074 | 1047 | start = format_datetime(start) |
paul@1074 | 1048 | |
paul@1074 | 1049 | condition, values = self.get_condition(["uid", "start"], [uid, start]) |
paul@1074 | 1050 | extra = "recurrenceid is null" |
paul@48 | 1051 | |
paul@1074 | 1052 | query = "select %(columns)s from %(table)s %(condition)s and %(extra)s" % { |
paul@1074 | 1053 | "columns" : ", ".join(self.period_columns), |
paul@1063 | 1054 | "table" : self.table_name, |
paul@1074 | 1055 | "condition" : condition, |
paul@1074 | 1056 | "extra" : extra |
paul@1063 | 1057 | } |
paul@1063 | 1058 | self.cursor.execute(query, values) |
paul@1063 | 1059 | removed = self.cursor.fetchall() |
paul@658 | 1060 | |
paul@1074 | 1061 | query = "delete from %(table)s %(condition)s and %(extra)s" % { |
paul@1063 | 1062 | "table" : self.table_name, |
paul@1074 | 1063 | "condition" : condition, |
paul@1074 | 1064 | "extra" : extra |
paul@1063 | 1065 | } |
paul@1063 | 1066 | self.cursor.execute(query, values) |
paul@658 | 1067 | |
paul@1063 | 1068 | return map(lambda t: FreeBusyPeriod(*t), removed) |
paul@658 | 1069 | |
paul@1063 | 1070 | def periods_from(self, period): |
paul@1063 | 1071 | |
paul@1063 | 1072 | "Return the entries in the collection at or after 'period'." |
paul@1063 | 1073 | |
paul@1074 | 1074 | condition, values = self.get_condition() |
paul@1074 | 1075 | extra = "start >= ?" |
paul@1074 | 1076 | values = values + (format_datetime(period.get_start_point()),) |
paul@1063 | 1077 | |
paul@1074 | 1078 | query = "select %(columns)s from %(table)s %(condition)s and %(extra)s" % { |
paul@1074 | 1079 | "columns" : ", ".join(self.period_columns), |
paul@1063 | 1080 | "table" : self.table_name, |
paul@1074 | 1081 | "condition" : condition, |
paul@1074 | 1082 | "extra" : extra |
paul@1063 | 1083 | } |
paul@1063 | 1084 | self.cursor.execute(query, values) |
paul@1063 | 1085 | |
paul@1063 | 1086 | return map(lambda t: FreeBusyPeriod(*t), self.cursor.fetchall()) |
paul@658 | 1087 | |
paul@1063 | 1088 | def periods_until(self, period): |
paul@658 | 1089 | |
paul@1063 | 1090 | "Return the entries in the collection before 'period'." |
paul@944 | 1091 | |
paul@1074 | 1092 | condition, values = self.get_condition() |
paul@1074 | 1093 | extra = "start < ?" |
paul@1074 | 1094 | values = values + (format_datetime(period.get_end_point()),) |
paul@658 | 1095 | |
paul@1074 | 1096 | query = "select %(columns)s from %(table)s %(condition)s and %(extra)s" % { |
paul@1074 | 1097 | "columns" : ", ".join(self.period_columns), |
paul@1063 | 1098 | "table" : self.table_name, |
paul@1074 | 1099 | "condition" : condition, |
paul@1074 | 1100 | "extra" : extra |
paul@1063 | 1101 | } |
paul@1063 | 1102 | self.cursor.execute(query, values) |
paul@658 | 1103 | |
paul@1063 | 1104 | return map(lambda t: FreeBusyPeriod(*t), self.cursor.fetchall()) |
paul@658 | 1105 | |
paul@1063 | 1106 | def get_overlapping(self, period): |
paul@944 | 1107 | |
paul@1063 | 1108 | """ |
paul@1063 | 1109 | Return the entries in the collection providing periods overlapping with |
paul@1063 | 1110 | 'period'. |
paul@1063 | 1111 | """ |
paul@944 | 1112 | |
paul@1074 | 1113 | condition, values = self.get_condition() |
paul@1074 | 1114 | extra = "start < ? and end > ?" |
paul@1074 | 1115 | values = values + (format_datetime(period.get_end_point()), format_datetime(period.get_start_point())) |
paul@658 | 1116 | |
paul@1074 | 1117 | query = "select %(columns)s from %(table)s %(condition)s and %(extra)s" % { |
paul@1074 | 1118 | "columns" : ", ".join(self.period_columns), |
paul@1063 | 1119 | "table" : self.table_name, |
paul@1074 | 1120 | "condition" : condition, |
paul@1074 | 1121 | "extra" : extra |
paul@1063 | 1122 | } |
paul@1063 | 1123 | self.cursor.execute(query, values) |
paul@1063 | 1124 | |
paul@1063 | 1125 | return map(lambda t: FreeBusyPeriod(*t), self.cursor.fetchall()) |
paul@1063 | 1126 | |
paul@1063 | 1127 | def remove_overlapping(self, period): |
paul@658 | 1128 | |
paul@1063 | 1129 | "Remove all periods overlapping with 'period' from the collection." |
paul@1063 | 1130 | |
paul@1071 | 1131 | self._check_mutable() |
paul@1071 | 1132 | |
paul@1074 | 1133 | condition, values = self.get_condition() |
paul@1074 | 1134 | extra = "start < ? and end > ?" |
paul@1074 | 1135 | values = values + (format_datetime(period.get_end_point()), format_datetime(period.get_start_point())) |
paul@944 | 1136 | |
paul@1074 | 1137 | query = "delete from %(table)s %(condition)s and %(extra)s" % { |
paul@1063 | 1138 | "table" : self.table_name, |
paul@1074 | 1139 | "condition" : condition, |
paul@1074 | 1140 | "extra" : extra |
paul@1063 | 1141 | } |
paul@1063 | 1142 | self.cursor.execute(query, values) |
paul@658 | 1143 | |
paul@529 | 1144 | # Period layout. |
paul@204 | 1145 | |
paul@884 | 1146 | def get_scale(periods, tzid, view_period=None): |
paul@113 | 1147 | |
paul@113 | 1148 | """ |
paul@925 | 1149 | Return a time scale from the given list of 'periods'. |
paul@153 | 1150 | |
paul@162 | 1151 | The given 'tzid' is used to make sure that the times are defined according |
paul@162 | 1152 | to the chosen time zone. |
paul@162 | 1153 | |
paul@884 | 1154 | An optional 'view_period' is used to constrain the scale to the given |
paul@884 | 1155 | period. |
paul@884 | 1156 | |
paul@162 | 1157 | The returned scale is a mapping from time to (starting, ending) tuples, |
paul@458 | 1158 | where starting and ending are collections of periods. |
paul@113 | 1159 | """ |
paul@113 | 1160 | |
paul@113 | 1161 | scale = {} |
paul@884 | 1162 | view_start = view_period and to_timezone(view_period.get_start_point(), tzid) or None |
paul@884 | 1163 | view_end = view_period and to_timezone(view_period.get_end_point(), tzid) or None |
paul@113 | 1164 | |
paul@458 | 1165 | for p in periods: |
paul@113 | 1166 | |
paul@113 | 1167 | # Add a point and this event to the starting list. |
paul@113 | 1168 | |
paul@536 | 1169 | start = to_timezone(p.get_start(), tzid) |
paul@884 | 1170 | start = view_start and max(start, view_start) or start |
paul@536 | 1171 | if not scale.has_key(start): |
paul@536 | 1172 | scale[start] = [], [] |
paul@536 | 1173 | scale[start][0].append(p) |
paul@113 | 1174 | |
paul@113 | 1175 | # Add a point and this event to the ending list. |
paul@113 | 1176 | |
paul@536 | 1177 | end = to_timezone(p.get_end(), tzid) |
paul@931 | 1178 | end = view_end and min(end, view_end) or end |
paul@931 | 1179 | if not scale.has_key(end): |
paul@931 | 1180 | scale[end] = [], [] |
paul@931 | 1181 | scale[end][1].append(p) |
paul@113 | 1182 | |
paul@113 | 1183 | return scale |
paul@113 | 1184 | |
paul@455 | 1185 | class Point: |
paul@455 | 1186 | |
paul@455 | 1187 | "A qualified point in time." |
paul@455 | 1188 | |
paul@455 | 1189 | PRINCIPAL, REPEATED = 0, 1 |
paul@455 | 1190 | |
paul@455 | 1191 | def __init__(self, point, indicator=None): |
paul@455 | 1192 | self.point = point |
paul@455 | 1193 | self.indicator = indicator or self.PRINCIPAL |
paul@455 | 1194 | |
paul@455 | 1195 | def __hash__(self): |
paul@455 | 1196 | return hash((self.point, self.indicator)) |
paul@455 | 1197 | |
paul@455 | 1198 | def __cmp__(self, other): |
paul@455 | 1199 | if isinstance(other, Point): |
paul@455 | 1200 | return cmp((self.point, self.indicator), (other.point, other.indicator)) |
paul@455 | 1201 | elif isinstance(other, datetime): |
paul@455 | 1202 | return cmp(self.point, other) |
paul@455 | 1203 | else: |
paul@455 | 1204 | return 1 |
paul@455 | 1205 | |
paul@455 | 1206 | def __eq__(self, other): |
paul@455 | 1207 | return self.__cmp__(other) == 0 |
paul@455 | 1208 | |
paul@455 | 1209 | def __ne__(self, other): |
paul@455 | 1210 | return not self == other |
paul@455 | 1211 | |
paul@455 | 1212 | def __lt__(self, other): |
paul@455 | 1213 | return self.__cmp__(other) < 0 |
paul@455 | 1214 | |
paul@455 | 1215 | def __le__(self, other): |
paul@455 | 1216 | return self.__cmp__(other) <= 0 |
paul@455 | 1217 | |
paul@455 | 1218 | def __gt__(self, other): |
paul@455 | 1219 | return not self <= other |
paul@455 | 1220 | |
paul@455 | 1221 | def __ge__(self, other): |
paul@455 | 1222 | return not self < other |
paul@455 | 1223 | |
paul@455 | 1224 | def __repr__(self): |
paul@455 | 1225 | return "Point(%r, Point.%s)" % (self.point, self.indicator and "REPEATED" or "PRINCIPAL") |
paul@452 | 1226 | |
paul@162 | 1227 | def get_slots(scale): |
paul@113 | 1228 | |
paul@113 | 1229 | """ |
paul@162 | 1230 | Return an ordered list of time slots from the given 'scale'. |
paul@113 | 1231 | |
paul@452 | 1232 | Each slot is a tuple containing details of a point in time for the start of |
paul@458 | 1233 | the slot, together with a list of parallel event periods. |
paul@452 | 1234 | |
paul@455 | 1235 | Each point in time is described as a Point representing the actual point in |
paul@455 | 1236 | time together with an indicator of the nature of the point in time (as a |
paul@455 | 1237 | principal point in a time scale or as a repeated point used to terminate |
paul@455 | 1238 | events occurring for an instant in time). |
paul@113 | 1239 | """ |
paul@113 | 1240 | |
paul@113 | 1241 | slots = [] |
paul@113 | 1242 | active = [] |
paul@113 | 1243 | |
paul@162 | 1244 | points = scale.items() |
paul@162 | 1245 | points.sort() |
paul@162 | 1246 | |
paul@162 | 1247 | for point, (starting, ending) in points: |
paul@449 | 1248 | ending = set(ending) |
paul@449 | 1249 | instants = ending.intersection(starting) |
paul@113 | 1250 | |
paul@113 | 1251 | # Discard all active events ending at or before this start time. |
paul@161 | 1252 | # Free up the position in the active list. |
paul@113 | 1253 | |
paul@449 | 1254 | for t in ending.difference(instants): |
paul@113 | 1255 | i = active.index(t) |
paul@113 | 1256 | active[i] = None |
paul@113 | 1257 | |
paul@161 | 1258 | # For each event starting at the current point, fill any newly-vacated |
paul@161 | 1259 | # position or add to the end of the active list. |
paul@161 | 1260 | |
paul@113 | 1261 | for t in starting: |
paul@113 | 1262 | try: |
paul@113 | 1263 | i = active.index(None) |
paul@113 | 1264 | active[i] = t |
paul@113 | 1265 | except ValueError: |
paul@113 | 1266 | active.append(t) |
paul@113 | 1267 | |
paul@161 | 1268 | # Discard vacant positions from the end of the active list. |
paul@161 | 1269 | |
paul@113 | 1270 | while active and active[-1] is None: |
paul@113 | 1271 | active.pop() |
paul@113 | 1272 | |
paul@452 | 1273 | # Add an entry for the time point before "instants". |
paul@452 | 1274 | |
paul@455 | 1275 | slots.append((Point(point), active[:])) |
paul@113 | 1276 | |
paul@449 | 1277 | # Discard events ending at the same time as they began. |
paul@449 | 1278 | |
paul@449 | 1279 | if instants: |
paul@449 | 1280 | for t in instants: |
paul@449 | 1281 | i = active.index(t) |
paul@449 | 1282 | active[i] = None |
paul@449 | 1283 | |
paul@449 | 1284 | # Discard vacant positions from the end of the active list. |
paul@449 | 1285 | |
paul@449 | 1286 | while active and active[-1] is None: |
paul@449 | 1287 | active.pop() |
paul@449 | 1288 | |
paul@452 | 1289 | # Add another entry for the time point after "instants". |
paul@449 | 1290 | |
paul@455 | 1291 | slots.append((Point(point, Point.REPEATED), active[:])) |
paul@449 | 1292 | |
paul@113 | 1293 | return slots |
paul@113 | 1294 | |
paul@244 | 1295 | def add_day_start_points(slots, tzid): |
paul@153 | 1296 | |
paul@153 | 1297 | """ |
paul@162 | 1298 | Introduce into the 'slots' any day start points required by multi-day |
paul@244 | 1299 | periods. The 'tzid' is required to make sure that appropriate time zones |
paul@244 | 1300 | are chosen and not necessarily those provided by the existing time points. |
paul@153 | 1301 | """ |
paul@153 | 1302 | |
paul@162 | 1303 | new_slots = [] |
paul@153 | 1304 | current_date = None |
paul@200 | 1305 | previously_active = [] |
paul@153 | 1306 | |
paul@455 | 1307 | for point, active in slots: |
paul@455 | 1308 | start_of_day = get_start_of_day(point.point, tzid) |
paul@455 | 1309 | this_date = point.point.date() |
paul@153 | 1310 | |
paul@198 | 1311 | # For each new day, add a slot for the start of the day where periods |
paul@198 | 1312 | # are active and where no such slot already exists. |
paul@153 | 1313 | |
paul@153 | 1314 | if this_date != current_date: |
paul@414 | 1315 | |
paul@414 | 1316 | # Fill in days where events remain active. |
paul@414 | 1317 | |
paul@414 | 1318 | if current_date: |
paul@414 | 1319 | current_date += timedelta(1) |
paul@414 | 1320 | while current_date < this_date: |
paul@455 | 1321 | new_slots.append((Point(get_start_of_day(current_date, tzid)), previously_active)) |
paul@414 | 1322 | current_date += timedelta(1) |
paul@414 | 1323 | else: |
paul@414 | 1324 | current_date = this_date |
paul@153 | 1325 | |
paul@153 | 1326 | # Add any continuing periods. |
paul@153 | 1327 | |
paul@455 | 1328 | if point.point != start_of_day: |
paul@455 | 1329 | new_slots.append((Point(start_of_day), previously_active)) |
paul@153 | 1330 | |
paul@153 | 1331 | # Add the currently active periods at this point in time. |
paul@153 | 1332 | |
paul@153 | 1333 | previously_active = active |
paul@153 | 1334 | |
paul@162 | 1335 | for t in new_slots: |
paul@162 | 1336 | insort_left(slots, t) |
paul@162 | 1337 | |
paul@931 | 1338 | def remove_end_slot(slots, view_period): |
paul@931 | 1339 | |
paul@931 | 1340 | """ |
paul@931 | 1341 | Remove from 'slots' any slot situated at the end of the given 'view_period'. |
paul@931 | 1342 | """ |
paul@931 | 1343 | |
paul@931 | 1344 | end = view_period.get_end_point() |
paul@931 | 1345 | if not end or not slots: |
paul@931 | 1346 | return |
paul@931 | 1347 | i = bisect_left(slots, (Point(end), None)) |
paul@931 | 1348 | if i < len(slots): |
paul@931 | 1349 | del slots[i:] |
paul@931 | 1350 | |
paul@162 | 1351 | def add_slots(slots, points): |
paul@162 | 1352 | |
paul@162 | 1353 | """ |
paul@162 | 1354 | Introduce into the 'slots' entries for those in 'points' that are not |
paul@170 | 1355 | already present, propagating active periods from time points preceding |
paul@170 | 1356 | those added. |
paul@162 | 1357 | """ |
paul@162 | 1358 | |
paul@162 | 1359 | new_slots = [] |
paul@162 | 1360 | |
paul@162 | 1361 | for point in points: |
paul@452 | 1362 | i = bisect_left(slots, (point,)) # slots is [(point, active)...] |
paul@162 | 1363 | if i < len(slots) and slots[i][0] == point: |
paul@162 | 1364 | continue |
paul@162 | 1365 | |
paul@170 | 1366 | new_slots.append((point, i > 0 and slots[i-1][1] or [])) |
paul@162 | 1367 | |
paul@162 | 1368 | for t in new_slots: |
paul@162 | 1369 | insort_left(slots, t) |
paul@162 | 1370 | |
paul@162 | 1371 | def partition_by_day(slots): |
paul@162 | 1372 | |
paul@162 | 1373 | """ |
paul@162 | 1374 | Return a mapping from dates to time points provided by 'slots'. |
paul@162 | 1375 | """ |
paul@162 | 1376 | |
paul@162 | 1377 | d = {} |
paul@162 | 1378 | |
paul@455 | 1379 | for point, value in slots: |
paul@455 | 1380 | day = point.point.date() |
paul@162 | 1381 | if not d.has_key(day): |
paul@162 | 1382 | d[day] = [] |
paul@455 | 1383 | d[day].append((point, value)) |
paul@162 | 1384 | |
paul@162 | 1385 | return d |
paul@153 | 1386 | |
paul@876 | 1387 | def add_empty_days(days, tzid, start=None, end=None): |
paul@279 | 1388 | |
paul@876 | 1389 | """ |
paul@876 | 1390 | Add empty days to 'days' between busy days, and optionally from the given |
paul@876 | 1391 | 'start' day and until the given 'end' day. |
paul@876 | 1392 | """ |
paul@279 | 1393 | |
paul@888 | 1394 | last_day = start - timedelta(1) |
paul@279 | 1395 | all_days = days.keys() |
paul@279 | 1396 | all_days.sort() |
paul@279 | 1397 | |
paul@279 | 1398 | for day in all_days: |
paul@279 | 1399 | if last_day: |
paul@279 | 1400 | empty_day = last_day + timedelta(1) |
paul@279 | 1401 | while empty_day < day: |
paul@455 | 1402 | days[empty_day] = [(Point(get_start_of_day(empty_day, tzid)), None)] |
paul@279 | 1403 | empty_day += timedelta(1) |
paul@876 | 1404 | last_day = day |
paul@876 | 1405 | |
paul@876 | 1406 | if end: |
paul@876 | 1407 | empty_day = last_day + timedelta(1) |
paul@876 | 1408 | while empty_day < end: |
paul@876 | 1409 | days[empty_day] = [(Point(get_start_of_day(empty_day, tzid)), None)] |
paul@876 | 1410 | empty_day += timedelta(1) |
paul@279 | 1411 | |
paul@114 | 1412 | def get_spans(slots): |
paul@114 | 1413 | |
paul@533 | 1414 | "Inspect the given 'slots', returning a mapping of period keys to spans." |
paul@114 | 1415 | |
paul@455 | 1416 | points = [point for point, active in slots] |
paul@114 | 1417 | spans = {} |
paul@114 | 1418 | |
paul@449 | 1419 | for _point, active in slots: |
paul@458 | 1420 | for p in active: |
paul@458 | 1421 | if p: |
paul@458 | 1422 | key = p.get_key() |
paul@529 | 1423 | start_slot = bisect_left(points, p.get_start()) |
paul@529 | 1424 | end_slot = bisect_left(points, p.get_end()) |
paul@185 | 1425 | spans[key] = end_slot - start_slot |
paul@114 | 1426 | |
paul@114 | 1427 | return spans |
paul@114 | 1428 | |
paul@48 | 1429 | # vim: tabstop=4 expandtab shiftwidth=4 |