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