paul@48 | 1 | #!/usr/bin/env python |
paul@48 | 2 | |
paul@48 | 3 | from bisect import bisect_left, insort_left |
paul@48 | 4 | |
paul@48 | 5 | # Time management. |
paul@48 | 6 | |
paul@72 | 7 | def have_conflict(freebusy, periods, get_conflicts=False): |
paul@72 | 8 | |
paul@72 | 9 | """ |
paul@72 | 10 | Return whether any period in 'freebusy' overlaps with the given 'periods', |
paul@72 | 11 | returning a collection of such overlapping periods if 'get_conflicts' is |
paul@72 | 12 | set to a true value. |
paul@72 | 13 | """ |
paul@72 | 14 | |
paul@72 | 15 | conflicts = [] |
paul@72 | 16 | for start, end in periods: |
paul@74 | 17 | overlapping = period_overlaps(freebusy, (start, end), get_conflicts) |
paul@74 | 18 | if overlapping: |
paul@72 | 19 | if get_conflicts: |
paul@74 | 20 | conflicts += overlapping |
paul@72 | 21 | else: |
paul@72 | 22 | return True |
paul@74 | 23 | |
paul@72 | 24 | if get_conflicts: |
paul@72 | 25 | return conflicts |
paul@72 | 26 | else: |
paul@72 | 27 | return False |
paul@72 | 28 | |
paul@48 | 29 | def insert_period(freebusy, period): |
paul@48 | 30 | insort_left(freebusy, period) |
paul@48 | 31 | |
paul@48 | 32 | def remove_period(freebusy, uid): |
paul@48 | 33 | i = 0 |
paul@48 | 34 | while i < len(freebusy): |
paul@48 | 35 | t = freebusy[i] |
paul@48 | 36 | if len(t) >= 3 and t[2] == uid: |
paul@48 | 37 | del freebusy[i] |
paul@48 | 38 | else: |
paul@48 | 39 | i += 1 |
paul@48 | 40 | |
paul@74 | 41 | def period_overlaps(freebusy, period, get_periods=False): |
paul@72 | 42 | |
paul@72 | 43 | """ |
paul@74 | 44 | Return whether any period in 'freebusy' overlaps with the given 'period', |
paul@74 | 45 | returning a collection of overlapping periods if 'get_periods' is set to a |
paul@74 | 46 | true value. |
paul@72 | 47 | """ |
paul@72 | 48 | |
paul@48 | 49 | dtstart, dtend = period[:2] |
paul@112 | 50 | found = bisect_left(freebusy, (dtstart, dtend, None, None)) |
paul@74 | 51 | |
paul@74 | 52 | overlapping = [] |
paul@74 | 53 | |
paul@74 | 54 | # Find earlier overlapping periods. |
paul@74 | 55 | |
paul@74 | 56 | i = found |
paul@74 | 57 | |
paul@74 | 58 | while i > 0 and freebusy[i - 1][1] > dtstart: |
paul@74 | 59 | if get_periods: |
paul@74 | 60 | overlapping.insert(0, freebusy[i - 1]) |
paul@74 | 61 | else: |
paul@74 | 62 | return True |
paul@74 | 63 | i -= 1 |
paul@74 | 64 | |
paul@74 | 65 | # Find later overlapping periods. |
paul@74 | 66 | |
paul@74 | 67 | i = found |
paul@74 | 68 | |
paul@74 | 69 | while i < len(freebusy) and (dtend is None or freebusy[i][0] < dtend): |
paul@74 | 70 | if get_periods: |
paul@74 | 71 | overlapping.append(freebusy[i]) |
paul@74 | 72 | else: |
paul@74 | 73 | return True |
paul@74 | 74 | i += 1 |
paul@74 | 75 | |
paul@74 | 76 | if get_periods: |
paul@74 | 77 | return overlapping |
paul@74 | 78 | else: |
paul@74 | 79 | return False |
paul@48 | 80 | |
paul@113 | 81 | # Period layout. |
paul@113 | 82 | |
paul@113 | 83 | def get_scale(l): |
paul@113 | 84 | |
paul@113 | 85 | """ |
paul@113 | 86 | Return an ordered time scale from the given list 'l' of tuples, with the |
paul@113 | 87 | first two elements of each tuple being start and end times. |
paul@113 | 88 | """ |
paul@113 | 89 | |
paul@113 | 90 | scale = {} |
paul@113 | 91 | |
paul@113 | 92 | for t in l: |
paul@113 | 93 | start, end = t[:2] |
paul@113 | 94 | |
paul@113 | 95 | # Add a point and this event to the starting list. |
paul@113 | 96 | |
paul@113 | 97 | if not scale.has_key(start): |
paul@113 | 98 | scale[start] = [], [] |
paul@113 | 99 | scale[start][0].append(t) |
paul@113 | 100 | |
paul@113 | 101 | # Add a point and this event to the ending list. |
paul@113 | 102 | |
paul@113 | 103 | if not scale.has_key(end): |
paul@113 | 104 | scale[end] = [], [] |
paul@113 | 105 | scale[end][1].append(t) |
paul@113 | 106 | |
paul@113 | 107 | scale = scale.items() |
paul@113 | 108 | scale.sort() |
paul@113 | 109 | return scale |
paul@113 | 110 | |
paul@113 | 111 | def get_slots(l): |
paul@113 | 112 | |
paul@113 | 113 | """ |
paul@113 | 114 | Return an ordered list of time slots from the given list 'l' of tuples, with |
paul@113 | 115 | the first two elements of each tuple being start and end times. |
paul@113 | 116 | |
paul@113 | 117 | Each slot is a tuple containing a point in time for the start of the slot, |
paul@113 | 118 | together with a list of parallel event tuples, each tuple containing the |
paul@113 | 119 | original details of an event. |
paul@113 | 120 | """ |
paul@113 | 121 | |
paul@113 | 122 | slots = [] |
paul@113 | 123 | active = [] |
paul@113 | 124 | |
paul@113 | 125 | for point, (starting, ending) in get_scale(l): |
paul@113 | 126 | |
paul@113 | 127 | # Discard all active events ending at or before this start time. |
paul@113 | 128 | |
paul@113 | 129 | for t in ending: |
paul@113 | 130 | i = active.index(t) |
paul@113 | 131 | active[i] = None |
paul@113 | 132 | |
paul@113 | 133 | for t in starting: |
paul@113 | 134 | try: |
paul@113 | 135 | i = active.index(None) |
paul@113 | 136 | active[i] = t |
paul@113 | 137 | except ValueError: |
paul@113 | 138 | active.append(t) |
paul@113 | 139 | |
paul@113 | 140 | while active and active[-1] is None: |
paul@113 | 141 | active.pop() |
paul@113 | 142 | |
paul@113 | 143 | slots.append((point, active[:])) |
paul@113 | 144 | |
paul@113 | 145 | return slots |
paul@113 | 146 | |
paul@114 | 147 | def get_spans(slots): |
paul@114 | 148 | |
paul@114 | 149 | "Inspect the given 'slots', returning a mapping of event uids to spans." |
paul@114 | 150 | |
paul@114 | 151 | points = [point for point, active in slots] |
paul@114 | 152 | spans = {} |
paul@114 | 153 | |
paul@114 | 154 | for point, active in slots: |
paul@114 | 155 | for t in active: |
paul@114 | 156 | if t: |
paul@114 | 157 | start, end, uid, transp = t |
paul@114 | 158 | start_slot = points.index(start) |
paul@114 | 159 | end_slot = points.index(end) |
paul@114 | 160 | spans[uid] = end_slot - start_slot |
paul@114 | 161 | |
paul@114 | 162 | return spans |
paul@114 | 163 | |
paul@48 | 164 | # vim: tabstop=4 expandtab shiftwidth=4 |