[CalendarServer-changes] [7164] CalDAVTester/branches/users/cdaboo/pycalendar/verifiers

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 9 12:41:01 PST 2011


Revision: 7164
          http://trac.macosforge.org/projects/calendarserver/changeset/7164
Author:   cdaboo at apple.com
Date:     2011-03-09 12:40:59 -0800 (Wed, 09 Mar 2011)
Log Message:
-----------
Switch to pycalendar.

Modified Paths:
--------------
    CalDAVTester/branches/users/cdaboo/pycalendar/verifiers/calendarDataMatch.py
    CalDAVTester/branches/users/cdaboo/pycalendar/verifiers/freeBusy.py
    CalDAVTester/branches/users/cdaboo/pycalendar/verifiers/postFreeBusy.py

Modified: CalDAVTester/branches/users/cdaboo/pycalendar/verifiers/calendarDataMatch.py
===================================================================
--- CalDAVTester/branches/users/cdaboo/pycalendar/verifiers/calendarDataMatch.py	2011-03-09 19:19:35 UTC (rev 7163)
+++ CalDAVTester/branches/users/cdaboo/pycalendar/verifiers/calendarDataMatch.py	2011-03-09 20:40:59 UTC (rev 7164)
@@ -14,11 +14,8 @@
 # limitations under the License.
 ##
 
-from vobject.base import readOne, ContentLine
-from vobject.base import Component
 from difflib import unified_diff
