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