<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[14140] CalendarServer/trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/14140">14140</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2014-11-06 17:50:02 -0800 (Thu, 06 Nov 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>First pass at import tool, only non-scheduled events supported so far</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunksetuppy">CalendarServer/trunk/setup.py</a></li>
<li><a href="#CalendarServertrunksupportApplemake">CalendarServer/trunk/support/Apple.make</a></li>
<li><a href="#CalendarServertrunktwistedcaldavicalpy">CalendarServer/trunk/twistedcaldav/ical.py</a></li>
<li><a href="#CalendarServertrunktwistedcaldavstorebridgepy">CalendarServer/trunk/twistedcaldav/storebridge.py</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcalendarservertoolsimporterpy">CalendarServer/trunk/calendarserver/tools/importer.py</a></li>
<li><a href="#CalendarServertrunkcalendarservertoolstesttest_importerpy">CalendarServer/trunk/calendarserver/tools/test/test_importer.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunkcalendarservertoolsimporterpy"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/calendarserver/tools/importer.py (0 => 14140)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/importer.py                                (rev 0)
+++ CalendarServer/trunk/calendarserver/tools/importer.py        2014-11-07 01:50:02 UTC (rev 14140)
</span><span class="lines">@@ -0,0 +1,319 @@
</span><ins>+#!/usr/bin/env python
+# -*- test-case-name: calendarserver.tools.test.test_importer -*-
+##
+# Copyright (c) 2014 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
+# 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 &quot;AS IS&quot; 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.
+##
+
+&quot;&quot;&quot;
+This tool imports calendar data
+
+This tool requires access to the calendar server's configuration and data
+storage; it does not operate by talking to the server via the network.  It
+therefore does not apply any of the access restrictions that the server would.
+&quot;&quot;&quot;
+
+from __future__ import print_function
+
+import os
+import sys
+import uuid
+
+from calendarserver.tools.cmdline import utilityMain, WorkerService
+from twext.python.log import Logger
+from twisted.internet.defer import inlineCallbacks
+from twisted.python.text import wordWrap
+from twisted.python.usage import Options, UsageError
+from twistedcaldav import customxml
+from twistedcaldav.ical import Component
+from twistedcaldav.stdconfig import DEFAULT_CONFIG_FILE
+from txdav.base.propertystore.base import PropertyName
+from txdav.common.icommondatastore import UIDExistsError
+from txdav.xml import element as davxml
+
+
+log = Logger()
+
+
+
+def usage(e=None):
+    if e:
+        print(e)
+        print(&quot;&quot;)
+    try:
+        ImportOptions().opt_help()
+    except SystemExit:
+        pass
+    if e:
+        sys.exit(64)
+    else:
+        sys.exit(0)
+
+
+description = '\n'.join(
+    wordWrap(
+        &quot;&quot;&quot;
+        Usage: calendarserver_import [options] [input specifiers]\n
+        &quot;&quot;&quot; + __doc__,
+        int(os.environ.get('COLUMNS', '80'))
+    )
+)
+
+
+
+class ImportException(Exception):
+    &quot;&quot;&quot;
+    An error occurred during import
+    &quot;&quot;&quot;
+
+
+class ImportOptions(Options):
+    &quot;&quot;&quot;
+    Command-line options for 'calendarserver_import'
+    &quot;&quot;&quot;
+
+    synopsis = description
+
+    optFlags = [
+        ['debug', 'D', &quot;Debug logging.&quot;],
+    ]
+
+    optParameters = [
+        ['config', 'f', DEFAULT_CONFIG_FILE, &quot;Specify caldavd.plist configuration path.&quot;],
+    ]
+
+    def __init__(self):
+        super(ImportOptions, self).__init__()
+        self.inputName = '-'
+
+
+    def opt_input(self, filename):
+        &quot;&quot;&quot;
+        Specify input file path (default: '-', meaning stdin).
+        &quot;&quot;&quot;
+        self.inputName = filename
+
+    opt_i = opt_input
+
+
+    def openInput(self):
+        &quot;&quot;&quot;
+        Open the appropriate input file based on the '--input' option.
+        &quot;&quot;&quot;
+        if self.inputName == '-':
+            return sys.stdin
+        else:
+            return open(self.inputName, 'r')
+
+
+# These could probably live on the collection class:
+
+def setCollectionPropertyValue(collection, element, value):
+    collectionProperties = collection.properties()
+    collectionProperties[PropertyName.fromElement(element)] = (
+        element.fromString(value)
+    )
+
+
+def getCollectionPropertyValue(collection, element):
+    collectionProperties = collection.properties()
+    name = PropertyName.fromElement(element)
+    if name in collectionProperties:
+        return str(collectionProperties[name])
+    else:
+        return None
+
+#
+
+
+@inlineCallbacks
+def importCollectionComponent(store, component):
+    &quot;&quot;&quot;
+    Import a component representing a collection (e.g. VCALENDAR) into the
+    store.
+
+    The homeUID and collection resource name the component will be imported
+    into is derived from the SOURCE property on the VCALENDAR (which must
+    be present).  The code assumes it will be a URI with slash-separated parts
+    with the penultimate part specifying the homeUID and the last part
+    specifying the calendar resource name.  The NAME property will be used
+    to set the DAV:display-name, while the COLOR property will be used to set
+    calendar-color.
+
+    Subcomponents (e.g. VEVENTs) are grouped into resources by UID.  Objects
+    which have a UID already in use within the home will be skipped.
+
+    @param store: The db store to add the component to
+    @type store: L{IDataStore}
+    @param component: The component to store
+    @type component: L{twistedcaldav.ical.Component}
+    &quot;&quot;&quot;
+
+    sourceURI = component.propertyValue(&quot;SOURCE&quot;)
+    if not sourceURI:
+        raise ImportException(&quot;Calendar is missing SOURCE property&quot;)
+
+    ownerUID, collectionResourceName = sourceURI.strip(&quot;/&quot;).split(&quot;/&quot;)[-2:]
+
+    dir = store.directoryService()
+    record = yield dir.recordWithUID(ownerUID)
+    if not record:
+        raise ImportException(&quot;{} is not in the directory&quot;.format(ownerUID))
+
+    # Set properties on the collection
+    txn = store.newTransaction()
+    home = yield txn.calendarHomeWithUID(ownerUID, create=True)
+    collection = yield home.childWithName(collectionResourceName)
+    if not collection:
+        collection = yield home.createChildWithName(collectionResourceName)
+    for propertyName, element in (
+        (&quot;NAME&quot;, davxml.DisplayName),
+        (&quot;COLOR&quot;, customxml.CalendarColor),
+    ):
+        value = component.propertyValue(propertyName)
+        if value is not None:
+            setCollectionPropertyValue(collection, element, value)
+    yield txn.commit()
+
+    # Populate the collection; NB we use a txn for each object, and we might
+    # want to batch them?
+    groupedComponents = Component.componentsFromComponent(component)
+    for groupedComponent in groupedComponents:
+        resourceName = &quot;{}.ics&quot;.format(str(uuid.uuid4()))
+        try:
+            yield storeComponentInHomeAndCalendar(
+                store, groupedComponent, ownerUID, collectionResourceName, resourceName
+            )
+        except UIDExistsError:
+            # That event is already in the home
+            print(
+                &quot;Skipping since UID already exists: {uid}&quot;.format(
+                    uid=groupedComponent.propertyValue(&quot;UID&quot;)
+                )
+            )
+        except Exception, e:
+            print(
+                &quot;Failed to import due to: {error}\n{comp}&quot;.format(
+                    error=e,
+                    comp=groupedComponent
+                )
+            )
+
+
+@inlineCallbacks
+def storeComponentInHomeAndCalendar(
+    store, component, homeUID, collectionResourceName, objectResourceName
+):
+    &quot;&quot;&quot;
+    Add a component to the store as an objectResource
+
+    If the calendar home does not yet exist for homeUID it will be created.
+    If the collection by the name collectionResourceName does not yet exist
+    it will be created.
+
+    @param store: The db store to add the component to
+    @type store: L{IDataStore}
+    @param component: The component to store
+    @type component: L{twistedcaldav.ical.Component}
+    @param homeUID: uid of the home collection
+    @type collectionResourceName: C{str}
+    @param collectionResourceName: name of the collection resource
+    @type collectionResourceName: C{str}
+    @param objectResourceName: name of the objectresource
+    @type objectResourceName: C{str}
+
+    &quot;&quot;&quot;
+    txn = store.newTransaction()
+    home = yield txn.calendarHomeWithUID(homeUID, create=True)
+    collection = yield home.childWithName(collectionResourceName)
+    if not collection:
+        collection = yield home.createChildWithName(collectionResourceName)
+
+    yield collection.createObjectResourceWithName(objectResourceName, component)
+    yield txn.commit()
+
+
+
+class ImporterService(WorkerService, object):
+    &quot;&quot;&quot;
+    Service which runs, imports the data, then stops the reactor.
+    &quot;&quot;&quot;
+
+    def __init__(self, store, options, input, reactor, config):
+        super(ImporterService, self).__init__(store)
+        self.options = options
+        self.input = input
+        self.reactor = reactor
+        self.config = config
+        self._directory = self.store.directoryService()
+
+
+    @inlineCallbacks
+    def doWork(self):
+        &quot;&quot;&quot;
+        Do the export, stopping the reactor when done.
+        &quot;&quot;&quot;
+        try:
+            component = Component.allFromStream(self.input)
+            self.input.close()
+            yield importCollectionComponent(self.store, component)
+        except:
+            log.failure(&quot;doWork()&quot;)
+
+
+    def directoryService(self):
+        &quot;&quot;&quot;
+        Get an appropriate directory service.
+        &quot;&quot;&quot;
+        return self._directory
+
+
+    def stopService(self):
+        &quot;&quot;&quot;
+        Stop the service.  Nothing to do; everything should be finished by this
+        time.
+        &quot;&quot;&quot;
+        # TODO: stopping this service mid-import should really stop the import
+        # loop, but this is not implemented because nothing will actually do it
+        # except hitting ^C (which also calls reactor.stop(), so that will exit
+        # anyway).
+
+
+
+def main(argv=sys.argv, stderr=sys.stderr, reactor=None):
+    &quot;&quot;&quot;
+    Do the import.
+    &quot;&quot;&quot;
+    if reactor is None:
+        from twisted.internet import reactor
+
+    options = ImportOptions()
+    try:
+        options.parseOptions(argv[1:])
+    except UsageError, e:
+        usage(e)
+
+    try:
+        input = options.openInput()
+    except IOError, e:
+        stderr.write(&quot;Unable to open input file for reading: %s\n&quot; %
+                     (e))
+        sys.exit(1)
+
+
+    def makeService(store):
+        from twistedcaldav.config import config
+        return ImporterService(store, options, input, reactor, config)
+
+    utilityMain(options[&quot;config&quot;], makeService, reactor, verbose=options[&quot;debug&quot;])
</ins><span class="cx">Property changes on: CalendarServer/trunk/calendarserver/tools/importer.py
</span><span class="cx">___________________________________________________________________
</span></span></pre></div>
<a id="svnexecutable"></a>
<div class="addfile"><h4>Added: svn:executable</h4></div>
<a id="CalendarServertrunkcalendarservertoolstesttest_importerpy"></a>
<div class="addfile"><h4>Added: CalendarServer/trunk/calendarserver/tools/test/test_importer.py (0 => 14140)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/test/test_importer.py                                (rev 0)
+++ CalendarServer/trunk/calendarserver/tools/test/test_importer.py        2014-11-07 01:50:02 UTC (rev 14140)
</span><span class="lines">@@ -0,0 +1,189 @@
</span><ins>+##
+# Copyright (c) 2014 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
+# 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 &quot;AS IS&quot; 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.
+##
+
+&quot;&quot;&quot;
+Unit tests for L{calendarsever.tools.importer}.
+&quot;&quot;&quot;
+
+from calendarserver.tools.importer import importCollectionComponent, ImportException
+from twisted.internet.defer import inlineCallbacks
+from twistedcaldav import customxml
+from twistedcaldav.ical import Component
+from twistedcaldav.test.util import StoreTestCase
+from txdav.xml import element as davxml
+from txdav.base.propertystore.base import PropertyName
+
+
+DATA_MISSING_SOURCE = &quot;&quot;&quot;BEGIN:VCALENDAR
+CALSCALE:GREGORIAN
+PRODID:-//Apple Computer\, Inc//iCal 2.0//EN
+VERSION:2.0
+END:VCALENDAR
+&quot;&quot;&quot;
+
+DATA_NO_SCHEDULING = &quot;&quot;&quot;BEGIN:VCALENDAR
+VERSION:2.0
+NAME:Sample Import Calendar
+COLOR:#0E61B9FF
+SOURCE;VALUE=URI:http://example.com/calendars/__uids__/user01/calendar/
+PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN
+BEGIN:VEVENT
+UID:5CE3B280-DBC9-4E8E-B0B2-996754020E5F
+DTSTART;TZID=America/Los_Angeles:20141108T093000
+DTEND;TZID=America/Los_Angeles:20141108T103000
+CREATED:20141106T192546Z
+DTSTAMP:20141106T192546Z
+RRULE:FREQ=DAILY
+SEQUENCE:0
+SUMMARY:repeating event
+TRANSP:OPAQUE
+END:VEVENT
+BEGIN:VEVENT
+UID:5CE3B280-DBC9-4E8E-B0B2-996754020E5F
+RECURRENCE-ID;TZID=America/Los_Angeles:20141111T093000
+DTSTART;TZID=America/Los_Angeles:20141111T110000
+DTEND;TZID=America/Los_Angeles:20141111T120000
+CREATED:20141106T192546Z
+DTSTAMP:20141106T192546Z
+SEQUENCE:0
+SUMMARY:repeating event
+TRANSP:OPAQUE
+END:VEVENT
+BEGIN:VEVENT
+UID:F6342D53-7D5E-4B5E-9E0A-F0A08977AFE5
+DTSTART;TZID=America/Los_Angeles:20141108T080000
+DTEND;TZID=America/Los_Angeles:20141108T091500
+CREATED:20141104T205338Z
+DTSTAMP:20141104T205338Z
+SEQUENCE:0
+SUMMARY:simple event
+TRANSP:OPAQUE
+END:VEVENT
+END:VCALENDAR
+&quot;&quot;&quot;
+
+DATA_NO_SCHEDULING_REIMPORT = &quot;&quot;&quot;BEGIN:VCALENDAR
+VERSION:2.0
+NAME:Sample Import Calendar Reimported
+COLOR:#FFFFFFFF
+SOURCE;VALUE=URI:http://example.com/calendars/__uids__/user01/calendar/
+PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN
+BEGIN:VEVENT
+UID:5CE3B280-DBC9-4E8E-B0B2-996754020E5F
+DTSTART;TZID=America/Los_Angeles:20141108T093000
+DTEND;TZID=America/Los_Angeles:20141108T103000
+CREATED:20141106T192546Z
+DTSTAMP:20141106T192546Z
+RRULE:FREQ=DAILY
+SEQUENCE:0
+SUMMARY:repeating event
+TRANSP:OPAQUE
+END:VEVENT
+BEGIN:VEVENT
+UID:5CE3B280-DBC9-4E8E-B0B2-996754020E5F
+RECURRENCE-ID;TZID=America/Los_Angeles:20141111T093000
+DTSTART;TZID=America/Los_Angeles:20141111T110000
+DTEND;TZID=America/Los_Angeles:20141111T120000
+CREATED:20141106T192546Z
+DTSTAMP:20141106T192546Z
+SEQUENCE:0
+SUMMARY:repeating event
+TRANSP:OPAQUE
+END:VEVENT
+BEGIN:VEVENT
+UID:CB194340-9B3C-40B1-B4E2-062FDB7C8829
+DTSTART;TZID=America/Los_Angeles:20141108T080000
+DTEND;TZID=America/Los_Angeles:20141108T091500
+CREATED:20141104T205338Z
+DTSTAMP:20141104T205338Z
+SEQUENCE:0
+SUMMARY:new event
+TRANSP:OPAQUE
+END:VEVENT
+END:VCALENDAR
+&quot;&quot;&quot;
+
+
+class ImportTests(StoreTestCase):
+    &quot;&quot;&quot;
+    Tests for importing data to a live store.
+    &quot;&quot;&quot;
+
+    @inlineCallbacks
+    def test_ImportComponentMissingSource(self):
+
+        component = Component.allFromString(DATA_MISSING_SOURCE)
+        try:
+            yield importCollectionComponent(self.store, component)
+        except ImportException:
+            pass
+        else:
+            self.fail(&quot;Did not raise ImportException&quot;)
+
+
+    @inlineCallbacks
+    def test_ImportComponentNoScheduling(self):
+
+        component = Component.allFromString(DATA_NO_SCHEDULING)
+        yield importCollectionComponent(self.store, component)
+
+        txn = self.store.newTransaction()
+        home = yield txn.calendarHomeWithUID(u&quot;user01&quot;)
+        collection = yield home.childWithName(u&quot;calendar&quot;)
+
+        # Verify properties have been set
+        collectionProperties = collection.properties()
+        for element, value in (
+            (davxml.DisplayName, &quot;Sample Import Calendar&quot;),
+            (customxml.CalendarColor, &quot;#0E61B9FF&quot;),
+        ):
+            self.assertEquals(
+                value,
+                collectionProperties[PropertyName.fromElement(element)]
+            )
+
+        # Verify child objects
+        objects = yield collection.listObjectResources()
+        self.assertEquals(len(objects), 2)
+
+        yield txn.commit()
+
+        # Reimport different component into same collection
+
+        component = Component.allFromString(DATA_NO_SCHEDULING_REIMPORT)
+
+        yield importCollectionComponent(self.store, component)
+
+        txn = self.store.newTransaction()
+        home = yield txn.calendarHomeWithUID(u&quot;user01&quot;)
+        collection = yield home.childWithName(u&quot;calendar&quot;)
+
+        # Verify properties have been changed
+        collectionProperties = collection.properties()
+        for element, value in (
+            (davxml.DisplayName, &quot;Sample Import Calendar Reimported&quot;),
+            (customxml.CalendarColor, &quot;#FFFFFFFF&quot;),
+        ):
+            self.assertEquals(
+                value,
+                collectionProperties[PropertyName.fromElement(element)]
+            )
+
+        # Verify child objects (should be 3 now)
+        objects = yield collection.listObjectResources()
+        self.assertEquals(len(objects), 3)
+
+        yield txn.commit()
</ins></span></pre></div>
<a id="CalendarServertrunksetuppy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/setup.py (14139 => 14140)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/setup.py        2014-11-06 21:51:03 UTC (rev 14139)
+++ CalendarServer/trunk/setup.py        2014-11-07 01:50:02 UTC (rev 14140)
</span><span class="lines">@@ -175,6 +175,9 @@
</span><span class="cx">     &quot;icalendar_validate&quot;:
</span><span class="cx">     (&quot;calendarserver.tools.validcalendardata&quot;, &quot;main&quot;),
</span><span class="cx"> 
</span><ins>+    &quot;import&quot;:
+    (&quot;calendarserver.tools.importer&quot;, &quot;main&quot;),
+
</ins><span class="cx">     &quot;manage_principals&quot;:
</span><span class="cx">     (&quot;calendarserver.tools.principals&quot;, &quot;main&quot;),
</span><span class="cx"> 
</span></span></pre></div>
<a id="CalendarServertrunksupportApplemake"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/support/Apple.make (14139 => 14140)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/support/Apple.make        2014-11-06 21:51:03 UTC (rev 14139)
+++ CalendarServer/trunk/support/Apple.make        2014-11-07 01:50:02 UTC (rev 14140)
</span><span class="lines">@@ -151,6 +151,7 @@
</span><span class="cx">                   calendarserver_command_gateway                                                                                 \
</span><span class="cx">                   calendarserver_diagnose                                                                                        \
</span><span class="cx">                   calendarserver_export                                                                                          \
</span><ins>+                  calendarserver_import                                                                                          \
</ins><span class="cx">                   calendarserver_manage_principals                                                                               \
</span><span class="cx">                   calendarserver_purge_attachments                                                                               \
</span><span class="cx">                   calendarserver_purge_events                                                                                    \
</span></span></pre></div>
<a id="CalendarServertrunktwistedcaldavicalpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/ical.py (14139 => 14140)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/ical.py        2014-11-06 21:51:03 UTC (rev 14139)
+++ CalendarServer/trunk/twistedcaldav/ical.py        2014-11-07 01:50:02 UTC (rev 14140)
</span><span class="lines">@@ -29,6 +29,7 @@
</span><span class="cx"> ]
</span><span class="cx"> 
</span><span class="cx"> import codecs
</span><ins>+import collections
</ins><span class="cx"> from difflib import unified_diff
</span><span class="cx"> import heapq
</span><span class="cx"> import itertools
</span><span class="lines">@@ -70,6 +71,7 @@
</span><span class="cx"> allowedSchedulingComponents = allowedStoreComponents + (&quot;VFREEBUSY&quot;,)
</span><span class="cx"> allowedComponents = allowedSchedulingComponents + (&quot;VTIMEZONE&quot;,)
</span><span class="cx"> 
</span><ins>+
</ins><span class="cx"> def _updateAllowedComponents(allowed):
</span><span class="cx">     global allowedStoreComponents, allowedSchedulingComponents, allowedComponents
</span><span class="cx">     allowedStoreComponents = allowed
</span><span class="lines">@@ -163,6 +165,7 @@
</span><span class="cx"> minDateTime = DateTime(1900, 1, 1, 0, 0, 0, tzid=Timezone(utc=True))
</span><span class="cx"> maxDateTime = DateTime(2100, 1, 1, 0, 0, 0, tzid=Timezone(utc=True))
</span><span class="cx"> 
</span><ins>+
</ins><span class="cx"> class InvalidICalendarDataError(ValueError):
</span><span class="cx">     pass
</span><span class="cx"> 
</span><span class="lines">@@ -509,6 +512,68 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     @classmethod
</span><ins>+    def componentsFromData(cls, data, format):
+        &quot;&quot;&quot;
+        Need to split a single VCALENDAR in text form into separate ones based
+        on UID with the appropriate VTIEMZONES included.
+        &quot;&quot;&quot;
+
+        # Split into components by UID and TZID
+        try:
+            vcal = cls.fromString(data, format)
+        except InvalidICalendarDataError:
+            return None
+
+        return cls.componentsFromComponent(vcal)
+
+
+    @classmethod
+    def componentsFromComponent(cls, sourceComponent):
+        &quot;&quot;&quot;
+        Need to split a single VCALENDAR in Component form into separate ones
+        based on UID with the appropriate VTIEMZONES included.
+        &quot;&quot;&quot;
+
+        results = []
+
+        by_uid = collections.OrderedDict()
+        by_tzid = {}
+        for subcomponent in sourceComponent.subcomponents():
+            if subcomponent.name() == &quot;VTIMEZONE&quot;:
+                by_tzid[subcomponent.propertyValue(&quot;TZID&quot;)] = subcomponent
+            else:
+                by_uid.setdefault(subcomponent.propertyValue(&quot;UID&quot;), []).append(subcomponent)
+
+        # Re-constitute as separate VCALENDAR objects
+        for components in by_uid.values():
+
+            newvcal = cls(&quot;VCALENDAR&quot;)
+            newvcal.addProperty(Property(&quot;VERSION&quot;, &quot;2.0&quot;))
+            newvcal.addProperty(Property(&quot;PRODID&quot;, sourceComponent.propertyValue(&quot;PRODID&quot;)))
+
+            # Get the set of TZIDs and include them
+            tzids = set()
+            for component in components:
+                tzids.update(component.timezoneIDs())
+            for tzid in tzids:
+                try:
+                    tz = by_tzid[tzid]
+                    newvcal.addComponent(tz.duplicate())
+                except KeyError:
+                    # We ignore the error and generate invalid ics which someone will
+                    # complain about at some point
+                    pass
+
+            # Now add each component
+            for component in components:
+                newvcal.addComponent(component.duplicate())
+
+            results.append(newvcal)
+
+        return results
+
+
+    @classmethod
</ins><span class="cx">     def newCalendar(cls):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Create and return an empty C{VCALENDAR} component.
</span></span></pre></div>
<a id="CalendarServertrunktwistedcaldavstorebridgepy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twistedcaldav/storebridge.py (14139 => 14140)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twistedcaldav/storebridge.py        2014-11-06 21:51:03 UTC (rev 14139)
+++ CalendarServer/trunk/twistedcaldav/storebridge.py        2014-11-07 01:50:02 UTC (rev 14140)
</span><span class="lines">@@ -15,7 +15,6 @@
</span><span class="cx"> # limitations under the License.
</span><span class="cx"> ##
</span><span class="cx"> 
</span><del>-import collections
</del><span class="cx"> import hashlib
</span><span class="cx"> import time
</span><span class="cx"> from urlparse import urlsplit, urljoin
</span><span class="lines">@@ -36,7 +35,7 @@
</span><span class="cx"> from twistedcaldav.config import config
</span><span class="cx"> from twistedcaldav.customxml import calendarserver_namespace
</span><span class="cx"> from twistedcaldav.ical import (
</span><del>-    Component as VCalendar, Property as VProperty, InvalidICalendarDataError,
</del><ins>+    Component as VCalendar, Property as VProperty,
</ins><span class="cx">     iCalendarProductID, Component
</span><span class="cx"> )
</span><span class="cx"> from twistedcaldav.instance import (
</span><span class="lines">@@ -1174,52 +1173,9 @@
</span><span class="cx">         Need to split a single VCALENDAR into separate ones based on UID with the
</span><span class="cx">         appropriate VTIEMZONES included.
</span><span class="cx">         &quot;&quot;&quot;
</span><ins>+        return Component.componentsFromData(data, format)
</ins><span class="cx"> 
</span><del>-        results = []
</del><span class="cx"> 
</span><del>-        # Split into components by UID and TZID
-        try:
-            vcal = VCalendar.fromString(data, format)
-        except InvalidICalendarDataError:
-            return None
-
-        by_uid = collections.OrderedDict()
-        by_tzid = {}
-        for subcomponent in vcal.subcomponents():
-            if subcomponent.name() == &quot;VTIMEZONE&quot;:
-                by_tzid[subcomponent.propertyValue(&quot;TZID&quot;)] = subcomponent
-            else:
-                by_uid.setdefault(subcomponent.propertyValue(&quot;UID&quot;), []).append(subcomponent)
-
-        # Re-constitute as separate VCALENDAR objects
-        for components in by_uid.values():
-
-            newvcal = VCalendar(&quot;VCALENDAR&quot;)
-            newvcal.addProperty(VProperty(&quot;VERSION&quot;, &quot;2.0&quot;))
-            newvcal.addProperty(VProperty(&quot;PRODID&quot;, vcal.propertyValue(&quot;PRODID&quot;)))
-
-            # Get the set of TZIDs and include them
-            tzids = set()
-            for component in components:
-                tzids.update(component.timezoneIDs())
-            for tzid in tzids:
-                try:
-                    tz = by_tzid[tzid]
-                    newvcal.addComponent(tz.duplicate())
-                except KeyError:
-                    # We ignore the error and generate invalid ics which someone will
-                    # complain about at some point
-                    pass
-
-            # Now add each component
-            for component in components:
-                newvcal.addComponent(component.duplicate())
-
-            results.append(newvcal)
-
-        return results
-
-
</del><span class="cx">     @classmethod
</span><span class="cx">     def resourceSuffix(cls):
</span><span class="cx">         return &quot;.ics&quot;
</span></span></pre>
</div>
</div>

</body>
</html>