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