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@146 | 6 | Copyright (C) 2014, 2015 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@48 | 22 | from bisect import bisect_left, insort_left |
paul@153 | 23 | from imiptools.dates import get_datetime, get_start_of_day, to_timezone |
paul@48 | 24 | |
paul@48 | 25 | # Time management. |
paul@48 | 26 | |
paul@72 | 27 | def have_conflict(freebusy, periods, get_conflicts=False): |
paul@72 | 28 | |
paul@72 | 29 | """ |
paul@72 | 30 | Return whether any period in 'freebusy' overlaps with the given 'periods', |
paul@72 | 31 | returning a collection of such overlapping periods if 'get_conflicts' is |
paul@72 | 32 | set to a true value. |
paul@72 | 33 | """ |
paul@72 | 34 | |
paul@72 | 35 | conflicts = [] |
paul@72 | 36 | for start, end in periods: |
paul@74 | 37 | overlapping = period_overlaps(freebusy, (start, end), get_conflicts) |
paul@74 | 38 | if overlapping: |
paul@72 | 39 | if get_conflicts: |
paul@74 | 40 | conflicts += overlapping |
paul@72 | 41 | else: |
paul@72 | 42 | return True |
paul@74 | 43 | |
paul@72 | 44 | if get_conflicts: |
paul@72 | 45 | return conflicts |
paul@72 | 46 | else: |
paul@72 | 47 | return False |
paul@72 | 48 | |
paul@48 | 49 | def insert_period(freebusy, period): |
paul@48 | 50 | insort_left(freebusy, period) |
paul@48 | 51 | |
paul@48 | 52 | def remove_period(freebusy, uid): |
paul@48 | 53 | i = 0 |
paul@48 | 54 | while i < len(freebusy): |
paul@48 | 55 | t = freebusy[i] |
paul@48 | 56 | if len(t) >= 3 and t[2] == uid: |
paul@48 | 57 | del freebusy[i] |
paul@48 | 58 | else: |
paul@48 | 59 | i += 1 |
paul@48 | 60 | |
paul@74 | 61 | def period_overlaps(freebusy, period, get_periods=False): |
paul@72 | 62 | |
paul@72 | 63 | """ |
paul@74 | 64 | Return whether any period in 'freebusy' overlaps with the given 'period', |
paul@74 | 65 | returning a collection of overlapping periods if 'get_periods' is set to a |
paul@74 | 66 | true value. |
paul@72 | 67 | """ |
paul@72 | 68 | |
paul@48 | 69 | dtstart, dtend = period[:2] |
paul@112 | 70 | found = bisect_left(freebusy, (dtstart, dtend, None, None)) |
paul@74 | 71 | |
paul@74 | 72 | overlapping = [] |
paul@74 | 73 | |
paul@74 | 74 | # Find earlier overlapping periods. |
paul@74 | 75 | |
paul@74 | 76 | i = found |
paul@74 | 77 | |
paul@74 | 78 | while i > 0 and freebusy[i - 1][1] > dtstart: |
paul@74 | 79 | if get_periods: |
paul@74 | 80 | overlapping.insert(0, freebusy[i - 1]) |
paul@74 | 81 | else: |
paul@74 | 82 | return True |
paul@74 | 83 | i -= 1 |
paul@74 | 84 | |
paul@74 | 85 | # Find later overlapping periods. |
paul@74 | 86 | |
paul@74 | 87 | i = found |
paul@74 | 88 | |
paul@74 | 89 | while i < len(freebusy) and (dtend is None or freebusy[i][0] < dtend): |
paul@74 | 90 | if get_periods: |
paul@74 | 91 | overlapping.append(freebusy[i]) |
paul@74 | 92 | else: |
paul@74 | 93 | return True |
paul@74 | 94 | i += 1 |
paul@74 | 95 | |
paul@74 | 96 | if get_periods: |
paul@74 | 97 | return overlapping |
paul@74 | 98 | else: |
paul@74 | 99 | return False |
paul@48 | 100 | |
paul@113 | 101 | # Period layout. |
paul@113 | 102 | |
paul@113 | 103 | def get_scale(l): |
paul@113 | 104 | |
paul@113 | 105 | """ |
paul@113 | 106 | Return an ordered time scale from the given list 'l' of tuples, with the |
paul@113 | 107 | first two elements of each tuple being start and end times. |
paul@153 | 108 | |
paul@153 | 109 | The returned scale is a collection of (time, (starting, ending)) tuples, |
paul@153 | 110 | where starting and ending are collections of tuples from 'l'. |
paul@113 | 111 | """ |
paul@113 | 112 | |
paul@113 | 113 | scale = {} |
paul@113 | 114 | |
paul@113 | 115 | for t in l: |
paul@113 | 116 | start, end = t[:2] |
paul@113 | 117 | |
paul@113 | 118 | # Add a point and this event to the starting list. |
paul@113 | 119 | |
paul@113 | 120 | if not scale.has_key(start): |
paul@113 | 121 | scale[start] = [], [] |
paul@113 | 122 | scale[start][0].append(t) |
paul@113 | 123 | |
paul@113 | 124 | # Add a point and this event to the ending list. |
paul@113 | 125 | |
paul@113 | 126 | if not scale.has_key(end): |
paul@113 | 127 | scale[end] = [], [] |
paul@113 | 128 | scale[end][1].append(t) |
paul@113 | 129 | |
paul@113 | 130 | scale = scale.items() |
paul@113 | 131 | scale.sort() |
paul@113 | 132 | return scale |
paul@113 | 133 | |
paul@113 | 134 | def get_slots(l): |
paul@113 | 135 | |
paul@113 | 136 | """ |
paul@113 | 137 | Return an ordered list of time slots from the given list 'l' of tuples, with |
paul@113 | 138 | the first two elements of each tuple being start and end times. |
paul@113 | 139 | |
paul@113 | 140 | Each slot is a tuple containing a point in time for the start of the slot, |
paul@113 | 141 | together with a list of parallel event tuples, each tuple containing the |
paul@113 | 142 | original details of an event. |
paul@113 | 143 | """ |
paul@113 | 144 | |
paul@113 | 145 | slots = [] |
paul@113 | 146 | active = [] |
paul@113 | 147 | |
paul@113 | 148 | for point, (starting, ending) in get_scale(l): |
paul@113 | 149 | |
paul@113 | 150 | # Discard all active events ending at or before this start time. |
paul@113 | 151 | |
paul@113 | 152 | for t in ending: |
paul@113 | 153 | i = active.index(t) |
paul@113 | 154 | active[i] = None |
paul@113 | 155 | |
paul@113 | 156 | for t in starting: |
paul@113 | 157 | try: |
paul@113 | 158 | i = active.index(None) |
paul@113 | 159 | active[i] = t |
paul@113 | 160 | except ValueError: |
paul@113 | 161 | active.append(t) |
paul@113 | 162 | |
paul@113 | 163 | while active and active[-1] is None: |
paul@113 | 164 | active.pop() |
paul@113 | 165 | |
paul@113 | 166 | slots.append((point, active[:])) |
paul@113 | 167 | |
paul@113 | 168 | return slots |
paul@113 | 169 | |
paul@153 | 170 | def partition_slots(slots, tzid): |
paul@153 | 171 | |
paul@153 | 172 | """ |
paul@153 | 173 | Partition the given 'slots' into separate collections having a date-level |
paul@153 | 174 | resolution, using the given 'tzid' to make sure that the day boundaries are |
paul@153 | 175 | defined according to the chosen time zone. |
paul@153 | 176 | |
paul@153 | 177 | Return a collection of (date, slots) tuples. |
paul@153 | 178 | """ |
paul@153 | 179 | |
paul@153 | 180 | partitioned = {} |
paul@153 | 181 | current = None |
paul@153 | 182 | current_date = None |
paul@153 | 183 | previously_active = None |
paul@153 | 184 | |
paul@153 | 185 | for point, active in slots: |
paul@153 | 186 | dt = to_timezone(get_datetime(point), tzid) |
paul@153 | 187 | start_of_day = get_start_of_day(dt) |
paul@153 | 188 | this_date = dt.date() |
paul@153 | 189 | |
paul@153 | 190 | # For each new day, create a partition of the original collection. |
paul@153 | 191 | |
paul@153 | 192 | if this_date != current_date: |
paul@153 | 193 | current_date = this_date |
paul@153 | 194 | partitioned[current_date] = current = [] |
paul@153 | 195 | |
paul@153 | 196 | # Add any continuing periods. |
paul@153 | 197 | |
paul@153 | 198 | if dt != start_of_day and previously_active: |
paul@153 | 199 | current.append((start_of_day, previously_active)) |
paul@153 | 200 | |
paul@153 | 201 | # Add the currently active periods at this point in time. |
paul@153 | 202 | |
paul@153 | 203 | current.append((point, active)) |
paul@153 | 204 | previously_active = active |
paul@153 | 205 | |
paul@153 | 206 | partitioned = partitioned.items() |
paul@153 | 207 | partitioned.sort() |
paul@153 | 208 | return partitioned |
paul@153 | 209 | |
paul@114 | 210 | def get_spans(slots): |
paul@114 | 211 | |
paul@114 | 212 | "Inspect the given 'slots', returning a mapping of event uids to spans." |
paul@114 | 213 | |
paul@114 | 214 | points = [point for point, active in slots] |
paul@114 | 215 | spans = {} |
paul@114 | 216 | |
paul@114 | 217 | for point, active in slots: |
paul@114 | 218 | for t in active: |
paul@114 | 219 | if t: |
paul@115 | 220 | start, end, uid = t[:3] |
paul@153 | 221 | try: |
paul@153 | 222 | start_slot = points.index(start) |
paul@153 | 223 | except ValueError: |
paul@153 | 224 | start_slot = 0 |
paul@153 | 225 | try: |
paul@153 | 226 | end_slot = points.index(end) |
paul@153 | 227 | except ValueError: |
paul@153 | 228 | end_slot = len(slots) |
paul@114 | 229 | spans[uid] = end_slot - start_slot |
paul@114 | 230 | |
paul@114 | 231 | return spans |
paul@114 | 232 | |
paul@48 | 233 | # vim: tabstop=4 expandtab shiftwidth=4 |