1.1 --- a/imiptools/data.py Wed Feb 04 15:30:03 2015 +0100
1.2 +++ b/imiptools/data.py Wed Feb 04 19:13:22 2015 +0100
1.3 @@ -19,9 +19,11 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 +from datetime import datetime, timedelta
1.8 from email.mime.text import MIMEText
1.9 -from imiptools.dates import get_datetime, to_utc_datetime
1.10 +from imiptools.dates import format_datetime, get_datetime, to_utc_datetime
1.11 from vCalendar import iterwrite, parse, ParseError, to_dict, to_node
1.12 +from vRecurrence import get_parameters, get_rule
1.13 import email.utils
1.14
1.15 try:
1.16 @@ -71,6 +73,11 @@
1.17 def __delitem__(self, name):
1.18 del self.details[name]
1.19
1.20 + # Computed results.
1.21 +
1.22 + def get_periods(self, window_size=100):
1.23 + return get_periods(self, window_size)
1.24 +
1.25 # Construction and serialisation.
1.26
1.27 def make_calendar(nodes, method=None):
1.28 @@ -259,4 +266,45 @@
1.29
1.30 return is_same_sequence and partstat_set or not is_old_sequence
1.31
1.32 +# NOTE: Need to expose the 100 day window for recurring events in the
1.33 +# NOTE: configuration.
1.34 +
1.35 +def get_periods(obj, window_size=100):
1.36 +
1.37 + """
1.38 + Return periods for the given object 'obj', confining materialised periods
1.39 + to the given 'window_size' in days starting from the present moment.
1.40 + """
1.41 +
1.42 + dtstart = obj.get_utc_datetime("DTSTART")
1.43 + dtend = obj.get_utc_datetime("DTEND")
1.44 +
1.45 + # NOTE: Need also DURATION support.
1.46 +
1.47 + duration = dtend - dtstart
1.48 +
1.49 + # Recurrence rules create multiple instances to be checked.
1.50 + # Conflicts may only be assessed within a period defined by policy
1.51 + # for the agent, with instances outside that period being considered
1.52 + # unchecked.
1.53 +
1.54 + window_end = datetime.now() + timedelta(window_size)
1.55 +
1.56 + # NOTE: Need also RDATE and EXDATE support.
1.57 +
1.58 + rrule = obj.get_value("RRULE")
1.59 +
1.60 + if rrule:
1.61 + selector = get_rule(dtstart, rrule)
1.62 + parameters = get_parameters(rrule)
1.63 + periods = []
1.64 + for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")):
1.65 + start = datetime(*start, tzinfo=timezone("UTC"))
1.66 + end = start + duration
1.67 + periods.append((format_datetime(start), format_datetime(end)))
1.68 + else:
1.69 + periods = [(format_datetime(dtstart), format_datetime(dtend))]
1.70 +
1.71 + return periods
1.72 +
1.73 # vim: tabstop=4 expandtab shiftwidth=4