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@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@939 | 308 | # Value correction methods. |
paul@939 | 309 | |
paul@941 | 310 | def with_duration(self, duration): |
paul@939 | 311 | |
paul@941 | 312 | """ |
paul@941 | 313 | Return a version of this period with the same start point but with the |
paul@941 | 314 | given 'duration'. |
paul@941 | 315 | """ |
paul@941 | 316 | |
paul@941 | 317 | return self.make_corrected(self.get_start(), self.get_start() + duration) |
paul@941 | 318 | |
paul@941 | 319 | def check_permitted(self, permitted_values): |
paul@941 | 320 | |
paul@941 | 321 | "Check the period against the given 'permitted_values'." |
paul@939 | 322 | |
paul@939 | 323 | start = self.get_start() |
paul@939 | 324 | end = self.get_end() |
paul@939 | 325 | start_errors = check_permitted_values(start, permitted_values) |
paul@939 | 326 | end_errors = check_permitted_values(end, permitted_values) |
paul@939 | 327 | |
paul@939 | 328 | if not (start_errors or end_errors): |
paul@941 | 329 | return None |
paul@941 | 330 | |
paul@941 | 331 | return start_errors, end_errors |
paul@941 | 332 | |
paul@941 | 333 | def get_corrected(self, permitted_values): |
paul@941 | 334 | |
paul@941 | 335 | "Return a corrected version of this period." |
paul@941 | 336 | |
paul@941 | 337 | errors = self.check_permitted(permitted_values) |
paul@941 | 338 | |
paul@941 | 339 | if not errors: |
paul@939 | 340 | return self |
paul@939 | 341 | |
paul@941 | 342 | start_errors, end_errors = errors |
paul@941 | 343 | |
paul@952 | 344 | start = self.get_start() |
paul@952 | 345 | end = self.get_end() |
paul@952 | 346 | |
paul@939 | 347 | if start_errors: |
paul@939 | 348 | start = correct_datetime(start, permitted_values) |
paul@939 | 349 | if end_errors: |
paul@939 | 350 | end = correct_datetime(end, permitted_values) |
paul@939 | 351 | |
paul@939 | 352 | return self.make_corrected(start, end) |
paul@939 | 353 | |
paul@939 | 354 | def make_corrected(self, start, end): |
paul@939 | 355 | return self.__class__(start, end, self.tzid, self.origin) |
paul@939 | 356 | |
paul@543 | 357 | class RecurringPeriod(Period): |
paul@543 | 358 | |
paul@620 | 359 | """ |
paul@620 | 360 | A period with iCalendar metadata attributes and origin information from an |
paul@620 | 361 | object. |
paul@620 | 362 | """ |
paul@543 | 363 | |
paul@543 | 364 | def __init__(self, start, end, tzid=None, origin=None, start_attr=None, end_attr=None): |
paul@543 | 365 | Period.__init__(self, start, end, tzid, origin) |
paul@543 | 366 | self.start_attr = start_attr |
paul@543 | 367 | self.end_attr = end_attr |
paul@543 | 368 | |
paul@620 | 369 | def get_start_attr(self): |
paul@620 | 370 | return self.start_attr |
paul@543 | 371 | |
paul@620 | 372 | def get_end_attr(self): |
paul@620 | 373 | return self.end_attr |
paul@543 | 374 | |
paul@543 | 375 | def as_tuple(self): |
paul@543 | 376 | return self.start, self.end, self.tzid, self.origin, self.start_attr, self.end_attr |
paul@543 | 377 | |
paul@543 | 378 | def __repr__(self): |
paul@630 | 379 | return "RecurringPeriod%r" % (self.as_tuple(),) |
paul@543 | 380 | |
paul@939 | 381 | def make_corrected(self, start, end): |
paul@939 | 382 | return self.__class__(start, end, self.tzid, self.origin, self.get_start_attr(), self.get_end_attr()) |
paul@939 | 383 | |
paul@1189 | 384 | def get_overlapping(first, second): |
paul@1189 | 385 | |
paul@1189 | 386 | """ |
paul@1189 | 387 | Return the entries in the sorted 'first' collection that are overlapping |
paul@1189 | 388 | with the given sorted 'second' collection. |
paul@1189 | 389 | """ |
paul@1189 | 390 | |
paul@1189 | 391 | if not first or not second: |
paul@1189 | 392 | return [] |
paul@1189 | 393 | |
paul@1189 | 394 | # Examine each period in the second collection, attempting to match periods |
paul@1189 | 395 | # in the first collection. |
paul@1189 | 396 | |
paul@1189 | 397 | overlapping = set() |
paul@1189 | 398 | |
paul@1189 | 399 | for p2 in second: |
paul@1189 | 400 | last_point = p2.get_end_point() |
paul@1189 | 401 | |
paul@1189 | 402 | # Examine the first collection up to the point where no matches will |
paul@1189 | 403 | # occur. |
paul@1189 | 404 | |
paul@1189 | 405 | for p1 in first: |
paul@1189 | 406 | if p1.get_start_point() > last_point: |
paul@1189 | 407 | break |
paul@1189 | 408 | elif p1.overlaps(p2): |
paul@1189 | 409 | overlapping.add(p1) |
paul@1189 | 410 | |
paul@1189 | 411 | overlapping = list(overlapping) |
paul@1189 | 412 | overlapping.sort() |
paul@1189 | 413 | return overlapping |
paul@1189 | 414 | |
paul@1310 | 415 | def get_overlapping_members(periods): |
paul@1310 | 416 | |
paul@1310 | 417 | "Return members of the 'periods' collection that overlap with others." |
paul@1310 | 418 | |
paul@1310 | 419 | if not periods: |
paul@1310 | 420 | return [] |
paul@1310 | 421 | |
paul@1310 | 422 | l = periods[:] |
paul@1310 | 423 | l.sort() |
paul@1310 | 424 | |
paul@1310 | 425 | overlapping = [] |
paul@1310 | 426 | |
paul@1310 | 427 | last = l[0] |
paul@1310 | 428 | last_added = None |
paul@1310 | 429 | |
paul@1310 | 430 | for p in l[1:]: |
paul@1310 | 431 | if p.get_start_point() < last.get_end_point(): |
paul@1310 | 432 | if last_added != last: |
paul@1310 | 433 | overlapping.append(last) |
paul@1310 | 434 | overlapping.append(p) |
paul@1310 | 435 | last_added = p |
paul@1310 | 436 | last = p |
paul@1310 | 437 | |
paul@1310 | 438 | return overlapping |
paul@1310 | 439 | |
paul@529 | 440 | # Period layout. |
paul@204 | 441 | |
paul@884 | 442 | def get_scale(periods, tzid, view_period=None): |
paul@113 | 443 | |
paul@113 | 444 | """ |
paul@925 | 445 | Return a time scale from the given list of 'periods'. |
paul@153 | 446 | |
paul@162 | 447 | The given 'tzid' is used to make sure that the times are defined according |
paul@162 | 448 | to the chosen time zone. |
paul@162 | 449 | |
paul@884 | 450 | An optional 'view_period' is used to constrain the scale to the given |
paul@884 | 451 | period. |
paul@884 | 452 | |
paul@162 | 453 | The returned scale is a mapping from time to (starting, ending) tuples, |
paul@458 | 454 | where starting and ending are collections of periods. |
paul@113 | 455 | """ |
paul@113 | 456 | |
paul@113 | 457 | scale = {} |
paul@884 | 458 | view_start = view_period and to_timezone(view_period.get_start_point(), tzid) or None |
paul@884 | 459 | view_end = view_period and to_timezone(view_period.get_end_point(), tzid) or None |
paul@113 | 460 | |
paul@458 | 461 | for p in periods: |
paul@113 | 462 | |
paul@113 | 463 | # Add a point and this event to the starting list. |
paul@113 | 464 | |
paul@536 | 465 | start = to_timezone(p.get_start(), tzid) |
paul@884 | 466 | start = view_start and max(start, view_start) or start |
paul@536 | 467 | if not scale.has_key(start): |
paul@536 | 468 | scale[start] = [], [] |
paul@536 | 469 | scale[start][0].append(p) |
paul@113 | 470 | |
paul@113 | 471 | # Add a point and this event to the ending list. |
paul@113 | 472 | |
paul@536 | 473 | end = to_timezone(p.get_end(), tzid) |
paul@931 | 474 | end = view_end and min(end, view_end) or end |
paul@931 | 475 | if not scale.has_key(end): |
paul@931 | 476 | scale[end] = [], [] |
paul@931 | 477 | scale[end][1].append(p) |
paul@113 | 478 | |
paul@113 | 479 | return scale |
paul@113 | 480 | |
paul@455 | 481 | class Point: |
paul@455 | 482 | |
paul@455 | 483 | "A qualified point in time." |
paul@455 | 484 | |
paul@455 | 485 | PRINCIPAL, REPEATED = 0, 1 |
paul@455 | 486 | |
paul@455 | 487 | def __init__(self, point, indicator=None): |
paul@455 | 488 | self.point = point |
paul@455 | 489 | self.indicator = indicator or self.PRINCIPAL |
paul@455 | 490 | |
paul@455 | 491 | def __hash__(self): |
paul@455 | 492 | return hash((self.point, self.indicator)) |
paul@455 | 493 | |
paul@455 | 494 | def __cmp__(self, other): |
paul@455 | 495 | if isinstance(other, Point): |
paul@455 | 496 | return cmp((self.point, self.indicator), (other.point, other.indicator)) |
paul@455 | 497 | elif isinstance(other, datetime): |
paul@455 | 498 | return cmp(self.point, other) |
paul@455 | 499 | else: |
paul@455 | 500 | return 1 |
paul@455 | 501 | |
paul@455 | 502 | def __eq__(self, other): |
paul@455 | 503 | return self.__cmp__(other) == 0 |
paul@455 | 504 | |
paul@455 | 505 | def __ne__(self, other): |
paul@455 | 506 | return not self == other |
paul@455 | 507 | |
paul@455 | 508 | def __lt__(self, other): |
paul@455 | 509 | return self.__cmp__(other) < 0 |
paul@455 | 510 | |
paul@455 | 511 | def __le__(self, other): |
paul@455 | 512 | return self.__cmp__(other) <= 0 |
paul@455 | 513 | |
paul@455 | 514 | def __gt__(self, other): |
paul@455 | 515 | return not self <= other |
paul@455 | 516 | |
paul@455 | 517 | def __ge__(self, other): |
paul@455 | 518 | return not self < other |
paul@455 | 519 | |
paul@455 | 520 | def __repr__(self): |
paul@455 | 521 | return "Point(%r, Point.%s)" % (self.point, self.indicator and "REPEATED" or "PRINCIPAL") |
paul@452 | 522 | |
paul@162 | 523 | def get_slots(scale): |
paul@113 | 524 | |
paul@113 | 525 | """ |
paul@162 | 526 | Return an ordered list of time slots from the given 'scale'. |
paul@113 | 527 | |
paul@452 | 528 | Each slot is a tuple containing details of a point in time for the start of |
paul@458 | 529 | the slot, together with a list of parallel event periods. |
paul@452 | 530 | |
paul@455 | 531 | Each point in time is described as a Point representing the actual point in |
paul@455 | 532 | time together with an indicator of the nature of the point in time (as a |
paul@455 | 533 | principal point in a time scale or as a repeated point used to terminate |
paul@455 | 534 | events occurring for an instant in time). |
paul@113 | 535 | """ |
paul@113 | 536 | |
paul@113 | 537 | slots = [] |
paul@113 | 538 | active = [] |
paul@113 | 539 | |
paul@162 | 540 | points = scale.items() |
paul@162 | 541 | points.sort() |
paul@162 | 542 | |
paul@162 | 543 | for point, (starting, ending) in points: |
paul@449 | 544 | ending = set(ending) |
paul@449 | 545 | instants = ending.intersection(starting) |
paul@113 | 546 | |
paul@113 | 547 | # Discard all active events ending at or before this start time. |
paul@161 | 548 | # Free up the position in the active list. |
paul@113 | 549 | |
paul@449 | 550 | for t in ending.difference(instants): |
paul@113 | 551 | i = active.index(t) |
paul@113 | 552 | active[i] = None |
paul@113 | 553 | |
paul@161 | 554 | # For each event starting at the current point, fill any newly-vacated |
paul@161 | 555 | # position or add to the end of the active list. |
paul@161 | 556 | |
paul@113 | 557 | for t in starting: |
paul@113 | 558 | try: |
paul@113 | 559 | i = active.index(None) |
paul@113 | 560 | active[i] = t |
paul@113 | 561 | except ValueError: |
paul@113 | 562 | active.append(t) |
paul@113 | 563 | |
paul@161 | 564 | # Discard vacant positions from the end of the active list. |
paul@161 | 565 | |
paul@113 | 566 | while active and active[-1] is None: |
paul@113 | 567 | active.pop() |
paul@113 | 568 | |
paul@452 | 569 | # Add an entry for the time point before "instants". |
paul@452 | 570 | |
paul@455 | 571 | slots.append((Point(point), active[:])) |
paul@113 | 572 | |
paul@449 | 573 | # Discard events ending at the same time as they began. |
paul@449 | 574 | |
paul@449 | 575 | if instants: |
paul@449 | 576 | for t in instants: |
paul@449 | 577 | i = active.index(t) |
paul@449 | 578 | active[i] = None |
paul@449 | 579 | |
paul@449 | 580 | # Discard vacant positions from the end of the active list. |
paul@449 | 581 | |
paul@449 | 582 | while active and active[-1] is None: |
paul@449 | 583 | active.pop() |
paul@449 | 584 | |
paul@452 | 585 | # Add another entry for the time point after "instants". |
paul@449 | 586 | |
paul@455 | 587 | slots.append((Point(point, Point.REPEATED), active[:])) |
paul@449 | 588 | |
paul@113 | 589 | return slots |
paul@113 | 590 | |
paul@244 | 591 | def add_day_start_points(slots, tzid): |
paul@153 | 592 | |
paul@153 | 593 | """ |
paul@162 | 594 | Introduce into the 'slots' any day start points required by multi-day |
paul@244 | 595 | periods. The 'tzid' is required to make sure that appropriate time zones |
paul@244 | 596 | are chosen and not necessarily those provided by the existing time points. |
paul@153 | 597 | """ |
paul@153 | 598 | |
paul@162 | 599 | new_slots = [] |
paul@153 | 600 | current_date = None |
paul@200 | 601 | previously_active = [] |
paul@153 | 602 | |
paul@455 | 603 | for point, active in slots: |
paul@455 | 604 | start_of_day = get_start_of_day(point.point, tzid) |
paul@455 | 605 | this_date = point.point.date() |
paul@153 | 606 | |
paul@198 | 607 | # For each new day, add a slot for the start of the day where periods |
paul@198 | 608 | # are active and where no such slot already exists. |
paul@153 | 609 | |
paul@153 | 610 | if this_date != current_date: |
paul@414 | 611 | |
paul@414 | 612 | # Fill in days where events remain active. |
paul@414 | 613 | |
paul@414 | 614 | if current_date: |
paul@414 | 615 | current_date += timedelta(1) |
paul@414 | 616 | while current_date < this_date: |
paul@455 | 617 | new_slots.append((Point(get_start_of_day(current_date, tzid)), previously_active)) |
paul@414 | 618 | current_date += timedelta(1) |
paul@414 | 619 | else: |
paul@414 | 620 | current_date = this_date |
paul@153 | 621 | |
paul@153 | 622 | # Add any continuing periods. |
paul@153 | 623 | |
paul@455 | 624 | if point.point != start_of_day: |
paul@455 | 625 | new_slots.append((Point(start_of_day), previously_active)) |
paul@153 | 626 | |
paul@153 | 627 | # Add the currently active periods at this point in time. |
paul@153 | 628 | |
paul@153 | 629 | previously_active = active |
paul@153 | 630 | |
paul@162 | 631 | for t in new_slots: |
paul@162 | 632 | insort_left(slots, t) |
paul@162 | 633 | |
paul@931 | 634 | def remove_end_slot(slots, view_period): |
paul@931 | 635 | |
paul@931 | 636 | """ |
paul@931 | 637 | Remove from 'slots' any slot situated at the end of the given 'view_period'. |
paul@931 | 638 | """ |
paul@931 | 639 | |
paul@931 | 640 | end = view_period.get_end_point() |
paul@931 | 641 | if not end or not slots: |
paul@931 | 642 | return |
paul@931 | 643 | i = bisect_left(slots, (Point(end), None)) |
paul@931 | 644 | if i < len(slots): |
paul@931 | 645 | del slots[i:] |
paul@931 | 646 | |
paul@162 | 647 | def add_slots(slots, points): |
paul@162 | 648 | |
paul@162 | 649 | """ |
paul@162 | 650 | Introduce into the 'slots' entries for those in 'points' that are not |
paul@170 | 651 | already present, propagating active periods from time points preceding |
paul@170 | 652 | those added. |
paul@162 | 653 | """ |
paul@162 | 654 | |
paul@162 | 655 | new_slots = [] |
paul@162 | 656 | |
paul@162 | 657 | for point in points: |
paul@452 | 658 | i = bisect_left(slots, (point,)) # slots is [(point, active)...] |
paul@162 | 659 | if i < len(slots) and slots[i][0] == point: |
paul@162 | 660 | continue |
paul@162 | 661 | |
paul@170 | 662 | new_slots.append((point, i > 0 and slots[i-1][1] or [])) |
paul@162 | 663 | |
paul@162 | 664 | for t in new_slots: |
paul@162 | 665 | insort_left(slots, t) |
paul@162 | 666 | |
paul@162 | 667 | def partition_by_day(slots): |
paul@162 | 668 | |
paul@162 | 669 | """ |
paul@162 | 670 | Return a mapping from dates to time points provided by 'slots'. |
paul@162 | 671 | """ |
paul@162 | 672 | |
paul@162 | 673 | d = {} |
paul@162 | 674 | |
paul@455 | 675 | for point, value in slots: |
paul@455 | 676 | day = point.point.date() |
paul@162 | 677 | if not d.has_key(day): |
paul@162 | 678 | d[day] = [] |
paul@455 | 679 | d[day].append((point, value)) |
paul@162 | 680 | |
paul@162 | 681 | return d |
paul@153 | 682 | |
paul@876 | 683 | def add_empty_days(days, tzid, start=None, end=None): |
paul@279 | 684 | |
paul@876 | 685 | """ |
paul@876 | 686 | Add empty days to 'days' between busy days, and optionally from the given |
paul@876 | 687 | 'start' day and until the given 'end' day. |
paul@876 | 688 | """ |
paul@279 | 689 | |
paul@888 | 690 | last_day = start - timedelta(1) |
paul@279 | 691 | all_days = days.keys() |
paul@279 | 692 | all_days.sort() |
paul@279 | 693 | |
paul@279 | 694 | for day in all_days: |
paul@279 | 695 | if last_day: |
paul@279 | 696 | empty_day = last_day + timedelta(1) |
paul@279 | 697 | while empty_day < day: |
paul@455 | 698 | days[empty_day] = [(Point(get_start_of_day(empty_day, tzid)), None)] |
paul@279 | 699 | empty_day += timedelta(1) |
paul@876 | 700 | last_day = day |
paul@876 | 701 | |
paul@876 | 702 | if end: |
paul@876 | 703 | empty_day = last_day + timedelta(1) |
paul@876 | 704 | while empty_day < end: |
paul@876 | 705 | days[empty_day] = [(Point(get_start_of_day(empty_day, tzid)), None)] |
paul@876 | 706 | empty_day += timedelta(1) |
paul@279 | 707 | |
paul@114 | 708 | def get_spans(slots): |
paul@114 | 709 | |
paul@533 | 710 | "Inspect the given 'slots', returning a mapping of period keys to spans." |
paul@114 | 711 | |
paul@455 | 712 | points = [point for point, active in slots] |
paul@114 | 713 | spans = {} |
paul@114 | 714 | |
paul@449 | 715 | for _point, active in slots: |
paul@458 | 716 | for p in active: |
paul@458 | 717 | if p: |
paul@458 | 718 | key = p.get_key() |
paul@529 | 719 | start_slot = bisect_left(points, p.get_start()) |
paul@529 | 720 | end_slot = bisect_left(points, p.get_end()) |
paul@185 | 721 | spans[key] = end_slot - start_slot |
paul@114 | 722 | |
paul@114 | 723 | return spans |
paul@114 | 724 | |
paul@48 | 725 | # vim: tabstop=4 expandtab shiftwidth=4 |