[CalendarServer-changes] [7468] CalendarServer/branches/users/glyph/new-export/calendarserver/tools

source_changes at macosforge.org source_changes at macosforge.org
Mon May 16 07:44:48 PDT 2011


Revision: 7468
          http://trac.macosforge.org/projects/calendarserver/changeset/7468
Author:   glyph at apple.com
Date:     2011-05-16 07:44:48 -0700 (Mon, 16 May 2011)
Log Message:
-----------
per-user data filtering and output file selection

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/new-export/calendarserver/tools/export.py
    CalendarServer/branches/users/glyph/new-export/calendarserver/tools/test/test_export.py

Modified: CalendarServer/branches/users/glyph/new-export/calendarserver/tools/export.py
===================================================================
--- CalendarServer/branches/users/glyph/new-export/calendarserver/tools/export.py	2011-05-16 14:44:35 UTC (rev 7467)
+++ CalendarServer/branches/users/glyph/new-export/calendarserver/tools/export.py	2011-05-16 14:44:48 UTC (rev 7468)
@@ -97,6 +97,7 @@
     def __init__(self):
         super(ExportOptions, self).__init__()
         self.exporters = []
+        self.outputName = '-'
 
 
     def opt_record(self, recordName):
@@ -106,18 +107,41 @@
         recordType, shortName = recordName.split(":", 1)
         self.exporters.append(HomeExporter(recordType, shortName))
 
+    opt_r = opt_record
 
+
     def opt_collection(self, collectionName):
         """
-        add a calendar collection.  must be passed after --record (or a synonym,
-        like --user).  for example, to export user1's calendars called
-        'meetings' and 'team', invoke 'calendarserver_export --user=user1
+        Add a calendar collection.  This option must be passed after --record
+        (or a synonym, like --user).  for example, to export user1's calendars
+        called 'meetings' and 'team', invoke 'calendarserver_export --user=user1
         --collection=meetings --collection=team'.
         """
         self.exporters[-1].collections.append(collectionName)
 
+    opt_c = opt_collection
 
 
+    def opt_output(self, filename):
+        """
+        Specify output file path (default: '-', meaning stdout).
+        """
+        self.outputName = filename
+
+    opt_o = opt_output
+
+
+    def openOutput(self):
+        """
+        Open the appropriate output file based on the '--output' option.
+        """
+        if self.outputName == '-':
+            return sys.stdout
+        else:
+            return open(self.outputName, 'wb')
+
+
+
 class HomeExporter(object):
     """
     An exporter that constructs a list of calendars based on the UID or
@@ -155,7 +179,7 @@
     for calendar in calendars:
         calendar = yield calendar
         for obj in (yield calendar.calendarObjects()):
-            evt = yield obj.component()
+            evt = yield obj.filteredComponent(exporterUID)
             for sub in evt.subcomponents():
                 if sub.name() != 'VTIMEZONE':
                     # Omit all VTIMEZONE components, since PyCalendar will

Modified: CalendarServer/branches/users/glyph/new-export/calendarserver/tools/test/test_export.py
===================================================================
--- CalendarServer/branches/users/glyph/new-export/calendarserver/tools/test/test_export.py	2011-05-16 14:44:35 UTC (rev 7467)
+++ CalendarServer/branches/users/glyph/new-export/calendarserver/tools/test/test_export.py	2011-05-16 14:44:48 UTC (rev 7468)
@@ -33,6 +33,8 @@
 from twistedcaldav.ical import Component
 from calendarserver.tools.export import ExportOptions
 from calendarserver.tools.export import HomeExporter
+from twistedcaldav.datafilters.test.test_peruserdata import dataForTwoUsers
+from twistedcaldav.datafilters.test.test_peruserdata import resultForUser2
 from txdav.common.datastore.test.util import buildStore
 from txdav.common.datastore.test.util import populateCalendarsFrom
 
@@ -119,7 +121,25 @@
         self.assertEquals(exp.collections, ["fun stuff"])
 
 
+    def test_outputFileSelection(self):
+        """
+        The --output option selects the file to write to, '-' or no parameter
+        meaning stdout; the L{ExportOptions.openOutput} method returns that
+        file.
+        """
+        eo = ExportOptions()
+        eo.parseOptions([])
+        self.assertIdentical(eo.openOutput(), sys.stdout)
+        eo = ExportOptions()
+        eo.parseOptions(["--output", "-"])
+        self.assertIdentical(eo.openOutput(), sys.stdout)
+        eo = ExportOptions()
+        tmpnam = self.mktemp()
+        eo.parseOptions(["--output", tmpnam])
+        self.assertEquals(eo.openOutput().name, tmpnam)
 
+
+
 class IntegrationTests(TestCase):
     """
     Tests for exporting data from a live store.
@@ -291,3 +311,30 @@
                           set(["America/New_Yrok", "US/Pacific"]))
 
 
+    @inlineCallbacks
+    def test_perUserFiltering(self):
+        """
+        L{exportToFile} performs per-user component filtering based on its
+        C{exporterUID} parameter.
+        """
+        yield populateCalendarsFrom(
+            {
+                "home1": {
+                    "calendar1": {
+                        "peruser.ics": (dataForTwoUsers, {}), # EST
+                    }
+                }
+            }, self.store
+        )
+        io = StringIO()
+        yield exportToFile(
+            [(yield self.txn().calendarHomeWithUID("home1"))
+              .calendarWithName("calendar1")], "user02", io
+        )
+        self.assertEquals(
+            Component.fromString(resultForUser2),
+            Component.fromString(io.getvalue())
+        )
+
+
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110516/1008541c/attachment-0001.html>


More information about the calendarserver-changes mailing list