1.1 --- a/imiptools/period.py Mon May 18 15:08:29 2015 +0200
1.2 +++ b/imiptools/period.py Mon May 18 17:13:46 2015 +0200
1.3 @@ -22,8 +22,11 @@
1.4 from bisect import bisect_left, bisect_right, insort_left
1.5 from datetime import date, datetime, timedelta
1.6 from imiptools.dates import format_datetime, get_datetime, \
1.7 - get_datetime_attributes, get_start_of_day, \
1.8 - get_tzid, to_timezone, to_utc_datetime
1.9 + get_datetime_attributes, \
1.10 + get_recurrence_start, get_recurrence_start_point, \
1.11 + get_start_of_day, \
1.12 + get_tzid, \
1.13 + to_timezone, to_utc_datetime
1.14
1.15 class Period:
1.16
1.17 @@ -110,10 +113,23 @@
1.18 self.summary = summary
1.19 self.organiser = organiser
1.20
1.21 - def as_tuple(self):
1.22 - return format_datetime(self.get_start_point()), \
1.23 - format_datetime(self.get_end_point()), \
1.24 - self.uid, self.transp, self.recurrenceid, self.summary, self.organiser
1.25 + def as_tuple(self, strings_only=False):
1.26 +
1.27 + """
1.28 + Return the initialisation parameter tuple, converting false value
1.29 + parameters to strings if 'strings_only' is set to a true value.
1.30 + """
1.31 +
1.32 + null = lambda x: (strings_only and [""] or [x])[0]
1.33 + return (
1.34 + format_datetime(self.get_start_point()),
1.35 + format_datetime(self.get_end_point()),
1.36 + self.uid or null(self.uid),
1.37 + self.transp or strings_only and "OPAQUE" or None,
1.38 + self.recurrenceid or null(self.recurrenceid),
1.39 + self.summary or null(self.summary),
1.40 + self.organiser or null(self.organiser)
1.41 + )
1.42
1.43 def __cmp__(self, other):
1.44
1.45 @@ -158,6 +174,35 @@
1.46 def __repr__(self):
1.47 return "RecurringPeriod(%r)" % (self.as_tuple(),)
1.48
1.49 +# Period and event recurrence logic.
1.50 +
1.51 +def is_replaced(period, recurrenceids, tzid):
1.52 +
1.53 + """
1.54 + Return whether 'period' refers to one of the 'recurrenceids', interpreted
1.55 + using 'tzid' if necessary.
1.56 + """
1.57 +
1.58 + for s in recurrenceids:
1.59 + if is_affected(period, s, tzid):
1.60 + return s
1.61 + return None
1.62 +
1.63 +def is_affected(period, recurrenceid, tzid):
1.64 +
1.65 + """
1.66 + Return whether 'period' refer to 'recurrenceid', interpreted using 'tzid' if
1.67 + necessary.
1.68 + """
1.69 +
1.70 + if not recurrenceid:
1.71 + return None
1.72 + d = get_recurrence_start(recurrenceid)
1.73 + dt = get_recurrence_start_point(recurrenceid, tzid)
1.74 + if period.get_start() == d or period.get_start_point() == dt:
1.75 + return recurrenceid
1.76 + return None
1.77 +
1.78 # Time and period management.
1.79
1.80 def can_schedule(freebusy, periods, uid, recurrenceid):