-import StringIO
-from vobject.icalendar import RecurringComponent, TimezoneComponent
+from pycalendar.calendar import PyCalendar
 
 """
 Verifier that checks the response body for an exact match to data in a file.
@@ -68,74 +65,38 @@
         
         def removePropertiesParameters(component):
             
-            for item in tuple(component.getChildren()):
-                if isinstance(item, Component):
-                    removePropertiesParameters(item)
-                elif isinstance(item, ContentLine):
-                    
-                    # Always remove DTSTAMP
-                    if item.name == "DTSTAMP":
-                        component.remove(item)
-                    elif item.name == "X-CALENDARSERVER-ATTENDEE-COMMENT":
-                        if item.params.has_key("X-CALENDARSERVER-DTSTAMP"):
-                            item.params["X-CALENDARSERVER-DTSTAMP"] = ["20080101T000000Z"]
-                            
-                    for filter in filters:
-                        if ":" in filter:
-                            property, parameter = filter.split(":")
-                            if item.name == property:
-                                if item.params.has_key(parameter):
-                                    del item.params[parameter]
-                        else:
-                            if item.name == filter:
-                                component.remove(item)
+            for component in component.getComponents():
+                removePropertiesParameters(component)
 
-        def normalizeRRULE(calobj):
-            
-            for component in calobj.getChildren():
-                if isinstance(component, RecurringComponent):
-                    rruleset = component.rruleset
-                    if rruleset:
-                        component.rruleset = rruleset
-                elif isinstance(component, TimezoneComponent):
-                    tzinfo = component.tzinfo
-                    if tzinfo:
-                        component.tzinfo = tzinfo
+            allProps = []
+            for properties in component.getProperties().itervalues():
+                allProps.extend(properties)
+            for property in allProps:                    
+                # Always remove DTSTAMP
+                if property.getName() == "DTSTAMP":
+                    component.removeProperty(property)
+                elif property.getName() == "X-CALENDARSERVER-ATTENDEE-COMMENT":
+                    if property.hasAttribute("X-CALENDARSERVER-DTSTAMP"):
+                        property.setAttribute("X-CALENDARSERVER-DTSTAMP", "20080101T000000Z")
+                        
+                for filter in filters:
+                    if ":" in filter:
+                        propname, parameter = filter.split(":")
+                        if property.getName() == propname:
+                            if property.hasAttribute(parameter):
+                                property.removeAttribute(parameter)
+                    else:
+                        if property.getName() == filter:
+                            component.removeProperty(property)
 
-        def sortComponents(calobj):
-            for compType in ('vevent', 'vtodo', 'vjournal', 'vavailability'):
-                try:
-                    comps = calobj.contents[compType]
-                    
-                    def _key(comp):
-                        try:
-                            uid = comp.contents['uid'][0].valueRepr()
-                        except KeyError:
-                            uid = ""
-                        try:
-                            rid = comp.contents['recurrence-id'][0].valueRepr()
-                        except KeyError:
-                            rid = ""
-                        return uid + ":" + rid
-        
-                    comps.sort(key=_key)
-                except KeyError:
-                    pass
-
-        s = StringIO.StringIO(respdata)
         try:
-            resp_calendar = readOne(s)
+            resp_calendar = PyCalendar.parseText(respdata)
             removePropertiesParameters(resp_calendar)
-            normalizeRRULE(resp_calendar)
-            sortComponents(resp_calendar)
-            respdata = resp_calendar.serialize()
+            respdata = resp_calendar.getText()
             
-            s = StringIO.StringIO(data)
-            data_calendar = readOne(s)
+            data_calendar = PyCalendar.parseText(data)
             removePropertiesParameters(data_calendar)
-            normalizeRRULE(data_calendar)
-            sortComponents(data_calendar)
-            data = data_calendar.serialize()
+            data = data_calendar.getText()
             
             result = respdata == data
                     

Modified: CalDAVTester/branches/users/cdaboo/pycalendar/verifiers/freeBusy.py
===================================================================
--- CalDAVTester/branches/users/cdaboo/pycalendar/verifiers/freeBusy.py	2011-03-09 19:19:35 UTC (rev 7163)
+++ CalDAVTester/branches/users/cdaboo/pycalendar/verifiers/freeBusy.py	2011-03-09 20:40:59 UTC (rev 7164)
@@ -17,12 +17,10 @@
 """
 Verifier that checks the response of a free-busy-query.
 """
-import StringIO
-from vobject.icalendar import periodToString
-import datetime
-from vobject.base import VObjectError
-from vobject.base import readOne
 
+from pycalendar.calendar import PyCalendar
+from pycalendar.exceptions import PyCalendarInvalidData
+
 class Verifier(object):
     
     def verify(self, manager, uri, response, respdata, args): #@UnusedVariable
@@ -38,37 +36,33 @@
         
         # Parse data as calendar object
         try:
-            s = StringIO.StringIO(respdata)
-            calendar = readOne(s)
+            calendar = PyCalendar.parseText(respdata)
             
             # Check for calendar
-            if calendar.name != "VCALENDAR":
-                raise ValueError("Top-level component is not a calendar: %s" % (calendar.name, ))
+            if calendar is None:
+                raise ValueError("Not a calendar: %s" % (respdata, ))
             
             # Only one component
-            comps = list(calendar.components())
+            comps = calendar.getComponents("VFREEBUSY")
             if len(comps) != 1:
-                raise ValueError("Wrong number of components in calendar")
+                raise ValueError("Wrong number or unexpected components in calendar")
             
             # Must be VFREEBUSY
             fb = comps[0]
-            if fb.name != "VFREEBUSY":
-                raise ValueError("Calendar contains unexpected component: %s" % (fb.name, ))
             
             # Extract periods
             busyp = []
             tentativep = []
             unavailablep = []
-            for fp in [x for x in fb.lines() if x.name == "FREEBUSY"]:
-                periods = fp.value
+            for fp in fb.getProperties("FREEBUSY"):
+                periods = fp.getValue().getValues()
                 # Convert start/duration to start/end
                 for i in range(len(periods)):
-                    if isinstance(periods[i][1], datetime.timedelta):
-                        periods[i] = (periods[i][0], periods[i][0] + periods[i][1])
+                    periods[i].getValue().setUseDuration(False)
                 # Check param
                 fbtype = "BUSY"
-                if "FBTYPE" in fp.params:
-                    fbtype = fp.params["FBTYPE"][0]
+                if fp.hasAttribute("FBTYPE"):
+                    fbtype = fp.getAttributeValue("FBTYPE")
                 if fbtype == "BUSY":
                     busyp.extend(periods)
                 elif fbtype == "BUSY-TENTATIVE":
@@ -86,13 +80,13 @@
             
             # Convert to string sets
             busy = set(busy)
-            busyp[:] = [periodToString(x) for x in busyp]
+            busyp = [x.getValue().getText() for x in busyp]
             busyp = set(busyp)
             tentative = set(tentative)
-            tentativep[:] = [periodToString(x) for x in tentativep]
+            tentativep = [x.getValue().getText() for x in tentativep]
             tentativep = set(tentativep)
             unavailable = set(unavailable)
-            unavailablep[:] = [periodToString(x) for x in unavailablep]
+            unavailablep = [x.getValue().getText() for x in unavailablep]
             unavailablep = set(unavailablep)
 
             # Compare all periods
@@ -103,7 +97,7 @@
             elif len(unavailablep.symmetric_difference(unavailable)):
                 raise ValueError("Busy-unavailable periods do not match")
                 
-        except VObjectError:
+        except PyCalendarInvalidData:
             return False, "        HTTP response data is not a calendar"
         except ValueError, txt:
             return False, "        HTTP response data is invalid: %s" % (txt,)

Modified: CalDAVTester/branches/users/cdaboo/pycalendar/verifiers/postFreeBusy.py
===================================================================
--- CalDAVTester/branches/users/cdaboo/pycalendar/verifiers/postFreeBusy.py	2011-03-09 19:19:35 UTC (rev 7163)
+++ CalDAVTester/branches/users/cdaboo/pycalendar/verifiers/postFreeBusy.py	2011-03-09 20:40:59 UTC (rev 7164)
@@ -18,13 +18,11 @@
 Verifier that checks the response of a free-busy-query.
 """
 
