[CalendarServer-changes] [10988] PyCalendar/trunk/src/pycalendar

source_changes at macosforge.org source_changes at macosforge.org
Wed Apr 3 08:19:39 PDT 2013


Revision: 10988
          http://trac.calendarserver.org//changeset/10988
Author:   cdaboo at apple.com
Date:     2013-04-03 08:19:39 -0700 (Wed, 03 Apr 2013)
Log Message:
-----------
Handle case of REQUEST-STATUS with a single value element.

Modified Paths:
--------------
    PyCalendar/trunk/src/pycalendar/parser.py
    PyCalendar/trunk/src/pycalendar/requeststatusvalue.py
    PyCalendar/trunk/src/pycalendar/tests/test_property.py
    PyCalendar/trunk/src/pycalendar/tests/test_requeststatus.py
    PyCalendar/trunk/src/pycalendar/utils.py

Modified: PyCalendar/trunk/src/pycalendar/parser.py
===================================================================
--- PyCalendar/trunk/src/pycalendar/parser.py	2013-04-03 03:25:14 UTC (rev 10987)
+++ PyCalendar/trunk/src/pycalendar/parser.py	2013-04-03 15:19:39 UTC (rev 10988)
@@ -1,5 +1,5 @@
 ##
-#    Copyright (c) 2011-2012 Cyrus Daboo. All rights reserved.
+#    Copyright (c) 2011-2013 Cyrus Daboo. All rights reserved.
 #
 #    Licensed under the Apache License, Version 2.0 (the "License");
 #    you may not use this file except in compliance with the License.
@@ -52,7 +52,7 @@
     # Truncate over long ADR and N values
     INVALID_ADR_N_VALUES = PARSER_FIX
 
-    # REQUEST-STATUS values with \; as the first separator
+    # REQUEST-STATUS values with \; as the first separator or single element
     INVALID_REQUEST_STATUS_VALUE = PARSER_FIX
 
     # Remove \-escaping in URI values when parsing - only PARSER_FIX or PARSER_ALLOW
@@ -72,3 +72,4 @@
         ParserContext.INVALID_ADR_N_VALUES = ParserContext.PARSER_RAISE
         ParserContext.INVALID_REQUEST_STATUS_VALUE = ParserContext.PARSER_RAISE
         ParserContext.BACKSLASH_IN_URI_VALUE = ParserContext.PARSER_RAISE
+        ParserContext.INVALID_REQUEST_STATUS = ParserContext.PARSER_RAISE

Modified: PyCalendar/trunk/src/pycalendar/requeststatusvalue.py
===================================================================
--- PyCalendar/trunk/src/pycalendar/requeststatusvalue.py	2013-04-03 03:25:14 UTC (rev 10987)
+++ PyCalendar/trunk/src/pycalendar/requeststatusvalue.py	2013-04-03 15:19:39 UTC (rev 10988)
@@ -1,5 +1,5 @@
 ##
-#    Copyright (c) 2011-2012 Cyrus Daboo. All rights reserved.
+#    Copyright (c) 2011-2013 Cyrus Daboo. All rights reserved.
 #
 #    Licensed under the Apache License, Version 2.0 (the "License");
 #    you may not use this file except in compliance with the License.
@@ -44,47 +44,40 @@
 
     def parse(self, data):
 
-        # Split fields based on ;
-        code, rest = data.split(";", 1)
+        result = utils.parseTextList(data, always_list=True)
+        if len(result) == 1:
+            if ParserContext.INVALID_REQUEST_STATUS_VALUE != ParserContext.PARSER_RAISE:
+                if ";" in result[0]:
+                    code, desc = result[0].split(";", 1)
+                else:
+                    code = result[0]
+                    desc = ""
+                rest = None
+            else:
+                raise ValueError
+        elif len(result) == 2:
+            code, desc = result
+            rest = None
+        elif len(result) == 3:
+            code, desc, rest = result
+        else:
+            if ParserContext.INVALID_REQUEST_STATUS_VALUE != ParserContext.PARSER_RAISE:
+                code, desc, rest = result[:3]
+            else:
+                raise ValueError
 
         if "\\" in code and ParserContext.INVALID_REQUEST_STATUS_VALUE in (ParserContext.PARSER_IGNORE, ParserContext.PARSER_FIX):
             code = code.replace("\\", "")
         elif ParserContext.INVALID_REQUEST_STATUS_VALUE == ParserContext.PARSER_RAISE:
             raise ValueError
 
-        # The next two items are text with possible \; sequences so we have to punt on those
-        desc = ""
-        semicolon = rest.find(";")
-        while semicolon != -1:
-            if rest[semicolon - 1] == "\\":
-                desc += rest[:semicolon + 1]
-                rest = rest[semicolon + 1:]
-                semicolon = rest.find(";")
-            else:
-                desc += rest[:semicolon]
-                rest = rest[semicolon + 1:]
-                break
-
-        if semicolon == -1:
-            desc += rest
-            rest = ""
-
         # Decoding required
