# HG changeset patch # User Paul Boddie # Date 1423001799 -3600 # Node ID 8a1c1d5c53eb9c32de2da0d2a817e35c476b2268 # Parent d19ab80f54656f4a4c009c765126b6fff91679d7 Moved various period-related functions to the period module. diff -r d19ab80f5465 -r 8a1c1d5c53eb imiptools/content.py --- a/imiptools/content.py Tue Feb 03 23:16:13 2015 +0100 +++ b/imiptools/content.py Tue Feb 03 23:16:39 2015 +0100 @@ -26,11 +26,13 @@ from imiptools.data import Object, parse_object, \ get_address, get_uri, get_value, \ is_new_object, uri_dict, uri_item -from imiptools.dates import * -from imiptools.period import can_schedule, insert_period, remove_period +from imiptools.dates import format_datetime, to_timezone +from imiptools.period import can_schedule, insert_period, remove_period, \ + get_periods, remove_from_freebusy, \ + remove_from_freebusy_for_other, \ + update_freebusy, update_freebusy_for_other from pytz import timezone from socket import gethostname -from vRecurrence import get_parameters, get_rule import imip_store try: @@ -38,99 +40,6 @@ except ImportError: from StringIO import StringIO -# NOTE: Need to expose the 100 day window for recurring events in the -# NOTE: configuration. - -def get_periods(obj, window_size=100): - - """ - Return periods for the given object 'obj', confining materialised periods - to the given 'window_size' in days starting from the present moment. - """ - - dtstart = obj.get_utc_datetime("DTSTART") - dtend = obj.get_utc_datetime("DTEND") - - # NOTE: Need also DURATION support. - - duration = dtend - dtstart - - # Recurrence rules create multiple instances to be checked. - # Conflicts may only be assessed within a period defined by policy - # for the agent, with instances outside that period being considered - # unchecked. - - window_end = datetime.now() + timedelta(window_size) - - # NOTE: Need also RDATE and EXDATE support. - - rrule = obj.get_value("RRULE") - - if rrule: - selector = get_rule(dtstart, rrule) - parameters = get_parameters(rrule) - periods = [] - for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")): - start = datetime(*start, tzinfo=timezone("UTC")) - end = start + duration - periods.append((format_datetime(start), format_datetime(end))) - else: - periods = [(format_datetime(dtstart), format_datetime(dtend))] - - return periods - -def remove_from_freebusy(freebusy, attendee, uid, store): - - """ - For the given 'attendee', remove periods from 'freebusy' that are associated - with 'uid' in the 'store'. - """ - - remove_period(freebusy, uid) - store.set_freebusy(attendee, freebusy) - -def remove_from_freebusy_for_other(freebusy, user, other, uid, store): - - """ - For the given 'user', remove for the 'other' party periods from 'freebusy' - that are associated with 'uid' in the 'store'. - """ - - remove_period(freebusy, uid) - store.set_freebusy_for_other(user, freebusy, other) - -def _update_freebusy(freebusy, periods, transp, uid): - - """ - Update the free/busy details with the given 'periods', 'transp' setting and - 'uid'. - """ - - remove_period(freebusy, uid) - - for start, end in periods: - insert_period(freebusy, (start, end, uid, transp)) - -def update_freebusy(freebusy, attendee, periods, transp, uid, store): - - """ - For the given 'attendee', update the free/busy details with the given - 'periods', 'transp' setting and 'uid' in the 'store'. - """ - - _update_freebusy(freebusy, periods, transp, uid) - store.set_freebusy(attendee, freebusy) - -def update_freebusy_for_other(freebusy, user, other, periods, transp, uid, store): - - """ - For the given 'user', update the free/busy details of 'other' with the given - 'periods', 'transp' setting and 'uid' in the 'store'. - """ - - _update_freebusy(freebusy, periods, transp, uid) - store.set_freebusy_for_other(user, freebusy, other) - # Handler mechanism objects. def handle_itip_part(part, handlers): diff -r d19ab80f5465 -r 8a1c1d5c53eb imiptools/period.py --- a/imiptools/period.py Tue Feb 03 23:16:13 2015 +0100 +++ b/imiptools/period.py Tue Feb 03 23:16:39 2015 +0100 @@ -20,8 +20,10 @@ """ from bisect import bisect_left, insort_left -from datetime import datetime -from imiptools.dates import get_datetime, get_start_of_day, to_timezone +from datetime import datetime, timedelta +from imiptools.dates import format_datetime, get_datetime, get_start_of_day, \ + to_timezone +from vRecurrence import get_parameters, get_rule # Time management with datetime strings. @@ -328,4 +330,98 @@ return start, end, uid, key +# NOTE: Need to expose the 100 day window for recurring events in the +# NOTE: configuration. + +def get_periods(obj, window_size=100): + + """ + Return periods for the given object 'obj', confining materialised periods + to the given 'window_size' in days starting from the present moment. + """ + + dtstart = obj.get_utc_datetime("DTSTART") + dtend = obj.get_utc_datetime("DTEND") + + # NOTE: Need also DURATION support. + + duration = dtend - dtstart + + # Recurrence rules create multiple instances to be checked. + # Conflicts may only be assessed within a period defined by policy + # for the agent, with instances outside that period being considered + # unchecked. + + window_end = datetime.now() + timedelta(window_size) + + # NOTE: Need also RDATE and EXDATE support. + + rrule = obj.get_value("RRULE") + + if rrule: + selector = get_rule(dtstart, rrule) + parameters = get_parameters(rrule) + periods = [] + for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")): + start = datetime(*start, tzinfo=timezone("UTC")) + end = start + duration + periods.append((format_datetime(start), format_datetime(end))) + else: + periods = [(format_datetime(dtstart), format_datetime(dtend))] + + return periods + +def remove_from_freebusy(freebusy, attendee, uid, store): + + """ + For the given 'attendee', remove periods from 'freebusy' that are associated + with 'uid' in the 'store'. + """ + + remove_period(freebusy, uid) + store.set_freebusy(attendee, freebusy) + +def remove_from_freebusy_for_other(freebusy, user, other, uid, store): + + """ + For the given 'user', remove for the 'other' party periods from 'freebusy' + that are associated with 'uid' in the 'store'. + """ + + remove_period(freebusy, uid) + store.set_freebusy_for_other(user, freebusy, other) + +def _update_freebusy(freebusy, periods, transp, uid): + + """ + Update the free/busy details with the given 'periods', 'transp' setting and + 'uid'. + """ + + remove_period(freebusy, uid) + + for start, end in periods: + insert_period(freebusy, (start, end, uid, transp)) + +def update_freebusy(freebusy, attendee, periods, transp, uid, store): + + """ + For the given 'attendee', update the free/busy details with the given + 'periods', 'transp' setting and 'uid' in the 'store'. + """ + + _update_freebusy(freebusy, periods, transp, uid) + store.set_freebusy(attendee, freebusy) + +def update_freebusy_for_other(freebusy, user, other, periods, transp, uid, store): + + """ + For the given 'user', update the free/busy details of 'other' with the given + 'periods', 'transp' setting and 'uid' in the 'store'. + """ + + _update_freebusy(freebusy, periods, transp, uid) + store.set_freebusy_for_other(user, freebusy, other) + + # vim: tabstop=4 expandtab shiftwidth=4