-from vobject.base import VObjectError
-from vobject.base import readOne
-from vobject.icalendar import periodToString
-import StringIO
-import datetime
+from pycalendar.calendar import PyCalendar
+from pycalendar.exceptions import PyCalendarInvalidData
 from xml.etree.ElementTree import ElementTree
 from xml.parsers.expat import ExpatError
+import StringIO
 
 class Verifier(object):
     
@@ -49,26 +47,23 @@
         for calendar in tree.findall("/{urn:ietf:params:xml:ns:caldav}response/{urn:ietf:params:xml:ns:caldav}calendar-data"):
             # Parse data as calendar object
             try:
-                s = StringIO.StringIO(calendar.text)
-                calendar = readOne(s)
+                calendar = PyCalendar.parseText(calendar.text)
                 
                 # Check for calendar
-                if calendar.name != "VCALENDAR":
-                    raise ValueError("Top-level component is not a calendar: %s" % (calendar.name, ))
+                if calendar is None:
+                    raise ValueError("Not a calendar: %s" % (calendar, ))
                 
                 # Only one component
-                comps = list(calendar.components())
+                comps = calendar.getComponents("VFREEBUSY")
                 if len(comps) != 1:
-                    raise ValueError("Wrong number of components in calendar")
+                    raise ValueError("Wrong number or unexpected components in calendar")
                 
                 # Must be VFREEBUSY
                 fb = comps[0]
-                if fb.name != "VFREEBUSY":
-                    raise ValueError("Calendar contains unexpected component: %s" % (fb.name, ))
                 
-                # Check for attendee value
-                for attendee in [x for x in fb.lines() if x.name == "ATTENDEE"]:
-                    if attendee.value in users:
+                    # Check for attendee value
+                for attendee in fb.getProperties("ATTENDEE"):
+                    if attendee.getValue().getValue() in users:
                         break
                 else:
                     continue
@@ -77,16 +72,15 @@
                 busyp = []
                 tentativep = []
                 unavailablep = []
-                for fp in [x for x in fb.lines() if x.name == "FREEBUSY"]:
-                    periods = fp.value
+                for fp in fb.getProperties("FREEBUSY"):
+                    periods = fp.getValue().getValues()
                     # Convert start/duration to start/end
                     for i in range(len(periods)):
-                        if isinstance(periods[i][1], datetime.timedelta):
-                            periods[i] = (periods[i][0], periods[i][0] + periods[i][1])
+                        periods[i].getValue().setUseDuration(False)
                     # Check param
                     fbtype = "BUSY"
-                    if "FBTYPE" in fp.params:
-                        fbtype = fp.params["FBTYPE"][0]
+                    if fp.hasAttribute("FBTYPE"):
+                        fbtype = fp.getAttributeValue("FBTYPE")
                     if fbtype == "BUSY":
                         busyp.extend(periods)
                     elif fbtype == "BUSY-TENTATIVE":
@@ -104,13 +98,13 @@
                 
                 # Convert to string sets
                 busy = set(busy)
-                busyp[:] = [periodToString(x) for x in busyp]
+                busyp = [x.getValue().getText() for x in busyp]
                 busyp = set(busyp)
                 tentative = set(tentative)
-                tentativep[:] = [periodToString(x) for x in tentativep]
+                tentativep = [x.getValue().getText() for x in tentativep]
                 tentativep = set(tentativep)
                 unavailable = set(unavailable)
-                unavailablep[:] = [periodToString(x) for x in unavailablep]
+                unavailablep = [x.getValue().getText() for x in unavailablep]
                 unavailablep = set(unavailablep)
     
                 # Compare all periods
@@ -123,7 +117,7 @@
                 
                 break
                     
-            except VObjectError:
+            except PyCalendarInvalidData:
                 return False, "        HTTP response data is not a calendar"
             except ValueError, txt:
                 return False, "        HTTP response data is invalid: %s" % (txt,)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110309/3aa7f8d8/attachment-0001.html>


More information about the calendarserver-changes mailing list