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@1230 | 6 | Copyright (C) 2014, 2015, 2016, 2017 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@1230 | 22 | from bisect import bisect_left, insort_left |
paul@529 | 23 | from datetime import date, datetime, timedelta |
paul@939 | 24 | from imiptools.dates import check_permitted_values, correct_datetime, \ |
paul@1230 | 25 | 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@1171 | 133 | |
paul@1171 | 134 | """ |
paul@1171 | 135 | Define a period according to 'start' and 'end' which may be special |
paul@1171 | 136 | start/end of time values or iCalendar-format datetime strings. |
paul@1171 | 137 | """ |
paul@1171 | 138 | |
paul@944 | 139 | if isinstance(start, (date, PointInTime)): |
paul@944 | 140 | self.start = start |
paul@944 | 141 | else: |
paul@944 | 142 | self.start = get_datetime(start) or StartOfTime() |
paul@1171 | 143 | |
paul@944 | 144 | if isinstance(end, (date, PointInTime)): |
paul@944 | 145 | self.end = end |
paul@944 | 146 | else: |
paul@944 | 147 | self.end = get_datetime(end) or EndOfTime() |
paul@944 | 148 | |
paul@646 | 149 | def as_tuple(self): |
paul@646 | 150 | return self.start, self.end |
paul@646 | 151 | |
paul@646 | 152 | def __hash__(self): |
paul@646 | 153 | return hash((self.get_start(), self.get_end())) |
paul@646 | 154 | |
paul@646 | 155 | def __cmp__(self, other): |
paul@646 | 156 | |
paul@646 | 157 | "Return a comparison result against 'other' using points in time." |
paul@646 | 158 | |
paul@646 | 159 | if isinstance(other, PeriodBase): |
paul@646 | 160 | return cmp( |
paul@924 | 161 | (Comparable(ifnone(self.get_start_point(), StartOfTime())), Comparable(ifnone(self.get_end_point(), EndOfTime()))), |
paul@924 | 162 | (Comparable(ifnone(other.get_start_point(), StartOfTime())), Comparable(ifnone(other.get_end_point(), EndOfTime()))) |
paul@646 | 163 | ) |
paul@646 | 164 | else: |
paul@646 | 165 | return 1 |
paul@646 | 166 | |
paul@874 | 167 | def overlaps(self, other): |
paul@924 | 168 | return Comparable(ifnone(self.get_end_point(), EndOfTime())) > Comparable(ifnone(other.get_start_point(), StartOfTime())) and \ |
paul@924 | 169 | Comparable(ifnone(self.get_start_point(), StartOfTime())) < Comparable(ifnone(other.get_end_point(), EndOfTime())) |
paul@874 | 170 | |
paul@941 | 171 | def within(self, other): |
paul@941 | 172 | return Comparable(ifnone(self.get_start_point(), StartOfTime())) >= Comparable(ifnone(other.get_start_point(), StartOfTime())) and \ |
paul@941 | 173 | Comparable(ifnone(self.get_end_point(), EndOfTime())) <= Comparable(ifnone(other.get_end_point(), EndOfTime())) |
paul@941 | 174 | |
paul@944 | 175 | def common(self, other): |
paul@944 | 176 | start = max(Comparable(ifnone(self.get_start_point(), StartOfTime())), Comparable(ifnone(other.get_start_point(), StartOfTime()))) |
paul@944 | 177 | end = min(Comparable(ifnone(self.get_end_point(), EndOfTime())), Comparable(ifnone(other.get_end_point(), EndOfTime()))) |
paul@944 | 178 | if start <= end: |
paul@944 | 179 | return self.make_corrected(start.dt, end.dt) |
paul@944 | 180 | else: |
paul@944 | 181 | return None |
paul@944 | 182 | |
paul@646 | 183 | def get_key(self): |
paul@646 | 184 | return self.get_start(), self.get_end() |
paul@646 | 185 | |
paul@646 | 186 | # Datetime and metadata methods. |
paul@646 | 187 | |
paul@646 | 188 | def get_start(self): |
paul@646 | 189 | return self.start |
paul@646 | 190 | |
paul@646 | 191 | def get_end(self): |
paul@646 | 192 | return self.end |
paul@646 | 193 | |
paul@646 | 194 | def get_start_attr(self): |
paul@646 | 195 | return get_datetime_attributes(self.start, self.tzid) |
paul@646 | 196 | |
paul@646 | 197 | def get_end_attr(self): |
paul@646 | 198 | return get_datetime_attributes(self.end, self.tzid) |
paul@646 | 199 | |
paul@646 | 200 | def get_start_item(self): |
paul@646 | 201 | return self.get_start(), self.get_start_attr() |
paul@646 | 202 | |
paul@646 | 203 | def get_end_item(self): |
paul@646 | 204 | return self.get_end(), self.get_end_attr() |
paul@646 | 205 | |
paul@646 | 206 | def get_start_point(self): |
paul@646 | 207 | return self.start |
paul@646 | 208 | |
paul@646 | 209 | def get_end_point(self): |
paul@646 | 210 | return self.end |
paul@646 | 211 | |
paul@659 | 212 | def get_duration(self): |
paul@944 | 213 | start = self.get_start_point() |
paul@944 | 214 | end = self.get_end_point() |
paul@944 | 215 | if start and end: |
paul@944 | 216 | return end - start |
paul@944 | 217 | else: |
paul@944 | 218 | return Endless() |
paul@659 | 219 | |
paul@646 | 220 | class Period(PeriodBase): |
paul@646 | 221 | |
paul@646 | 222 | "A simple period abstraction." |
paul@646 | 223 | |
paul@541 | 224 | def __init__(self, start, end, tzid=None, origin=None): |
paul@541 | 225 | |
paul@541 | 226 | """ |
paul@541 | 227 | Initialise a period with the given 'start' and 'end', having a |
paul@541 | 228 | contextual 'tzid', if specified, and an indicated 'origin'. |
paul@620 | 229 | |
paul@620 | 230 | All metadata from the start and end points are derived from the supplied |
paul@620 | 231 | dates/datetimes. |
paul@541 | 232 | """ |
paul@541 | 233 | |
paul@944 | 234 | PeriodBase.__init__(self, start, end) |
paul@541 | 235 | self.tzid = tzid |
paul@528 | 236 | self.origin = origin |
paul@458 | 237 | |
paul@458 | 238 | def as_tuple(self): |
paul@541 | 239 | return self.start, self.end, self.tzid, self.origin |
paul@458 | 240 | |
paul@458 | 241 | def __repr__(self): |
paul@630 | 242 | return "Period%r" % (self.as_tuple(),) |
paul@458 | 243 | |
paul@646 | 244 | # Datetime and metadata methods. |
paul@528 | 245 | |
paul@541 | 246 | def get_tzid(self): |
paul@620 | 247 | return get_tzid(self.get_start_attr(), self.get_end_attr()) or self.tzid |
paul@620 | 248 | |
paul@557 | 249 | def get_start_point(self): |
paul@874 | 250 | start = self.get_start() |
paul@924 | 251 | if isinstance(start, PointInTime): return start |
paul@924 | 252 | else: return to_utc_datetime(start, self.get_tzid()) |
paul@557 | 253 | |
paul@557 | 254 | def get_end_point(self): |
paul@874 | 255 | end = self.get_end() |
paul@924 | 256 | if isinstance(end, PointInTime): return end |
paul@924 | 257 | else: return to_utc_datetime(end, self.get_tzid()) |
paul@557 | 258 | |
paul@633 | 259 | # Period and event recurrence logic. |
paul@633 | 260 | |
paul@633 | 261 | def is_replaced(self, recurrenceids): |
paul@633 | 262 | |
paul@633 | 263 | """ |
paul@633 | 264 | Return whether this period refers to one of the 'recurrenceids'. |
paul@633 | 265 | The 'recurrenceids' should be normalised to UTC datetimes according to |
paul@633 | 266 | time zone information provided by their objects or be floating dates or |
paul@633 | 267 | datetimes requiring conversion using contextual time zone information. |
paul@633 | 268 | """ |
paul@633 | 269 | |
paul@633 | 270 | for recurrenceid in recurrenceids: |
paul@647 | 271 | if self.is_affected(recurrenceid): |
paul@633 | 272 | return recurrenceid |
paul@633 | 273 | return None |
paul@633 | 274 | |
paul@633 | 275 | def is_affected(self, recurrenceid): |
paul@633 | 276 | |
paul@633 | 277 | """ |
paul@633 | 278 | Return whether this period refers to 'recurrenceid'. The 'recurrenceid' |
paul@633 | 279 | should be normalised to UTC datetimes according to time zone information |
paul@633 | 280 | provided by their objects. Otherwise, this period's contextual time zone |
paul@633 | 281 | information is used to convert any date or floating datetime |
paul@633 | 282 | representation to a point in time. |
paul@633 | 283 | """ |
paul@633 | 284 | |
paul@633 | 285 | if not recurrenceid: |
paul@633 | 286 | return None |
paul@633 | 287 | d = get_recurrence_start(recurrenceid) |
paul@633 | 288 | dt = get_recurrence_start_point(recurrenceid, self.tzid) |
paul@1126 | 289 | |
paul@1126 | 290 | # Compare the start to dates only, using the normalised start datetime |
paul@1126 | 291 | # for comparisons with the start point. |
paul@1126 | 292 | |
paul@1126 | 293 | if not isinstance(d, datetime) and self.get_start() == d or self.get_start_point() == dt: |
paul@633 | 294 | return recurrenceid |
paul@1126 | 295 | |
paul@633 | 296 | return None |
paul@633 | 297 | |
paul@939 | 298 | # Value correction methods. |
paul@939 | 299 | |
paul@941 | 300 | def with_duration(self, duration): |
paul@939 | 301 | |
paul@941 | 302 | """ |
paul@941 | 303 | Return a version of this period with the same start point but with the |
paul@941 | 304 | given 'duration'. |
paul@941 | 305 | """ |
paul@941 | 306 | |
paul@941 | 307 | return self.make_corrected(self.get_start(), self.get_start() + duration) |
paul@941 | 308 | |
paul@941 | 309 | def check_permitted(self, permitted_values): |
paul@941 | 310 | |
paul@941 | 311 | "Check the period against the given 'permitted_values'." |
paul@939 | 312 | |
paul@939 | 313 | start = self.get_start() |
paul@939 | 314 | end = self.get_end() |
paul@939 | 315 | start_errors = check_permitted_values(start, permitted_values) |
paul@939 | 316 | end_errors = check_permitted_values(end, permitted_values) |
paul@939 | 317 | |
paul@939 | 318 | if not (start_errors or end_errors): |
paul@941 | 319 | return None |
paul@941 | 320 | |
paul@941 | 321 | return start_errors, end_errors |
paul@941 | 322 | |
paul@941 | 323 | def get_corrected(self, permitted_values): |
paul@941 | 324 | |
paul@941 | 325 | "Return a corrected version of this period." |
paul@941 | 326 | |
paul@941 | 327 | errors = self.check_permitted(permitted_values) |
paul@941 | 328 | |
paul@941 | 329 | if not errors: |
paul@939 | 330 | return self |
paul@939 | 331 | |
paul@941 | 332 | start_errors, end_errors = errors |
paul@941 | 333 | |
paul@952 | 334 | start = self.get_start() |
paul@952 | 335 | end = self.get_end() |
paul@952 | 336 | |
paul@939 | 337 | if start_errors: |
paul@939 | 338 | start = correct_datetime(start, permitted_values) |
paul@939 | 339 | if end_errors: |
paul@939 | 340 | end = correct_datetime(end, permitted_values) |
paul@939 | 341 | |
paul@939 | 342 | return self.make_corrected(start, end) |
paul@939 | 343 | |
paul@939 | 344 | def make_corrected(self, start, end): |
paul@939 | 345 | return self.__class__(start, end, self.tzid, self.origin) |
paul@939 | 346 | |
paul@543 | 347 | class RecurringPeriod(Period): |
paul@543 | 348 | |
paul@620 | 349 | """ |
paul@620 | 350 | A period with iCalendar metadata attributes and origin information from an |
paul@620 | 351 | object. |
paul@620 | 352 | """ |
paul@543 | 353 | |
paul@543 | 354 | def __init__(self, start, end, tzid=None, origin=None, start_attr=None, end_attr=None): |
paul@543 | 355 | Period.__init__(self, start, end, tzid, origin) |
paul@543 | 356 | self.start_attr = start_attr |
paul@543 | 357 | self.end_attr = end_attr |
paul@543 | 358 | |
paul@620 | 359 | def get_start_attr(self): |
paul@620 | 360 | return self.start_attr |
paul@543 | 361 | |
paul@620 | 362 | def get_end_attr(self): |
paul@620 | 363 | return self.end_attr |
paul@543 | 364 | |
paul@543 | 365 | def as_tuple(self): |
paul@543 | 366 | return self.start, self.end, self.tzid, self.origin, self.start_attr, self.end_attr |
paul@543 | 367 | |
paul@543 | 368 | def __repr__(self): |
paul@630 | 369 | return "RecurringPeriod%r" % (self.as_tuple(),) |
paul@543 | 370 | |
paul@939 | 371 | def make_corrected(self, start, end): |
paul@939 | 372 | return self.__class__(start, end, self.tzid, self.origin, self.get_start_attr(), self.get_end_attr()) |
paul@939 | 373 | |
paul@1189 | 374 | def get_overlapping(first, second): |
paul@1189 | 375 | |
paul@1189 | 376 | """ |
paul@1189 | 377 | Return the entries in the sorted 'first' collection that are overlapping |
paul@1189 | 378 | with the given sorted 'second' collection. |
paul@1189 | 379 | """ |
paul@1189 | 380 | |
paul@1189 | 381 | if not first or not second: |
paul@1189 | 382 | return [] |
paul@1189 | 383 | |
paul@1189 | 384 | # Examine each period in the second collection, attempting to match periods |
paul@1189 | 385 | # in the first collection. |
paul@1189 | 386 | |
paul@1189 | 387 | overlapping = set() |
paul@1189 | 388 | |
paul@1189 | 389 | for p2 in second: |
paul@1189 | 390 | last_point = p2.get_end_point() |
paul@1189 | 391 | |
paul@1189 | 392 | # Examine the first collection up to the point where no matches will |
paul@1189 | 393 | # occur. |
paul@1189 | 394 | |
paul@1189 | 395 | for p1 in first: |
paul@1189 | 396 | if p1.get_start_point() > last_point: |
paul@1189 | 397 | break |
paul@1189 | 398 | elif p1.overlaps(p2): |
paul@1189 | 399 | overlapping.add(p1) |
paul@1189 | 400 | |
paul@1189 | 401 | overlapping = list(overlapping) |
paul@1189 | 402 | overlapping.sort() |
paul@1189 | 403 | return overlapping |
paul@1189 | 404 | |
paul@529 | 405 | # Period layout. |
paul@204 | 406 | |
paul@884 | 407 | def get_scale(periods, tzid, view_period=None): |
paul@113 | 408 | |
paul@113 | 409 | """ |
paul@925 | 410 | Return a time scale from the given list of 'periods'. |
paul@153 | 411 | |
paul@162 | 412 | The given 'tzid' is used to make sure that the times are defined according |
paul@162 | 413 | to the chosen time zone. |
paul@162 | 414 | |
paul@884 | 415 | An optional 'view_period' is used to constrain the scale to the given |
paul@884 | 416 | period. |
paul@884 | 417 | |
paul@162 | 418 | The returned scale is a mapping from time to (starting, ending) tuples, |
paul@458 | 419 | where starting and ending are collections of periods. |
paul@113 | 420 | """ |
paul@113 | 421 | |
paul@113 | 422 | scale = {} |
paul@884 | 423 | view_start = view_period and to_timezone(view_period.get_start_point(), tzid) or None |
paul@884 | 424 | view_end = view_period and to_timezone(view_period.get_end_point(), tzid) or None |
paul@113 | 425 | |
paul@458 | 426 | for p in periods: |
paul@113 | 427 | |
paul@113 | 428 | # Add a point and this event to the starting list. |
paul@113 | 429 | |
paul@536 | 430 | start = to_timezone(p.get_start(), tzid) |
paul@884 | 431 | start = view_start and max(start, view_start) or start |
paul@536 | 432 | if not scale.has_key(start): |
paul@536 | 433 | scale[start] = [], [] |
paul@536 | 434 | scale[start][0].append(p) |
paul@113 | 435 | |
paul@113 | 436 | # Add a point and this event to the ending list. |
paul@113 | 437 | |
paul@536 | 438 | end = to_timezone(p.get_end(), tzid) |
paul@931 | 439 | end = view_end and min(end, view_end) or end |
paul@931 | 440 | if not scale.has_key(end): |
paul@931 | 441 | scale[end] = [], [] |
paul@931 | 442 | scale[end][1].append(p) |
paul@113 | 443 | |
paul@113 | 444 | return scale |
paul@113 | 445 | |
paul@455 | 446 | class Point: |
paul@455 | 447 | |
paul@455 | 448 | "A qualified point in time." |
paul@455 | 449 | |
paul@455 | 450 | PRINCIPAL, REPEATED = 0, 1 |
paul@455 | 451 | |
paul@455 | 452 | def __init__(self, point, indicator=None): |
paul@455 | 453 | self.point = point |
paul@455 | 454 | self.indicator = indicator or self.PRINCIPAL |
paul@455 | 455 | |
paul@455 | 456 | def __hash__(self): |
paul@455 | 457 | return hash((self.point, self.indicator)) |
paul@455 | 458 | |
paul@455 | 459 | def __cmp__(self, other): |
paul@455 | 460 | if isinstance(other, Point): |
paul@455 | 461 | return cmp((self.point, self.indicator), (other.point, other.indicator)) |
paul@455 | 462 | elif isinstance(other, datetime): |
paul@455 | 463 | return cmp(self.point, other) |
paul@455 | 464 | else: |
paul@455 | 465 | return 1 |
paul@455 | 466 | |
paul@455 | 467 | def __eq__(self, other): |
paul@455 | 468 | return self.__cmp__(other) == 0 |
paul@455 | 469 | |
paul@455 | 470 | def __ne__(self, other): |
paul@455 | 471 | return not self == other |
paul@455 | 472 | |
paul@455 | 473 | def __lt__(self, other): |
paul@455 | 474 | return self.__cmp__(other) < 0 |
paul@455 | 475 | |
paul@455 | 476 | def __le__(self, other): |
paul@455 | 477 | return self.__cmp__(other) <= 0 |
paul@455 | 478 | |
paul@455 | 479 | def __gt__(self, other): |
paul@455 | 480 | return not self <= other |
paul@455 | 481 | |
paul@455 | 482 | def __ge__(self, other): |
paul@455 | 483 | return not self < other |
paul@455 | 484 | |
paul@455 | 485 | def __repr__(self): |
paul@455 | 486 | return "Point(%r, Point.%s)" % (self.point, self.indicator and "REPEATED" or "PRINCIPAL") |
paul@452 | 487 | |
paul@162 | 488 | def get_slots(scale): |
paul@113 | 489 | |
paul@113 | 490 | """ |
paul@162 | 491 | Return an ordered list of time slots from the given 'scale'. |
paul@113 | 492 | |
paul@452 | 493 | Each slot is a tuple containing details of a point in time for the start of |
paul@458 | 494 | the slot, together with a list of parallel event periods. |
paul@452 | 495 | |
paul@455 | 496 | Each point in time is described as a Point representing the actual point in |
paul@455 | 497 | time together with an indicator of the nature of the point in time (as a |
paul@455 | 498 | principal point in a time scale or as a repeated point used to terminate |
paul@455 | 499 | events occurring for an instant in time). |
paul@113 | 500 | """ |
paul@113 | 501 | |
paul@113 | 502 | slots = [] |
paul@113 | 503 | active = [] |
paul@113 | 504 | |
paul@162 | 505 | points = scale.items() |
paul@162 | 506 | points.sort() |
paul@162 | 507 | |
paul@162 | 508 | for point, (starting, ending) in points: |
paul@449 | 509 | ending = set(ending) |
paul@449 | 510 | instants = ending.intersection(starting) |
paul@113 | 511 | |
paul@113 | 512 | # Discard all active events ending at or before this start time. |
paul@161 | 513 | # Free up the position in the active list. |
paul@113 | 514 | |
paul@449 | 515 | for t in ending.difference(instants): |
paul@113 | 516 | i = active.index(t) |
paul@113 | 517 | active[i] = None |
paul@113 | 518 | |
paul@161 | 519 | # For each event starting at the current point, fill any newly-vacated |
paul@161 | 520 | # position or add to the end of the active list. |
paul@161 | 521 | |
paul@113 | 522 | for t in starting: |
paul@113 | 523 | try: |
paul@113 | 524 | i = active.index(None) |
paul@113 | 525 | active[i] = t |
paul@113 | 526 | except ValueError: |
paul@113 | 527 | active.append(t) |
paul@113 | 528 | |
paul@161 | 529 | # Discard vacant positions from the end of the active list. |
paul@161 | 530 | |
paul@113 | 531 | while active and active[-1] is None: |
paul@113 | 532 | active.pop() |
paul@113 | 533 | |
paul@452 | 534 | # Add an entry for the time point before "instants". |
paul@452 | 535 | |
paul@455 | 536 | slots.append((Point(point), active[:])) |
paul@113 | 537 | |
paul@449 | 538 | # Discard events ending at the same time as they began. |
paul@449 | 539 | |
paul@449 | 540 | if instants: |
paul@449 | 541 | for t in instants: |
paul@449 | 542 | i = active.index(t) |
paul@449 | 543 | active[i] = None |
paul@449 | 544 | |
paul@449 | 545 | # Discard vacant positions from the end of the active list. |
paul@449 | 546 | |
paul@449 | 547 | while active and active[-1] is None: |
paul@449 | 548 | active.pop() |
paul@449 | 549 | |
paul@452 | 550 | # Add another entry for the time point after "instants". |
paul@449 | 551 | |
paul@455 | 552 | slots.append((Point(point, Point.REPEATED), active[:])) |
paul@449 | 553 | |
paul@113 | 554 | return slots |
paul@113 | 555 | |
paul@244 | 556 | def add_day_start_points(slots, tzid): |
paul@153 | 557 | |
paul@153 | 558 | """ |
paul@162 | 559 | Introduce into the 'slots' any day start points required by multi-day |
paul@244 | 560 | periods. The 'tzid' is required to make sure that appropriate time zones |
paul@244 | 561 | are chosen and not necessarily those provided by the existing time points. |
paul@153 | 562 | """ |
paul@153 | 563 | |
paul@162 | 564 | new_slots = [] |
paul@153 | 565 | current_date = None |
paul@200 | 566 | previously_active = [] |
paul@153 | 567 | |
paul@455 | 568 | for point, active in slots: |
paul@455 | 569 | start_of_day = get_start_of_day(point.point, tzid) |
paul@455 | 570 | this_date = point.point.date() |
paul@153 | 571 | |
paul@198 | 572 | # For each new day, add a slot for the start of the day where periods |
paul@198 | 573 | # are active and where no such slot already exists. |
paul@153 | 574 | |
paul@153 | 575 | if this_date != current_date: |
paul@414 | 576 | |
paul@414 | 577 | # Fill in days where events remain active. |
paul@414 | 578 | |
paul@414 | 579 | if current_date: |
paul@414 | 580 | current_date += timedelta(1) |
paul@414 | 581 | while current_date < this_date: |
paul@455 | 582 | new_slots.append((Point(get_start_of_day(current_date, tzid)), previously_active)) |
paul@414 | 583 | current_date += timedelta(1) |
paul@414 | 584 | else: |
paul@414 | 585 | current_date = this_date |
paul@153 | 586 | |
paul@153 | 587 | # Add any continuing periods. |
paul@153 | 588 | |
paul@455 | 589 | if point.point != start_of_day: |
paul@455 | 590 | new_slots.append((Point(start_of_day), previously_active)) |
paul@153 | 591 | |
paul@153 | 592 | # Add the currently active periods at this point in time. |
paul@153 | 593 | |
paul@153 | 594 | previously_active = active |
paul@153 | 595 | |
paul@162 | 596 | for t in new_slots: |
paul@162 | 597 | insort_left(slots, t) |
paul@162 | 598 | |
paul@931 | 599 | def remove_end_slot(slots, view_period): |
paul@931 | 600 | |
paul@931 | 601 | """ |
paul@931 | 602 | Remove from 'slots' any slot situated at the end of the given 'view_period'. |
paul@931 | 603 | """ |
paul@931 | 604 | |
paul@931 | 605 | end = view_period.get_end_point() |
paul@931 | 606 | if not end or not slots: |
paul@931 | 607 | return |
paul@931 | 608 | i = bisect_left(slots, (Point(end), None)) |
paul@931 | 609 | if i < len(slots): |
paul@931 | 610 | del slots[i:] |
paul@931 | 611 | |
paul@162 | 612 | def add_slots(slots, points): |
paul@162 | 613 | |
paul@162 | 614 | """ |
paul@162 | 615 | Introduce into the 'slots' entries for those in 'points' that are not |
paul@170 | 616 | already present, propagating active periods from time points preceding |
paul@170 | 617 | those added. |
paul@162 | 618 | """ |
paul@162 | 619 | |
paul@162 | 620 | new_slots = [] |
paul@162 | 621 | |
paul@162 | 622 | for point in points: |
paul@452 | 623 | i = bisect_left(slots, (point,)) # slots is [(point, active)...] |
paul@162 | 624 | if i < len(slots) and slots[i][0] == point: |
paul@162 | 625 | continue |
paul@162 | 626 | |
paul@170 | 627 | new_slots.append((point, i > 0 and slots[i-1][1] or [])) |
paul@162 | 628 | |
paul@162 | 629 | for t in new_slots: |
paul@162 | 630 | insort_left(slots, t) |
paul@162 | 631 | |
paul@162 | 632 | def partition_by_day(slots): |
paul@162 | 633 | |
paul@162 | 634 | """ |
paul@162 | 635 | Return a mapping from dates to time points provided by 'slots'. |
paul@162 | 636 | """ |
paul@162 | 637 | |
paul@162 | 638 | d = {} |
paul@162 | 639 | |
paul@455 | 640 | for point, value in slots: |
paul@455 | 641 | day = point.point.date() |
paul@162 | 642 | if not d.has_key(day): |
paul@162 | 643 | d[day] = [] |
paul@455 | 644 | d[day].append((point, value)) |
paul@162 | 645 | |
paul@162 | 646 | return d |
paul@153 | 647 | |
paul@876 | 648 | def add_empty_days(days, tzid, start=None, end=None): |
paul@279 | 649 | |
paul@876 | 650 | """ |
paul@876 | 651 | Add empty days to 'days' between busy days, and optionally from the given |
paul@876 | 652 | 'start' day and until the given 'end' day. |
paul@876 | 653 | """ |
paul@279 | 654 | |
paul@888 | 655 | last_day = start - timedelta(1) |
paul@279 | 656 | all_days = days.keys() |
paul@279 | 657 | all_days.sort() |
paul@279 | 658 | |
paul@279 | 659 | for day in all_days: |
paul@279 | 660 | if last_day: |
paul@279 | 661 | empty_day = last_day + timedelta(1) |
paul@279 | 662 | while empty_day < day: |
paul@455 | 663 | days[empty_day] = [(Point(get_start_of_day(empty_day, tzid)), None)] |
paul@279 | 664 | empty_day += timedelta(1) |
paul@876 | 665 | last_day = day |
paul@876 | 666 | |
paul@876 | 667 | if end: |
paul@876 | 668 | empty_day = last_day + timedelta(1) |
paul@876 | 669 | while empty_day < end: |
paul@876 | 670 | days[empty_day] = [(Point(get_start_of_day(empty_day, tzid)), None)] |
paul@876 | 671 | empty_day += timedelta(1) |
paul@279 | 672 | |
paul@114 | 673 | def get_spans(slots): |
paul@114 | 674 | |
paul@533 | 675 | "Inspect the given 'slots', returning a mapping of period keys to spans." |
paul@114 | 676 | |
paul@455 | 677 | points = [point for point, active in slots] |
paul@114 | 678 | spans = {} |
paul@114 | 679 | |
paul@449 | 680 | for _point, active in slots: |
paul@458 | 681 | for p in active: |
paul@458 | 682 | if p: |
paul@458 | 683 | key = p.get_key() |
paul@529 | 684 | start_slot = bisect_left(points, p.get_start()) |
paul@529 | 685 | end_slot = bisect_left(points, p.get_end()) |
paul@185 | 686 | spans[key] = end_slot - start_slot |
paul@114 | 687 | |
paul@114 | 688 | return spans |
paul@114 | 689 | |
paul@48 | 690 | # vim: tabstop=4 expandtab shiftwidth=4 |