[CalendarServer-changes] [14245] PyCalendar/trunk/src/pycalendar/icalendar

source_changes at macosforge.org source_changes at macosforge.org
Tue Jan 6 08:20:23 PST 2015


Revision: 14245
          http://trac.calendarserver.org//changeset/14245
Author:   cdaboo at apple.com
Date:     2015-01-06 08:20:22 -0800 (Tue, 06 Jan 2015)
Log Message:
-----------
Addition recurrence-related api.

Modified Paths:
--------------
    PyCalendar/trunk/src/pycalendar/icalendar/calendar.py
    PyCalendar/trunk/src/pycalendar/icalendar/tests/test_calendar.py

Modified: PyCalendar/trunk/src/pycalendar/icalendar/calendar.py
===================================================================
--- PyCalendar/trunk/src/pycalendar/icalendar/calendar.py	2015-01-06 16:20:06 UTC (rev 14244)
+++ PyCalendar/trunk/src/pycalendar/icalendar/calendar.py	2015-01-06 16:20:22 UTC (rev 14245)
@@ -351,6 +351,80 @@
                 del self.mMasterComponentsByTypeAndUID[component.getType()][uid]
 
 
+    def deriveComponent(self, recurrenceID):
+        """
+        Derive an overridden component for the associated RECURRENCE-ID. This assumes
+        that the R-ID is valid for the actual recurrence being used.
+
+        @param recurrenceID: the recurrence instance
+        @type recurrenceID: L{DateTime}
+
+        @return: the derived component
+        @rtype: L{ComponentRecur} or L{None}
+        """
+        master = self.masterComponent()
+        if master is None:
+            return None
+
+        # Create the derived instance
+        newcomp = master.duplicate()
+
+        # Strip out unwanted recurrence properties
+        for propname in (
+            definitions.cICalProperty_RRULE,
+            definitions.cICalProperty_RDATE,
+            definitions.cICalProperty_EXRULE,
+            definitions.cICalProperty_EXDATE,
+            definitions.cICalProperty_RECURRENCE_ID,
+        ):
+            newcomp.removeProperties(propname)
+
+        # New DTSTART is the RECURRENCE-ID we are deriving but adjusted to the
+        # original DTSTART's localtime
+        dtstart = newcomp.getStart()
+        dtend = newcomp.getEnd()
+        oldduration = dtend - dtstart
+
+        newdtstartValue = recurrenceID.duplicate()
+        if not dtstart.isDateOnly():
+            if dtstart.local():
+                newdtstartValue.adjustTimezone(dtstart.getTimezone())
+        else:
+            newdtstartValue.setDateOnly(True)
+
+        newcomp.removeProperties(definitions.cICalProperty_DTSTART)
+        newcomp.removeProperties(definitions.cICalProperty_DTEND)
+        prop = Property(definitions.cICalProperty_DTSTART, newdtstartValue)
+        newcomp.addProperty(prop)
+        if not newcomp.useDuration():
+            prop = Property(definitions.cICalProperty_DTEND, newdtstartValue + oldduration)
+            newcomp.addProperty(prop)
+
+        newcomp.addProperty(Property("RECURRENCE-ID", newdtstartValue))
+
+        # After creating/changing a component we need to do this to keep PyCalendar happy
+        newcomp.finalise()
+
+        return newcomp
+
+
+    def masterComponent(self):
+        """
+        Return the first sub-component of a recurring type that represents the master
+        instance.
+
+        @return: the master component
+        @rtype: L{ComponentRecur} or L{None}
+        """
+        for component in self.getComponents():
+            if isinstance(component, ComponentRecur):
+                rid = component.getRecurrenceID()
+                if rid is None:
+                    return component
+        else:
+            return None
+
+
     def getText(self, includeTimezones=None, format=None):
 
         if format is None or format == self.sFormatText:

Modified: PyCalendar/trunk/src/pycalendar/icalendar/tests/test_calendar.py
===================================================================
--- PyCalendar/trunk/src/pycalendar/icalendar/tests/test_calendar.py	2015-01-06 16:20:06 UTC (rev 14244)
+++ PyCalendar/trunk/src/pycalendar/icalendar/tests/test_calendar.py	2015-01-06 16:20:22 UTC (rev 14245)
@@ -20,6 +20,7 @@
 from pycalendar.icalendar.property import Property
 from pycalendar.parser import ParserContext
 from pycalendar.period import Period
+from pycalendar.timezone import Timezone
 import cStringIO as StringIO
 import difflib
 import unittest
@@ -852,3 +853,390 @@
             )
             instances = tuple([instance.getInstanceStart() for instance in instances])
             self.assertEqual(instances, result, "Failed in %s: got %s, expected %s" % (title, instances, result))
+
+
+    def testMasterComponent(self):
+
+        data = (
+            (
+                "1.1 Non-recurring no VTIMEZONE",
+                """BEGIN:VCALENDAR
+VERSION:2.0
+CALSCALE:GREGORIAN
+PRODID:-//mulberrymail.com//Mulberry v4.0//EN
+BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+DTSTART;VALUE=DATE:20110601
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+END:VEVENT
+END:VCALENDAR
+""".replace("\n", "\r\n"),
+                """BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+DTSTART;VALUE=DATE:20110601
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+END:VEVENT
+""".replace("\n", "\r\n"),
+            ),
+            (
+                "1.2 Non-recurring with VTIMEZONE",
+                """BEGIN:VCALENDAR
+VERSION:2.0
+CALSCALE:GREGORIAN
+PRODID:-//mulberrymail.com//Mulberry v4.0//EN
+BEGIN:VTIMEZONE
+TZID:Etc/GMT+1
+X-LIC-LOCATION:Etc/GMT+1
+BEGIN:STANDARD
+DTSTART:18000101T000000
+RDATE:18000101T000000
+TZNAME:GMT+1
+TZOFFSETFROM:-0100
+TZOFFSETTO:-0100
+END:STANDARD
+END:VTIMEZONE
+BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+DTSTART;TZID=Etc/GMT+1:20110601T000000
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+END:VEVENT
+END:VCALENDAR
+""".replace("\n", "\r\n"),
+                """BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+DTSTART;TZID=Etc/GMT+1:20110601T000000
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+END:VEVENT
+""".replace("\n", "\r\n"),
+            ),
+            (
+                "2.1 Recurring no VTIMEZONE",
+                """BEGIN:VCALENDAR
+VERSION:2.0
+CALSCALE:GREGORIAN
+PRODID:-//mulberrymail.com//Mulberry v4.0//EN
+BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+DTSTART;VALUE=DATE:20110601
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+RRULE:FREQ=DAILY
+SUMMARY:New Year's Day
+END:VEVENT
+BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+RECURRENCE-ID;VALUE=DATE:20110602
+DTSTART;VALUE=DATE:20110602
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+END:VEVENT
+END:VCALENDAR
+""".replace("\n", "\r\n"),
+                """BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+DTSTART;VALUE=DATE:20110601
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+RRULE:FREQ=DAILY
+SUMMARY:New Year's Day
+END:VEVENT
+""".replace("\n", "\r\n"),
+            ),
+            (
+                "2.2 Recurring with VTIMEZONE",
+                """BEGIN:VCALENDAR
+VERSION:2.0
+CALSCALE:GREGORIAN
+PRODID:-//mulberrymail.com//Mulberry v4.0//EN
+BEGIN:VTIMEZONE
+TZID:Etc/GMT+1
+X-LIC-LOCATION:Etc/GMT+1
+BEGIN:STANDARD
+DTSTART:18000101T000000
+RDATE:18000101T000000
+TZNAME:GMT+1
+TZOFFSETFROM:-0100
+TZOFFSETTO:-0100
+END:STANDARD
+END:VTIMEZONE
+BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+DTSTART;TZID=Etc/GMT+1:20110601T000000
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+RRULE:FREQ=DAILY
+END:VEVENT
+BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+RECURRENCE-ID;TZID=Etc/GMT+1:20110602T000000
+DTSTART;TZID=Etc/GMT+1:20110602T000000
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+END:VEVENT
+END:VCALENDAR
+""".replace("\n", "\r\n"),
+                """BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+DTSTART;TZID=Etc/GMT+1:20110601T000000
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+RRULE:FREQ=DAILY
+SUMMARY:New Year's Day
+END:VEVENT
+""".replace("\n", "\r\n"),
+            ),
+            (
+                "3.1 Recurring no master, no VTIMEZONE",
+                """BEGIN:VCALENDAR
+VERSION:2.0
+CALSCALE:GREGORIAN
+PRODID:-//mulberrymail.com//Mulberry v4.0//EN
+BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+RECURRENCE-ID;VALUE=DATE:20110602
+DTSTART;VALUE=DATE:20110602
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+END:VEVENT
+END:VCALENDAR
+""".replace("\n", "\r\n"),
+                "",
+            ),
+            (
+                "3.2 Recurring no master, with VTIMEZONE",
+                """BEGIN:VCALENDAR
+VERSION:2.0
+CALSCALE:GREGORIAN
+PRODID:-//mulberrymail.com//Mulberry v4.0//EN
+BEGIN:VTIMEZONE
+TZID:Etc/GMT+1
+X-LIC-LOCATION:Etc/GMT+1
+BEGIN:STANDARD
+DTSTART:18000101T000000
+RDATE:18000101T000000
+TZNAME:GMT+1
+TZOFFSETFROM:-0100
+TZOFFSETTO:-0100
+END:STANDARD
+END:VTIMEZONE
+BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+RECURRENCE-ID;TZID=Etc/GMT+1:20110602T000000
+DTSTART;TZID=Etc/GMT+1:20110602T000000
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+END:VEVENT
+END:VCALENDAR
+""".replace("\n", "\r\n"),
+                "",
+            ),
+        )
+
+        for title, caldata, result in data:
+            calendar = Calendar.parseText(caldata)
+            master = calendar.masterComponent()
+            if master is None:
+                master = ""
+            self.assertEqual(str(master), result, "Failed in %s: got %s, expected %s" % (title, master, result))
+
+
+    def testDeriveComponent(self):
+
+        data = (
+            (
+                "1.1 Recurring no VTIMEZONE",
+                """BEGIN:VCALENDAR
+VERSION:2.0
+CALSCALE:GREGORIAN
+PRODID:-//mulberrymail.com//Mulberry v4.0//EN
+BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+DTSTART;VALUE=DATE:20110601
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+RRULE:FREQ=DAILY
+SUMMARY:New Year's Day
+END:VEVENT
+BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+RECURRENCE-ID;VALUE=DATE:20110602
+DTSTART;VALUE=DATE:20110602
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+END:VEVENT
+END:VCALENDAR
+""".replace("\n", "\r\n"),
+                DateTime(2011, 6, 3),
+                """BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+RECURRENCE-ID;VALUE=DATE:20110603
+DTSTART;VALUE=DATE:20110603
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+END:VEVENT
+""".replace("\n", "\r\n"),
+            ),
+            (
+                "2.2 Recurring with VTIMEZONE",
+                """BEGIN:VCALENDAR
+VERSION:2.0
+CALSCALE:GREGORIAN
+PRODID:-//mulberrymail.com//Mulberry v4.0//EN
+BEGIN:VTIMEZONE
+TZID:Etc/GMT+1
+X-LIC-LOCATION:Etc/GMT+1
+BEGIN:STANDARD
+DTSTART:18000101T000000
+RDATE:18000101T000000
+TZNAME:GMT+1
+TZOFFSETFROM:-0100
+TZOFFSETTO:-0100
+END:STANDARD
+END:VTIMEZONE
+BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+DTSTART;TZID=Etc/GMT+1:20110601T000000
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+RRULE:FREQ=DAILY
+END:VEVENT
+BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+RECURRENCE-ID;TZID=Etc/GMT+1:20110602T000000
+DTSTART;TZID=Etc/GMT+1:20110602T000000
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+END:VEVENT
+END:VCALENDAR
+""".replace("\n", "\r\n"),
+                DateTime(2011, 6, 3, 1, 0, 0, Timezone(utc=True)),
+                """BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+RECURRENCE-ID;TZID=Etc/GMT+1:20110603T000000
+DTSTART;TZID=Etc/GMT+1:20110603T000000
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+END:VEVENT
+""".replace("\n", "\r\n"),
+            ),
+            (
+                "2.3 Recurring with VTIMEZONE, DTEND",
+                """BEGIN:VCALENDAR
+VERSION:2.0
+CALSCALE:GREGORIAN
+PRODID:-//mulberrymail.com//Mulberry v4.0//EN
+BEGIN:VTIMEZONE
+TZID:Etc/GMT+1
+X-LIC-LOCATION:Etc/GMT+1
+BEGIN:STANDARD
+DTSTART:18000101T000000
+RDATE:18000101T000000
+TZNAME:GMT+1
+TZOFFSETFROM:-0100
+TZOFFSETTO:-0100
+END:STANDARD
+END:VTIMEZONE
+BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+DTSTART;TZID=Etc/GMT+1:20110601T000000
+DTEND;TZID=Etc/GMT+1:20110601T020000
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+RRULE:FREQ=DAILY
+END:VEVENT
+BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+RECURRENCE-ID;TZID=Etc/GMT+1:20110602T000000
+DTSTART;TZID=Etc/GMT+1:20110602T000000
+DTEND;TZID=Etc/GMT+1:20110602T020000
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+END:VEVENT
+END:VCALENDAR
+""".replace("\n", "\r\n"),
+                DateTime(2011, 6, 3, 1, 0, 0, Timezone(utc=True)),
+                """BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+RECURRENCE-ID;TZID=Etc/GMT+1:20110603T000000
+DTSTART;TZID=Etc/GMT+1:20110603T000000
+DTEND;TZID=Etc/GMT+1:20110603T020000
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+END:VEVENT
+""".replace("\n", "\r\n"),
+            ),
+            (
+                "2.1 Recurring no master, no VTIMEZONE",
+                """BEGIN:VCALENDAR
+VERSION:2.0
+CALSCALE:GREGORIAN
+PRODID:-//mulberrymail.com//Mulberry v4.0//EN
+BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+RECURRENCE-ID;VALUE=DATE:20110602
+DTSTART;VALUE=DATE:20110602
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+END:VEVENT
+END:VCALENDAR
+""".replace("\n", "\r\n"),
+                DateTime(2011, 6, 3),
+                "",
+            ),
+            (
+                "2.2 Recurring no master, with VTIMEZONE",
+                """BEGIN:VCALENDAR
+VERSION:2.0
+CALSCALE:GREGORIAN
+PRODID:-//mulberrymail.com//Mulberry v4.0//EN
+BEGIN:VTIMEZONE
+TZID:Etc/GMT+1
+X-LIC-LOCATION:Etc/GMT+1
+BEGIN:STANDARD
+DTSTART:18000101T000000
+RDATE:18000101T000000
+TZNAME:GMT+1
+TZOFFSETFROM:-0100
+TZOFFSETTO:-0100
+END:STANDARD
+END:VTIMEZONE
+BEGIN:VEVENT
+UID:C3184A66-1ED0-11D9-A5E0-000A958A3252
+RECURRENCE-ID;TZID=Etc/GMT+1:20110602T000000
+DTSTART;TZID=Etc/GMT+1:20110602T000000
+DURATION:P1D
+DTSTAMP:20020101T000000Z
+SUMMARY:New Year's Day
+END:VEVENT
+END:VCALENDAR
+""".replace("\n", "\r\n"),
+                DateTime(2011, 6, 3, 1, 0, 0, Timezone(utc=True)),
+                "",
+            ),
+        )
+
+        for title, caldata, rid, result in data:
+            calendar = Calendar.parseText(caldata)
+            master = calendar.deriveComponent(rid)
+            if master is None:
+                master = ""
+            self.assertEqual(str(master), result, "Failed in %s: got %s, expected %s" % (title, master, result))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20150106/a43530a9/attachment-0001.html>


More information about the calendarserver-changes mailing list