[CalendarServer-changes] [9866] CalendarServer/trunk/calendarserver/tools/validcalendardata.py

source_changes at macosforge.org source_changes at macosforge.org
Thu Sep 27 08:49:22 PDT 2012


Revision: 9866
          http://trac.calendarserver.org//changeset/9866
Author:   cdaboo at apple.com
Date:     2012-09-27 08:49:22 -0700 (Thu, 27 Sep 2012)
Log Message:
-----------
Add option to only validate parsing and not check CalDAV constraints. That can be used for checking an arbitrary
.ics file, not just a CalDAV calendar object resource.

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/tools/validcalendardata.py

Modified: CalendarServer/trunk/calendarserver/tools/validcalendardata.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/validcalendardata.py	2012-09-27 00:06:04 UTC (rev 9865)
+++ CalendarServer/trunk/calendarserver/tools/validcalendardata.py	2012-09-27 15:49:22 UTC (rev 9866)
@@ -17,7 +17,7 @@
 
 """
 This tool takes data from stdin and validates it as iCalendar data suitable
-for the server. 
+for the server.
 """
 
 from calendarserver.tools.cmdline import utilityMain
@@ -25,7 +25,7 @@
 from twisted.python.text import wordWrap
 from twisted.python.usage import Options
 from twistedcaldav.config import config
-from twistedcaldav.ical import Component
+from twistedcaldav.ical import Component, InvalidICalendarDataError
 from twistedcaldav.stdconfig import DEFAULT_CONFIG_FILE
 import os
 import sys
@@ -62,6 +62,7 @@
 
     optFlags = [
         ['verbose', 'v', "Verbose logging."],
+        ['parse-only', 'p', "Only validate parsing of the data."],
     ]
 
     optParameters = [
@@ -91,8 +92,9 @@
         if self.outputName == '-':
             return sys.stdout
         else:
-            return open(self.outputName, 'wb')
+            return open(self.outputName, "wb")
 
+
     def opt_input(self, filename):
         """
         Specify output file path (default: '-', meaning stdin).
@@ -109,7 +111,7 @@
         if self.inputName == '-':
             return sys.stdin
         else:
-            return open(os.path.expanduser(self.inputName), 'rb')
+            return open(os.path.expanduser(self.inputName), "rb")
 
 
 
@@ -122,10 +124,10 @@
 
     def __init__(self, store, options, output, input, reactor, config):
         super(ValidService, self).__init__()
-        self.store   = store
+        self.store = store
         self.options = options
-        self.output  = output
-        self.input  = input
+        self.output = output
+        self.input = input
         self.reactor = reactor
         self.config = config
         self._directory = None
@@ -136,8 +138,11 @@
         Start the service.
         """
         super(ValidService, self).startService()
-        result, message = self.validCalendarData()
-        
+        if self.options["parse-only"]:
+            result, message = self.parseCalendarData()
+        else:
+            result, message = self.validCalendarData()
+
         if result:
             print "Calendar data OK"
         else:
@@ -145,11 +150,38 @@
         self.reactor.stop()
 
 
+    def parseCalendarData(self):
+        """
+        Check the calendar data for valid iCalendar data.
+        """
+
+        result = True
+        message = ""
+        try:
+            component = Component.fromString(self.input.read())
+
+            # Do underlying iCalendar library validation with data fix
+            fixed, unfixed = component._pycalendar.validate(doFix=True)
+
+            if unfixed:
+                raise InvalidICalendarDataError("Calendar data had unfixable problems:\n  %s" % ("\n  ".join(unfixed),))
+            if fixed:
+                print "Calendar data had fixable problems:\n  %s" % ("\n  ".join(fixed),)
+
+        except ValueError, e:
+            result = False
+            message = str(e)
+            if message.startswith(errorPrefix):
+                message = message[len(errorPrefix):]
+
+        return (result, message,)
+
+
     def validCalendarData(self):
         """
         Check the calendar data for valid iCalendar data.
         """
-    
+
         result = True
         message = ""
         truncated = False
@@ -167,10 +199,11 @@
                 message = message[len(errorPrefix):]
             if truncated:
                 message = "Calendar data RRULE truncated\n" + message
-    
+
         return (result, message,)
 
 
+
 def main(argv=sys.argv, stderr=sys.stderr, reactor=None):
     """
     Do the export.
@@ -189,9 +222,14 @@
     except IOError, e:
         stderr.write("Unable to open input file for reading: %s\n" % (e))
         sys.exit(1)
+
+
     def makeService(store):
         return ValidService(store, options, output, input, reactor, config)
-    utilityMain(options['config'], makeService, reactor)
 
-if __name__ == '__main__':
+    utilityMain(options["config"], makeService, reactor)
+
+
+
+if __name__ == "__main__":
     main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20120927/6b19a5e9/attachment.html>


More information about the calendarserver-changes mailing list