paul@347 | 1 | # -*- coding: iso-8859-1 -*- |
paul@347 | 2 | """ |
paul@347 | 3 | MoinMoin - EventAggregator event filtering functionality. |
paul@347 | 4 | |
paul@347 | 5 | @copyright: 2008, 2009, 2010, 2011, 2012, 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@347 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@347 | 7 | """ |
paul@347 | 8 | |
paul@347 | 9 | from DateSupport import DateTime, Timespan, TimespanCollection, \ |
paul@355 | 10 | getCurrentDate, getCurrentMonth, cmp_dates_as_day_start |
paul@355 | 11 | |
paul@355 | 12 | try: |
paul@355 | 13 | set |
paul@355 | 14 | except NameError: |
paul@355 | 15 | from sets import Set as set |
paul@355 | 16 | |
paul@355 | 17 | # Sortable values representing start and end limits of timespans/events. |
paul@355 | 18 | |
paul@355 | 19 | START, END = 0, 1 |
paul@347 | 20 | |
paul@347 | 21 | # Event filtering and limits. |
paul@347 | 22 | |
paul@347 | 23 | def getEventsInPeriod(events, calendar_period): |
paul@347 | 24 | |
paul@347 | 25 | """ |
paul@347 | 26 | Return a collection containing those of the given 'events' which occur |
paul@347 | 27 | within the given 'calendar_period'. |
paul@347 | 28 | """ |
paul@347 | 29 | |
paul@347 | 30 | all_shown_events = [] |
paul@347 | 31 | |
paul@347 | 32 | for event in events: |
paul@347 | 33 | |
paul@347 | 34 | # Test for the suitability of the event. |
paul@347 | 35 | |
paul@347 | 36 | if event.as_timespan() is not None: |
paul@347 | 37 | |
paul@347 | 38 | # Compare the dates to the requested calendar window, if any. |
paul@347 | 39 | |
paul@347 | 40 | if event in calendar_period: |
paul@347 | 41 | all_shown_events.append(event) |
paul@347 | 42 | |
paul@347 | 43 | return all_shown_events |
paul@347 | 44 | |
paul@347 | 45 | def getEventLimits(events): |
paul@347 | 46 | |
paul@347 | 47 | "Return the earliest and latest of the given 'events'." |
paul@347 | 48 | |
paul@347 | 49 | earliest = None |
paul@347 | 50 | latest = None |
paul@347 | 51 | |
paul@347 | 52 | for event in events: |
paul@347 | 53 | |
paul@347 | 54 | # Test for the suitability of the event. |
paul@347 | 55 | |
paul@347 | 56 | if event.as_timespan() is not None: |
paul@347 | 57 | ts = event.as_timespan() |
paul@347 | 58 | if earliest is None or ts.start < earliest: |
paul@347 | 59 | earliest = ts.start |
paul@347 | 60 | if latest is None or ts.end > latest: |
paul@347 | 61 | latest = ts.end |
paul@347 | 62 | |
paul@347 | 63 | return earliest, latest |
paul@347 | 64 | |
paul@347 | 65 | def getLatestEventTimestamp(events): |
paul@347 | 66 | |
paul@347 | 67 | """ |
paul@347 | 68 | Return the latest timestamp found from the given 'events'. |
paul@347 | 69 | """ |
paul@347 | 70 | |
paul@347 | 71 | latest = None |
paul@347 | 72 | |
paul@347 | 73 | for event in events: |
paul@347 | 74 | metadata = event.getMetadata() |
paul@347 | 75 | |
paul@347 | 76 | if latest is None or latest < metadata["last-modified"]: |
paul@347 | 77 | latest = metadata["last-modified"] |
paul@347 | 78 | |
paul@347 | 79 | return latest |
paul@347 | 80 | |
paul@347 | 81 | def getCalendarPeriod(calendar_start, calendar_end): |
paul@347 | 82 | |
paul@347 | 83 | """ |
paul@347 | 84 | Return a calendar period for the given 'calendar_start' and 'calendar_end'. |
paul@347 | 85 | These parameters can be given as None. |
paul@347 | 86 | """ |
paul@347 | 87 | |
paul@347 | 88 | # Re-order the window, if appropriate. |
paul@347 | 89 | |
paul@347 | 90 | if calendar_start is not None and calendar_end is not None and calendar_start > calendar_end: |
paul@347 | 91 | calendar_start, calendar_end = calendar_end, calendar_start |
paul@347 | 92 | |
paul@347 | 93 | return Timespan(calendar_start, calendar_end) |
paul@347 | 94 | |
paul@347 | 95 | def getConcretePeriod(calendar_start, calendar_end, earliest, latest, resolution): |
paul@347 | 96 | |
paul@347 | 97 | """ |
paul@347 | 98 | From the requested 'calendar_start' and 'calendar_end', which may be None, |
paul@347 | 99 | indicating that no restriction is imposed on the period for each of the |
paul@347 | 100 | boundaries, use the 'earliest' and 'latest' event months to define a |
paul@347 | 101 | specific period of interest. |
paul@347 | 102 | """ |
paul@347 | 103 | |
paul@347 | 104 | # Define the period as starting with any specified start month or the |
paul@347 | 105 | # earliest event known, ending with any specified end month or the latest |
paul@347 | 106 | # event known. |
paul@347 | 107 | |
paul@347 | 108 | first = calendar_start or earliest |
paul@347 | 109 | last = calendar_end or latest |
paul@347 | 110 | |
paul@347 | 111 | # If there is no range of months to show, perhaps because there are no |
paul@347 | 112 | # events in the requested period, and there was no start or end month |
paul@347 | 113 | # specified, show only the month indicated by the start or end of the |
paul@347 | 114 | # requested period. If all events were to be shown but none were found show |
paul@347 | 115 | # the current month. |
paul@347 | 116 | |
paul@347 | 117 | if resolution == "date": |
paul@347 | 118 | get_current = getCurrentDate |
paul@347 | 119 | else: |
paul@347 | 120 | get_current = getCurrentMonth |
paul@347 | 121 | |
paul@347 | 122 | if first is None: |
paul@347 | 123 | first = last or get_current() |
paul@347 | 124 | if last is None: |
paul@347 | 125 | last = first or get_current() |
paul@347 | 126 | |
paul@347 | 127 | if resolution == "month": |
paul@347 | 128 | first = first.as_month() |
paul@347 | 129 | last = last.as_month() |
paul@347 | 130 | |
paul@347 | 131 | # Permit "expiring" periods (where the start date approaches the end date). |
paul@347 | 132 | |
paul@347 | 133 | return min(first, last), last |
paul@347 | 134 | |
paul@347 | 135 | def getCoverage(events, resolution="date"): |
paul@347 | 136 | |
paul@347 | 137 | """ |
paul@347 | 138 | Determine the coverage of the given 'events', returning a collection of |
paul@347 | 139 | timespans, along with a dictionary mapping locations to collections of |
paul@347 | 140 | slots, where each slot contains a tuple of the form (timespans, events). |
paul@347 | 141 | """ |
paul@347 | 142 | |
paul@347 | 143 | all_events = {} |
paul@347 | 144 | full_coverage = TimespanCollection(resolution) |
paul@347 | 145 | |
paul@347 | 146 | # Get event details. |
paul@347 | 147 | |
paul@347 | 148 | for event in events: |
paul@347 | 149 | event_details = event.getDetails() |
paul@347 | 150 | |
paul@347 | 151 | # Find the coverage of this period for the event. |
paul@347 | 152 | |
paul@347 | 153 | # For day views, each location has its own slot, but for month |
paul@347 | 154 | # views, all locations are pooled together since having separate |
paul@347 | 155 | # slots for each location can lead to poor usage of vertical space. |
paul@347 | 156 | |
paul@347 | 157 | if resolution == "datetime": |
paul@347 | 158 | event_location = event_details.get("location") |
paul@347 | 159 | else: |
paul@347 | 160 | event_location = None |
paul@347 | 161 | |
paul@347 | 162 | # Update the overall coverage. |
paul@347 | 163 | |
paul@347 | 164 | full_coverage.insert_in_order(event) |
paul@347 | 165 | |
paul@347 | 166 | # Add a new events list for a new location. |
paul@347 | 167 | # Locations can be unspecified, thus None refers to all unlocalised |
paul@347 | 168 | # events. |
paul@347 | 169 | |
paul@347 | 170 | if not all_events.has_key(event_location): |
paul@347 | 171 | all_events[event_location] = [TimespanCollection(resolution, [event])] |
paul@347 | 172 | |
paul@347 | 173 | # Try and fit the event into an events list. |
paul@347 | 174 | |
paul@347 | 175 | else: |
paul@347 | 176 | slot = all_events[event_location] |
paul@347 | 177 | |
paul@347 | 178 | for slot_events in slot: |
paul@347 | 179 | |
paul@347 | 180 | # Where the event does not overlap with the events in the |
paul@347 | 181 | # current collection, add it alongside these events. |
paul@347 | 182 | |
paul@347 | 183 | if not event in slot_events: |
paul@347 | 184 | slot_events.insert_in_order(event) |
paul@347 | 185 | break |
paul@347 | 186 | |
paul@347 | 187 | # Make a new element in the list if the event cannot be |
paul@347 | 188 | # marked alongside existing events. |
paul@347 | 189 | |
paul@347 | 190 | else: |
paul@347 | 191 | slot.append(TimespanCollection(resolution, [event])) |
paul@347 | 192 | |
paul@347 | 193 | return full_coverage, all_events |
paul@347 | 194 | |
paul@347 | 195 | def getCoverageScale(coverage): |
paul@347 | 196 | |
paul@347 | 197 | """ |
paul@347 | 198 | Return a scale for the given coverage so that the times involved are |
paul@347 | 199 | exposed. The scale consists of a list of non-overlapping timespans forming |
paul@355 | 200 | a contiguous period of time, where each timespan is accompanied in a tuple |
paul@355 | 201 | by a limit and a list of original time details. Thus, the scale consists of |
paul@355 | 202 | (timespan, limit, set-of-times) tuples. |
paul@347 | 203 | """ |
paul@347 | 204 | |
paul@355 | 205 | times = {} |
paul@355 | 206 | |
paul@347 | 207 | for timespan in coverage: |
paul@347 | 208 | start, end = timespan.as_limits() |
paul@347 | 209 | |
paul@347 | 210 | # Add either genuine times or dates converted to times. |
paul@347 | 211 | |
paul@347 | 212 | if isinstance(start, DateTime): |
paul@355 | 213 | value = start |
paul@355 | 214 | key = value.to_utc(), START |
paul@347 | 215 | else: |
paul@355 | 216 | value = start.as_start_of_day() |
paul@355 | 217 | key = value, START |
paul@355 | 218 | |
paul@355 | 219 | if not times.has_key(key): |
paul@355 | 220 | times[key] = set() |
paul@355 | 221 | times[key].add(value) |
paul@347 | 222 | |
paul@347 | 223 | if isinstance(end, DateTime): |
paul@355 | 224 | value = end |
paul@355 | 225 | key = value.to_utc(), END |
paul@347 | 226 | else: |
paul@355 | 227 | value = end.as_date().next_day() |
paul@355 | 228 | key = value, END |
paul@347 | 229 | |
paul@355 | 230 | if not times.has_key(key): |
paul@355 | 231 | times[key] = set() |
paul@355 | 232 | times[key].add(value) |
paul@355 | 233 | |
paul@355 | 234 | keys = times.keys() |
paul@355 | 235 | keys.sort(cmp_tuples_with_dates_as_day_start) |
paul@347 | 236 | |
paul@347 | 237 | scale = [] |
paul@347 | 238 | first = 1 |
paul@355 | 239 | start, start_limit = None, None |
paul@355 | 240 | |
paul@355 | 241 | for time, limit in keys: |
paul@347 | 242 | if not first: |
paul@355 | 243 | scale.append((Timespan(start, time), limit, times[(start, start_limit)])) |
paul@347 | 244 | else: |
paul@347 | 245 | first = 0 |
paul@355 | 246 | start, start_limit = time, limit |
paul@347 | 247 | |
paul@347 | 248 | return scale |
paul@347 | 249 | |
paul@355 | 250 | def cmp_tuples_with_dates_as_day_start(a, b): |
paul@355 | 251 | |
paul@355 | 252 | """ |
paul@355 | 253 | Compare (datetime, limit) tuples, where identical datetimes are |
paul@355 | 254 | distinguished by the limit associated with them. |
paul@355 | 255 | """ |
paul@355 | 256 | |
paul@355 | 257 | a_date, a_limit = a |
paul@355 | 258 | b_date, b_limit = b |
paul@355 | 259 | result = cmp_dates_as_day_start(a_date, b_date) |
paul@355 | 260 | |
paul@355 | 261 | if result == 0: |
paul@355 | 262 | if a_limit < b_limit: |
paul@355 | 263 | return -1 |
paul@355 | 264 | else: |
paul@355 | 265 | return 1 |
paul@355 | 266 | |
paul@355 | 267 | return result |
paul@355 | 268 | |
paul@347 | 269 | # vim: tabstop=4 expandtab shiftwidth=4 |