-        self.mValue = [code, utils.decodeTextValue(desc), utils.decodeTextValue(rest) if rest else None]
+        self.mValue = [code, desc, rest, ] if rest else [code, desc, ]
 
 
     # os - StringIO object
     def generate(self, os):
-        try:
-            # Encoding required
-            utils.writeTextValue(os, self.mValue[0])
-            os.write(";")
-            utils.writeTextValue(os, self.mValue[1])
-            if len(self.mValue) == 3 and self.mValue[2]:
-                os.write(";")
-                utils.writeTextValue(os, self.mValue[2])
-        except:
-            pass
+        utils.generateTextList(os, self.mValue if len(self.mValue) < 3 or self.mValue[2] else self.mValue[:2])
 
 
     def writeXML(self, node, namespace):

Modified: PyCalendar/trunk/src/pycalendar/tests/test_property.py
===================================================================
--- PyCalendar/trunk/src/pycalendar/tests/test_property.py	2013-04-03 03:25:14 UTC (rev 10987)
+++ PyCalendar/trunk/src/pycalendar/tests/test_property.py	2013-04-03 15:19:39 UTC (rev 10988)
@@ -89,7 +89,6 @@
             "FREEBUSY:20060226T120000Z/ABC",
             "SUMMARY:Some \\qtext",
             "RRULE:FREQ=MONTHLY;COUNT=3;BYDAY=TU,WE,VE;BYSETPOS=-1",
-            "REQUEST-STATUS:2.0,Success",
             "TZOFFSETFROM:-050",
             """ATTENDEE;CN="\\";CUTYPE=INDIVIDUAL;PARTSTAT=X-UNDELIVERABLE:invalid:nomai
  l""",

Modified: PyCalendar/trunk/src/pycalendar/tests/test_requeststatus.py
===================================================================
--- PyCalendar/trunk/src/pycalendar/tests/test_requeststatus.py	2013-04-03 03:25:14 UTC (rev 10987)
+++ PyCalendar/trunk/src/pycalendar/tests/test_requeststatus.py	2013-04-03 15:19:39 UTC (rev 10988)
@@ -1,5 +1,5 @@
 ##
-#    Copyright (c) 2011-2012 Cyrus Daboo. All rights reserved.
+#    Copyright (c) 2011-2013 Cyrus Daboo. All rights reserved.
 #
 #    Licensed under the Apache License, Version 2.0 (the "License");
 #    you may not use this file except in compliance with the License.
@@ -58,6 +58,26 @@
         ParserContext.INVALID_REQUEST_STATUS_VALUE = oldContext
 
 
+    def testTruncatedValue(self):
+
+        bad_value = "2.0"
+        ok_value = "2.0;"
+
+        # Fix the value
+        oldContext = ParserContext.INVALID_REQUEST_STATUS_VALUE
+        ParserContext.INVALID_REQUEST_STATUS_VALUE = ParserContext.PARSER_FIX
+        req = PyCalendarRequestStatusValue()
+        req.parse(bad_value)
+        self.assertEqual(req.getText(), ok_value, "Failed to parse and re-generate '%s'" % (bad_value,))
+
+        # Raise the value
+        ParserContext.INVALID_REQUEST_STATUS_VALUE = ParserContext.PARSER_RAISE
+        req = PyCalendarRequestStatusValue()
+        self.assertRaises(ValueError, req.parse, bad_value)
+
+        ParserContext.INVALID_REQUEST_STATUS_VALUE = oldContext
+
+
     def testParseProperty(self):
 
         items = (

Modified: PyCalendar/trunk/src/pycalendar/utils.py
===================================================================
--- PyCalendar/trunk/src/pycalendar/utils.py	2013-04-03 03:25:14 UTC (rev 10987)
+++ PyCalendar/trunk/src/pycalendar/utils.py	2013-04-03 15:19:39 UTC (rev 10988)
@@ -1,5 +1,5 @@
 ##
-#    Copyright (c) 2007-2012 Cyrus Daboo. All rights reserved.
+#    Copyright (c) 2007-2013 Cyrus Daboo. All rights reserved.
 #
 #    Licensed under the Apache License, Version 2.0 (the "License");
 #    you may not use this file except in compliance with the License.
@@ -255,7 +255,7 @@
 
 
 # vCard text list parsing/generation
-def parseTextList(data, sep=';'):
+def parseTextList(data, sep=';', always_list=False):
     """
     Each element of the list has to be separately un-escaped
     """
@@ -272,7 +272,7 @@
 
     results.append(decodeTextValue("".join(item)))
 
-    return tuple(results) if len(results) > 1 else (results[0] if len(results) else "")
+    return tuple(results) if len(results) > 1 or always_list else (results[0] if len(results) else "")
 
 
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130403/0b03b8a6/attachment-0001.html>


More information about the calendarserver-changes mailing list