1.1 --- a/EventAggregatorSupport/Filter.py Wed May 01 17:38:06 2013 +0200
1.2 +++ b/EventAggregatorSupport/Filter.py Fri May 03 16:37:36 2013 +0200
1.3 @@ -7,8 +7,16 @@
1.4 """
1.5
1.6 from DateSupport import DateTime, Timespan, TimespanCollection, \
1.7 - getCurrentDate, getCurrentMonth
1.8 -from MoinSupport import *
1.9 + getCurrentDate, getCurrentMonth, cmp_dates_as_day_start
1.10 +
1.11 +try:
1.12 + set
1.13 +except NameError:
1.14 + from sets import Set as set
1.15 +
1.16 +# Sortable values representing start and end limits of timespans/events.
1.17 +
1.18 +START, END = 0, 1
1.19
1.20 # Event filtering and limits.
1.21
1.22 @@ -189,38 +197,73 @@
1.23 """
1.24 Return a scale for the given coverage so that the times involved are
1.25 exposed. The scale consists of a list of non-overlapping timespans forming
1.26 - a contiguous period of time.
1.27 + a contiguous period of time, where each timespan is accompanied in a tuple
1.28 + by a limit and a list of original time details. Thus, the scale consists of
1.29 + (timespan, limit, set-of-times) tuples.
1.30 """
1.31
1.32 - times = set()
1.33 + times = {}
1.34 +
1.35 for timespan in coverage:
1.36 start, end = timespan.as_limits()
1.37
1.38 # Add either genuine times or dates converted to times.
1.39
1.40 if isinstance(start, DateTime):
1.41 - times.add(start)
1.42 + value = start
1.43 + key = value.to_utc(), START
1.44 else:
1.45 - times.add(start.as_start_of_day())
1.46 + value = start.as_start_of_day()
1.47 + key = value, START
1.48 +
1.49 + if not times.has_key(key):
1.50 + times[key] = set()
1.51 + times[key].add(value)
1.52
1.53 if isinstance(end, DateTime):
1.54 - times.add(end)
1.55 + value = end
1.56 + key = value.to_utc(), END
1.57 else:
1.58 - times.add(end.as_date().next_day())
1.59 + value = end.as_date().next_day()
1.60 + key = value, END
1.61
1.62 - times = list(times)
1.63 - times.sort(cmp_dates_as_day_start)
1.64 + if not times.has_key(key):
1.65 + times[key] = set()
1.66 + times[key].add(value)
1.67 +
1.68 + keys = times.keys()
1.69 + keys.sort(cmp_tuples_with_dates_as_day_start)
1.70
1.71 scale = []
1.72 first = 1
1.73 - start = None
1.74 - for time in times:
1.75 + start, start_limit = None, None
1.76 +
1.77 + for time, limit in keys:
1.78 if not first:
1.79 - scale.append(Timespan(start, time))
1.80 + scale.append((Timespan(start, time), limit, times[(start, start_limit)]))
1.81 else:
1.82 first = 0
1.83 - start = time
1.84 + start, start_limit = time, limit
1.85
1.86 return scale
1.87
1.88 +def cmp_tuples_with_dates_as_day_start(a, b):
1.89 +
1.90 + """
1.91 + Compare (datetime, limit) tuples, where identical datetimes are
1.92 + distinguished by the limit associated with them.
1.93 + """
1.94 +
1.95 + a_date, a_limit = a
1.96 + b_date, b_limit = b
1.97 + result = cmp_dates_as_day_start(a_date, b_date)
1.98 +
1.99 + if result == 0:
1.100 + if a_limit < b_limit:
1.101 + return -1
1.102 + else:
1.103 + return 1
1.104 +
1.105 + return result
1.106 +
1.107 # vim: tabstop=4 expandtab shiftwidth=4