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