[CalendarServer-changes] [4725] CalendarServer/trunk

source_changes at macosforge.org source_changes at macosforge.org
Mon Nov 9 15:14:19 PST 2009


Revision: 4725
          http://trac.macosforge.org/projects/calendarserver/changeset/4725
Author:   wsanchez at apple.com
Date:     2009-11-09 15:14:19 -0800 (Mon, 09 Nov 2009)
Log Message:
-----------
Add tool to split iCalendar data

Added Paths:
-----------
    CalendarServer/trunk/bin/icalendar_split
    CalendarServer/trunk/calendarserver/tools/icalsplit.py

Added: CalendarServer/trunk/bin/icalendar_split
===================================================================
--- CalendarServer/trunk/bin/icalendar_split	                        (rev 0)
+++ CalendarServer/trunk/bin/icalendar_split	2009-11-09 23:14:19 UTC (rev 4725)
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+##
+# Copyright (c) 2006-2007 Apple Inc. 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.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+import sys
+
+#PYTHONPATH
+
+if __name__ == "__main__":
+    if "PYTHONPATH" in globals():
+        sys.path.insert(0, PYTHONPATH)
+    else:
+        from os.path import dirname, abspath, join
+        from subprocess import Popen, PIPE
+
+        home = dirname(dirname(abspath(__file__)))
+        run = join(home, "run")
+
+        child = Popen((run, "-p"), stdout=PIPE)
+        path, stderr = child.communicate()
+
+        if child.wait() == 0:
+            sys.path[0:0] = path.split(":")
+
+    from calendarserver.tools.icalsplit import main
+    main()


Property changes on: CalendarServer/trunk/bin/icalendar_split
___________________________________________________________________
Added: svn:executable
   + *

Added: CalendarServer/trunk/calendarserver/tools/icalsplit.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/icalsplit.py	                        (rev 0)
+++ CalendarServer/trunk/calendarserver/tools/icalsplit.py	2009-11-09 23:14:19 UTC (rev 4725)
@@ -0,0 +1,114 @@
+##
+# Copyright (c) 2005-2009 Apple Inc. 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.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+import os
+import sys
+from getopt import getopt, GetoptError
+
+from twistedcaldav.ical import Component as iComponent
+
+
+def splitICalendarFile(inputFileName, outputDirectory):
+    """
+    Reads iCalendar data from a file and outputs a separate file for
+    each iCalendar component into a directory.  This is useful for
+    converting a monolithic iCalendar object into a set of objects
+    that comply with CalDAV's requirements on resources.
+    """
+    inputFile = open(inputFileName)
+    try:
+        calendar = iComponent.fromStream(inputFile)
+    finally:
+        inputFile.close()
+
+    assert calendar.name() == "VCALENDAR"
+
+    topLevelProperties = tuple(calendar.properties())
+
+    for subcomponent in calendar.subcomponents():
+        subcalendar = iComponent("VCALENDAR")
+
+        #
+        # Add top-level properties from monolithic calendar to
+        # top-level properties of subcalendar.
+        #
+        for property in topLevelProperties:
+            subcalendar.addProperty(property)
+
+        subcalendar.addComponent(subcomponent)
+
+        uid = subcalendar.resourceUID()
+        subFileName = os.path.join(outputDirectory, uid + ".ics")
+
+        print "Writing %s" % (subFileName,)
+
+        subcalendar_file = file(subFileName, "w")
+        try:
+            subcalendar_file.write(str(subcalendar))
+        finally:
+            subcalendar_file.close()
+
+
+def usage(e=None):
+    if e:
+        print e
+        print ""
+
+    name = os.path.basename(sys.argv[0])
+    print "usage: %s [options] input_file output_directory" % (name,)
+    print ""
+    print "  Splits up monolithic iCalendar data into separate files for each"
+    print "  subcomponent so as to comply with CalDAV requirements for"
+    print "  individual resources."
+    print ""
+    print "options:"
+    print "  -h --help: print this help and exit"
+    print ""
+
+    if e:
+        sys.exit(64)
+    else:
+        sys.exit(0)
+
+
+def main():
+    try:
+        (optargs, args) = getopt(
+            sys.argv[1:], "h", [
+                "help",
+            ],
+        )
+    except GetoptError, e:
+        usage(e)
+
+    for opt, arg in optargs:
+        if opt in ("-h", "--help"):
+            usage()
+
+    try:
+        inputFileName, outputDirectory = args
+    except ValueError:
+        if len(args) > 2:
+            many = "many"
+        else:
+            many = "few"
+        usage("Too %s arguments" % (many,))
+
+    splitICalendarFile(inputFileName, outputDirectory)
+
+
+if __name__ == "__main__":
+    main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20091109/67b784e4/attachment.html>


More information about the calendarserver-changes mailing list