[CalendarServer-changes] [4446] CalendarServer/branches/more-deferreds

source_changes at macosforge.org source_changes at macosforge.org
Thu Jul 9 16:56:11 PDT 2009


Revision: 4446
          http://trac.macosforge.org/projects/calendarserver/changeset/4446
Author:   william_short at apple.com
Date:     2009-07-09 16:56:11 -0700 (Thu, 09 Jul 2009)
Log Message:
-----------
change some APIs to return deferreds

Modified Paths:
--------------
    CalendarServer/branches/more-deferreds/calendarserver/tap/caldav.py
    CalendarServer/branches/more-deferreds/twistedcaldav/caldavxml.py
    CalendarServer/branches/more-deferreds/twistedcaldav/directory/calendar.py
    CalendarServer/branches/more-deferreds/twistedcaldav/directory/directory.py
    CalendarServer/branches/more-deferreds/twistedcaldav/directory/principal.py
    CalendarServer/branches/more-deferreds/twistedcaldav/directory/test/test_principal.py
    CalendarServer/branches/more-deferreds/twistedcaldav/freebusyurl.py
    CalendarServer/branches/more-deferreds/twistedcaldav/icaldav.py
    CalendarServer/branches/more-deferreds/twistedcaldav/index.py
    CalendarServer/branches/more-deferreds/twistedcaldav/memcacheprops.py
    CalendarServer/branches/more-deferreds/twistedcaldav/method/delete_common.py
    CalendarServer/branches/more-deferreds/twistedcaldav/method/get.py
    CalendarServer/branches/more-deferreds/twistedcaldav/method/propfind.py
    CalendarServer/branches/more-deferreds/twistedcaldav/method/put_common.py
    CalendarServer/branches/more-deferreds/twistedcaldav/method/report_calquery.py
    CalendarServer/branches/more-deferreds/twistedcaldav/method/report_common.py
    CalendarServer/branches/more-deferreds/twistedcaldav/resource.py
    CalendarServer/branches/more-deferreds/twistedcaldav/scheduling/implicit.py
    CalendarServer/branches/more-deferreds/twistedcaldav/scheduling/scheduler.py
    CalendarServer/branches/more-deferreds/twistedcaldav/scheduling/utils.py
    CalendarServer/branches/more-deferreds/twistedcaldav/static.py
    CalendarServer/branches/more-deferreds/twistedcaldav/test/test_memcacheprops.py
    CalendarServer/branches/more-deferreds/twistedcaldav/test/test_static.py

Modified: CalendarServer/branches/more-deferreds/calendarserver/tap/caldav.py
===================================================================
--- CalendarServer/branches/more-deferreds/calendarserver/tap/caldav.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/calendarserver/tap/caldav.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -597,14 +597,6 @@
             directory,
         )
 
-        self.log_info("Setting up calendar collection: %r"
-                      % (self.calendarResourceClass,))
-
-        calendarCollection = self.calendarResourceClass(
-            os.path.join(config.DocumentRoot, "calendars"),
-            directory, "/calendars/",
-        )
-
         self.log_info("Setting up root resource: %r"
                       % (self.rootResourceClass,))
 
@@ -612,10 +604,18 @@
             config.DocumentRoot,
             principalCollections=(principalCollection,),
         )
-
         root.putChild("principals", principalCollection)
-        root.putChild("calendars", calendarCollection)
 
+        d = self.calendarResourceClass.fetch(None,
+            os.path.join(config.DocumentRoot, "calendars"),
+            directory, "/calendars/")
+
+        def _installCalendars(calendarCollection):
+            self.log_info("Setting up calendar collection: %r"
+                          % (self.calendarResourceClass,))
+            root.putChild("calendars", calendarCollection)
+        d.addCallback(_installCalendars)
+
         for name, info in config.Aliases.iteritems():
             if os.path.sep in name or not info.get("path", None):
                 self.log_error("Invalid alias: %s" % (name,))

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/caldavxml.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/caldavxml.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/caldavxml.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -497,7 +497,7 @@
         @param timezone: the L{Component} the VTIMEZONE to use for floating/all-day.
         @return: an L{CalendarData} with the (filtered) calendar data.
         """
-        return self.elementFromCalendar(resource.iCalendarText(), timezone)
+        return resource.iCalendarText().addCallback(self.elementFromCalendar, timezone)
 
     def elementFromCalendar(self, calendar, timezone=None):
         """
@@ -526,7 +526,7 @@
         @param timezone: the L{Component} the VTIMEZONE to use for floating/all-day.
         @return: an L{CalendarData} with the (filtered) calendar data.
         """
-        return self.elementFromCalendarWithAccessRestrictions(resource.iCalendarText(), access, timezone)
+        return resource.iCalendarText().addCallback(self.elementFromCalendarWithAccessRestrictions, access, timezone)
 
     def elementFromCalendarWithAccessRestrictions(self, calendar, access, timezone=None):
         """

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/directory/calendar.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/directory/calendar.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/directory/calendar.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -27,7 +27,7 @@
     "DirectoryCalendarHomeResource",
 ]
 
-from twisted.internet.defer import succeed, inlineCallbacks, returnValue
+from twisted.internet.defer import succeed, inlineCallbacks, returnValue, gatherResults
 from twisted.web2 import responsecode
 from twisted.web2.dav import davxml
 from twisted.web2.http import HTTPError
@@ -117,7 +117,7 @@
     def homeForDirectoryRecord(self, record):
         uidResource = self.getChild(uidsResourceName)
         if uidResource is None:
-            return None
+            return succeed(None)
         else:
             return uidResource.getChild(record.uid)
 
@@ -251,6 +251,7 @@
     """
     Calendar home collection resource.
     """
+
     def __init__(self, parent, record):
         """
         @param path: the path to the file which will back the resource.
@@ -263,31 +264,13 @@
         self.record = record
         self.parent = parent
 
-        # Cache children which must be of a specific type
-        childlist = (
-            ("inbox" , ScheduleInboxResource ),
-            ("outbox", ScheduleOutboxResource),
-        )
-        if config.EnableDropBox:
-            childlist += (
-                ("dropbox", DropBoxHomeResource),
-            )
-        if config.FreeBusyURL.Enabled:
-            childlist += (
-                ("freebusy", FreeBusyURLResource),
-            )
-        for name, cls in childlist:
-            child = self.provisionChild(name)
-            assert isinstance(child, cls), "Child %r is not a %s: %r" % (name, cls.__name__, child)
-            self.putChild(name, child)
-
     def provisionDefaultCalendars(self):
 
         # Disable notifications during provisioning
         if hasattr(self, "clientNotifier"):
             self.clientNotifier.disableNotify()
 
-        def setupFreeBusy(_):
+        def setupFreeBusy(_, child):
             # Default calendar is initially opaque to freebusy
             child.writeDeadProperty(caldavxml.ScheduleCalendarTransp(caldavxml.Opaque()))
 
@@ -307,15 +290,17 @@
             return self
 
         try:
-            self.provision()
+            d = self.provision()
 
             childName = "calendar"
             childURL = joinURL(self.url(), childName)
-            child = self.provisionChild(childName)
-            assert isinstance(child, CalDAVResource), "Child %r is not a %s: %r" % (childName, CalDAVResource.__name__, child)
+            d.addCallback(lambda _: self.provisionChild(childName))
+            
+            def _makeChild(child):
+                assert isinstance(child, CalDAVResource), "Child %r is not a %s: %r" % (childName, CalDAVResource.__name__, child)
+                return child.createCalendarCollection().addCallback(setupFreeBusy, child)
 
-            d = child.createCalendarCollection()
-            d.addCallback(setupFreeBusy)
+            d.addCallback(_makeChild)
         except:
             # We want to make sure to re-enable notifications, so do so
             # if there is an immediate exception above, or via errback, below

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/directory/directory.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/directory/directory.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/directory/directory.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -107,13 +107,16 @@
                 credentials.authzPrincipal.principalURL(),
             )
         else:
-            if credentials.authnPrincipal.record.verifyCredentials(credentials.credentials):
-                return (
-                    credentials.authnPrincipal.principalURL(),
-                    credentials.authzPrincipal.principalURL(),
-                )
-            else:
-                raise UnauthorizedLogin("Incorrect credentials for %s" % (credentials.credentials.username,)) 
+            d = credentials.authnPrincipal.record.verifyCredentials(credentials.credentials)
+            def _verify(authed):
+                if authed:
+                    return (
+                        credentials.authnPrincipal.principalURL(),
+                        credentials.authzPrincipal.principalURL(),
+                        )
+                else:
+                    raise UnauthorizedLogin("Incorrect credentials for %s" % (credentials.credentials.username,)) 
+            return d.addCallback(_verify)
 
     def recordTypes(self):
         raise NotImplementedError("Subclass must implement recordTypes()")

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/directory/principal.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/directory/principal.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/directory/principal.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -838,10 +838,13 @@
         returnValue(result)
 
     def extraDirectoryBodyItems(self, request):
-        return "".join((
-            """\nCalendar homes:\n"""          , format_list(format_link(u) for u in self.calendarHomeURLs()),
-            """\nCalendar user addresses:\n""" , format_list(format_link(a) for a in self.calendarUserAddresses()),
-        ))
+        d = self.calendarHomeURLs()
+        def _gotURLs(homeURLs):
+            return "".join((
+                    """\nCalendar homes:\n"""          , format_list(format_link(u) for u in homeURLs),
+                    """\nCalendar user addresses:\n""" , format_list(format_link(a) for a in self.calendarUserAddresses()),
+                    ))
+        return d.addCallbacks(_gotURLs)
 
     ##
     # CalDAV
@@ -877,22 +880,22 @@
             return False
 
     def scheduleInbox(self, request):
-        home = self.calendarHome()
-        if home is None:
-            return succeed(None)
+        d = self.calendarHome()
+        def _gotHome(home):
+            if home is None:
+                return None
+            return home.getChild("inbox")
 
-        inbox = home.getChild("inbox")
-        if inbox is None:
-            return succeed(None)
+        return d.addCallback(_gotHome)
 
-        return succeed(inbox)
-
     def calendarHomeURLs(self):
-        home = self.calendarHome()
-        if home is None:
-            return ()
-        else:
-            return (home.url(),)
+        d = self.calendarHome()
+        def _gotHome(home):
+            if home is None:
+                return ()
+            else:
+                return (home.url(),)
+        return d.addCallback(_gotHome)
 
     def scheduleInboxURL(self):
         return self._homeChildURL("inbox/")
@@ -907,11 +910,13 @@
             return None
 
     def _homeChildURL(self, name):
-        home = self.calendarHome()
-        if home is None:
-            return None
-        else:
-            return joinURL(home.url(), name)
+        d = self.calendarHome()
+        def _gotHome(home):
+            if home is None:
+                return None
+            else:
+                return joinURL(home.url(), name)
+        return d.addCallback(_gotHome)
 
     def calendarHome(self):
         # FIXME: self.record.service.calendarHomesCollection smells like a hack
@@ -920,7 +925,7 @@
         if hasattr(service, "calendarHomesCollection"):
             return service.calendarHomesCollection.homeForDirectoryRecord(self.record)
         else:
-            return None
+            return succeed(None)
 
 
     ##

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/directory/test/test_principal.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/directory/test/test_principal.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/directory/test/test_principal.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -17,7 +17,7 @@
 import os
 
 from twisted.cred.credentials import UsernamePassword
-from twisted.internet.defer import inlineCallbacks
+from twisted.internet.defer import inlineCallbacks, gatherResults
 from twisted.web2.dav import davxml
 from twisted.web2.dav.fileop import rmdir
 from twisted.web2.dav.resource import AccessDeniedError
@@ -331,12 +331,15 @@
         DirectoryPrincipalResource.scheduleInboxURL(),
         DirectoryPrincipalResource.scheduleOutboxURL()
         """
+        ds = []
         # No calendar home provisioner should result in no calendar homes.
         for provisioningResource, recordType, recordResource, record in self._allRecords():
             if record.enabledForCalendaring:
-                self.failIf(tuple(recordResource.calendarHomeURLs()))
-                self.failIf(recordResource.scheduleInboxURL())
-                self.failIf(recordResource.scheduleOutboxURL())
+                d = recordResource.calendarHomeURLs()
+                d.addCallback(lambda u: self.assertEqual(len(u), 0))
+                ds.append(d)
+                ds.append(recordResource.scheduleInboxURL().addCallback(self.failIf))
+                ds.append(recordResource.scheduleOutboxURL().addCallback(self.failIf))
 
         # Need to create a calendar home provisioner for each service.
         calendarRootResources = {}
@@ -356,33 +359,32 @@
         # Calendar home provisioners should result in calendar homes.
         for provisioningResource, recordType, recordResource, record in self._allRecords():
             if record.enabledForCalendaring:
-                homeURLs = tuple(recordResource.calendarHomeURLs())
-                self.failUnless(homeURLs)
+                d = gatherResults([recordResource.calendarHomeURLs(),
+                                  recordResource.scheduleInboxURL(),
+                                  recordResource.scheduleOutboxURL()])
+                def _gotURLs(homeURLs, inboxURL, outboxURL):
+                    calendarRootURL = calendarRootResources[record.service.__class__.__name__].url()
 
-                calendarRootURL = calendarRootResources[record.service.__class__.__name__].url()
+                    self.failUnless(inboxURL)
+                    self.failUnless(outboxURL)
 
-                inboxURL = recordResource.scheduleInboxURL()
-                outboxURL = recordResource.scheduleOutboxURL()
+                    for homeURL in homeURLs:
+                        self.failUnless(homeURL.startswith(calendarRootURL))
 
-                self.failUnless(inboxURL)
-                self.failUnless(outboxURL)
+                        if inboxURL and inboxURL.startswith(homeURL):
+                            self.failUnless(len(inboxURL) > len(homeURL))
+                            self.failUnless(inboxURL.endswith("/"))
+                            inboxURL = None
 
-                for homeURL in homeURLs:
-                    self.failUnless(homeURL.startswith(calendarRootURL))
+                        if outboxURL and outboxURL.startswith(homeURL):
+                            self.failUnless(len(outboxURL) > len(homeURL))
+                            self.failUnless(outboxURL.endswith("/"))
+                            outboxURL = None
 
-                    if inboxURL and inboxURL.startswith(homeURL):
-                        self.failUnless(len(inboxURL) > len(homeURL))
-                        self.failUnless(inboxURL.endswith("/"))
-                        inboxURL = None
+                    self.failIf(inboxURL)
+                    self.failIf(outboxURL)
+        return gatherResults(ds)
 
-                    if outboxURL and outboxURL.startswith(homeURL):
-                        self.failUnless(len(outboxURL) > len(homeURL))
-                        self.failUnless(outboxURL.endswith("/"))
-                        outboxURL = None
-
-                self.failIf(inboxURL)
-                self.failIf(outboxURL)
-
     @inlineCallbacks
     def test_defaultAccessControlList_principals(self):
         """

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/freebusyurl.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/freebusyurl.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/freebusyurl.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -211,7 +211,7 @@
                 break
 
         # Get inbox details
-        inboxURL = principal.scheduleInboxURL()
+        inboxURL = yield principal.scheduleInboxURL(request)
         if inboxURL is None:
             raise HTTPError(StatusResponse(responsecode.INTERNAL_SERVER_ERROR, "No schedule inbox URL for principal: %s" % (principal,)))
         try:

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/icaldav.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/icaldav.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/icaldav.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -135,14 +135,14 @@
             free-busy for this principal's calendar user.
         """
 
-    def scheduleInboxURL():
+    def scheduleInboxURL(request=None):
         """
         Get the schedule INBOX URL for this principal's calendar user.
-        @return: a string containing the URL from the schedule-inbox-URL property.
+        @return: a Deferred that fires with a string containing the URL from the schedule-inbox-URL property.
         """
 
-    def scheduleOutboxURL():
+    def scheduleOutboxURL(request=None):
         """
         Get the schedule OUTBOX URL for this principal's calendar user.
-        @return: a string containing the URL from the schedule-outbox-URL property.
+        @return: a Deferred that fires with string containing the URL from the schedule-outbox-URL property.
         """

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/index.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/index.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/index.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -42,7 +42,7 @@
 
 from vobject.icalendar import utc
 
-from twisted.internet.defer import maybeDeferred, succeed
+from twisted.internet.defer import maybeDeferred, succeed, returnValue
 
 from twistedcaldav.ical import Component
 from twistedcaldav.query import calendarquery
@@ -191,7 +191,7 @@
             assert result is None, "More than one resource with UID %s in calendar collection %r" % (uid, self)
             result = name
 
-        return result
+        return succeed(result)
 
     def resourceUIDForName(self, name):
         """
@@ -218,7 +218,7 @@
         oldUID = self.resourceUIDForName(name)
         if oldUID is not None:
             self._delete_from_db(name, oldUID)
-        self._add_to_db(name, calendar, reCreate=reCreate)
+        self._add_to_db(name, calendar)
         if not fast:
             self._db_commit()
 
@@ -454,10 +454,12 @@
         Given a resource name, remove it from the database and re-add it
         with a longer expansion.
         """
-        calendar = self.resource.getChild(name).iCalendar()
-        self._add_to_db(name, calendar, expand_until=expand_until, reCreate=True)
-        self._db_commit()
-
+        d = self.resource.getChild(name).iCalendar()
+        def _gotCalendar(calendar):
+            self._add_to_db(name, calendar, expand_until=expand_until, reCreate=True)
+            self._db_commit()
+        return d.addCallback(_gotCalendar)
+        
     def _add_to_db(self, name, calendar, cursor = None, expand_until=None, reCreate=False):
         """
         Records the given calendar resource in the index with the given name.
@@ -742,8 +744,8 @@
         @return: True if the UID is not in the index and is not reserved,
             False otherwise.
         """
-        rname = self.resourceNameForUID(uid)
-        return (rname is None or rname in names)
+        rname = yield self.resourceNameForUID(uid) # TODO: Check in callers to isAllowedUID, change this to an inlineCallBack
+        returnValue(rname is None or rname in names)
 
     def _db_type(self):
         """

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/memcacheprops.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/memcacheprops.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/memcacheprops.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -182,23 +182,25 @@
         self.log_debug("Building cache for %s" % (self.collection,))
 
         cache = {}
-
+        ds = []
         for childName in childNames:
-            child = self.collection.getChild(childName)
-            if child is None:
-                continue
+            d = self.collection.getChild(childName)
+            def _gotChild(child):
+                if child is not None:
+                    propertyStore = child.deadProperties()
+                    props = {}
+                    for qname in propertyStore.list(cache=False):
+                        props[qname] = propertyStore.get(qname, cache=False)
 
-            propertyStore = child.deadProperties()
-            props = {}
-            for qname in propertyStore.list(cache=False):
-                props[qname] = propertyStore.get(qname, cache=False)
+                    cache[child.fp.path] = props
+            d.addCallback(_gotChild)
+            ds.append(d)
+        def _collectedChildren(_):
+            self._storeCache(cache)
+            return cache
+        return DeferredList(ds).addCallback(_collectedChildren)
 
-            cache[child.fp.path] = props
 
-        self._storeCache(cache)
-
-        return cache
-
     def setProperty(self, child, property, delete=False):
         propertyCache, key, childCache, token = self.childCache(child)
 

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/method/delete_common.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/method/delete_common.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/method/delete_common.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -143,7 +143,7 @@
         lock = None
         if not self.internal_request:
             # Get data we need for implicit scheduling
-            calendar = delresource.iCalendar()
+            calendar = yield delresource.iCalendar()
             scheduler = ImplicitScheduler()
             do_implicit_action, _ignore = (yield scheduler.testImplicitSchedulingDELETE(self.request, delresource, calendar))
             if do_implicit_action:

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/method/get.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/method/get.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/method/get.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -20,7 +20,7 @@
 
 __all__ = ["http_GET"]
 
-from twisted.internet.defer import inlineCallbacks, returnValue
+from twisted.internet.defer import inlineCallbacks, returnValue, succeed
 from twisted.web2.dav import davxml
 from twisted.web2.http import HTTPError
 from twisted.web2.http import Response

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/method/propfind.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/method/propfind.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/method/propfind.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -24,11 +24,10 @@
 """
 WebDAV PROPFIND method
 """
-
 __all__ = ["http_PROPFIND"]
 
 from twisted.python.failure import Failure
-from twisted.internet.defer import inlineCallbacks, returnValue
+from twisted.internet.defer import inlineCallbacks, returnValue, succeed
 from twisted.web2.http import HTTPError
 from twisted.web2 import responsecode
 from twisted.web2.http import StatusResponse

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/method/put_common.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/method/put_common.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/method/put_common.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -116,7 +116,7 @@
                         self.destination_created = False
                     if self.destination_index_deleted:
                         # Must read in calendar for destination being re-indexed
-                        self.storer.doDestinationIndex(self.storer.destination.iCalendar())
+                        self.storer.destination.iCalendar().addCallback(self.storer.doDestinationIndex)
                         self.destination_index_deleted = False
                         log.debug("Rollback: destination re-indexed %s" % (self.storer.destination.fp.path,))
                     if self.source_index_deleted:
@@ -313,7 +313,7 @@
                         raise HTTPError(ErrorResponse(responsecode.FORBIDDEN, (caldav_namespace, "supported-calendar-data")))
                 
                     # At this point we need the calendar data to do more tests
-                    self.calendar = self.source.iCalendar()
+                    self.calendar = yield self.source.iCalendar()
                 else:
                     try:
                         if type(self.calendar) in (types.StringType, types.UnicodeType,):
@@ -357,7 +357,7 @@
 
                 # FIXME: We need this here because we have to re-index the destination. Ideally it
                 # would be better to copy the index entries from the source and add to the destination.
-                self.calendar = self.source.iCalendar()
+                self.calendar = yield self.source.iCalendar()
 
             # Check access
             if self.destinationcal and config.EnablePrivateEvents:
@@ -368,7 +368,7 @@
 
         elif self.sourcecal:
             self.source_index = self.sourceparent.index()
-            self.calendar = self.source.iCalendar()
+            self.calendar = yield self.source.iCalendar()
     
     @inlineCallbacks
     def validCopyMoveOperation(self):
@@ -573,6 +573,7 @@
                 
         return succeed(None)
 
+    @inlineCallbacks
     def noUIDConflict(self, uid):
         """
         Check that the UID of the new calendar object conforms to the requirements of
@@ -595,7 +596,7 @@
         # UID must be unique
         index = self.destinationparent.index()
         if not index.isAllowedUID(uid, oldname, self.destination.fp.basename()):
-            rname = index.resourceNameForUID(uid)
+            rname = yield index.resourceNameForUID(uid)
             # This can happen if two simultaneous PUTs occur with the same UID.
             # i.e. one PUT has reserved the UID but has not yet written the resource,
             # the other PUT tries to reserve and fails but no index entry exists yet.
@@ -613,7 +614,7 @@
                     result = False
                     message = "Cannot overwrite calendar resource %s with different UID %s" % (rname, olduid)
         
-        return result, message, rname
+        returnValue((result, message, rname))
 
     @inlineCallbacks
     def checkQuota(self):
@@ -702,11 +703,9 @@
             if old_has_private_comments and not new_has_private_comments:
                 # Transfer old comments to new calendar
                 log.debug("Private Comments properties were entirely removed by the client. Restoring existing properties.")
-                old_calendar = self.destination.iCalendar()
-                self.calendar.transferProperties(old_calendar, (
-                    "X-CALENDARSERVER-PRIVATE-COMMENT",
-                    "X-CALENDARSERVER-ATTENDEE-COMMENT",
-                ))
+                self.destination.iCalendar().addCallback(self.calendar.transferProperties,
+                                                         "X-CALENDARSERVER-PRIVATE-COMMENT",
+                                                         "X-CALENDARSERVER-ATTENDEE-COMMENT")
                 self.calendardata = None
         
         return new_has_private_comments
@@ -948,7 +947,7 @@
                 # UID conflict check - note we do this after reserving the UID to avoid a race condition where two requests
                 # try to write the same calendar data to two different resource URIs.
                 if not self.isiTIP:
-                    result, message, rname = self.noUIDConflict(self.uid)
+                    result, message, rname = yield self.noUIDConflict(self.uid)
                     if not result:
                         log.err(message)
                         raise HTTPError(ErrorResponse(responsecode.FORBIDDEN,

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/method/report_calquery.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/method/report_calquery.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/method/report_calquery.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -173,11 +173,13 @@
                 try:
                     # Get list of children that match the search and have read
                     # access
+                    results = yield calresource.index().indexedSearch(filter)
                     names = [name for name, ignore_uid, ignore_type
-                        in calresource.index().indexedSearch(filter)]
+                        in results]
                 except IndexedSearchException:
+                    results = calresource.index().bruteForceSearch()
                     names = [name for name, ignore_uid, ignore_type
-                        in calresource.index().bruteForceSearch()]
+                        in results]
                     index_query_ok = False
 
                 if not names:
@@ -200,7 +202,7 @@
                     child_path_name = urllib.unquote(child_uri_name)
                     
                     if generate_calendar_data or not index_query_ok:
-                        calendar = calresource.iCalendar(child_path_name)
+                        calendar = yield calresource.iCalendar(child_path_name)
                         assert calendar is not None, "Calendar %s is missing from calendar collection %r" % (child_uri_name, self)
                     else:
                         calendar = None
@@ -224,7 +226,7 @@
             # Check private events access status
             isowner = (yield calresource.isOwner(request, adminprincipals=True, readprincipals=True))
 
-            calendar = calresource.iCalendar()
+            calendar = yield calresource.iCalendar()
             yield queryCalendarObjectResource(calresource, uri, None, calendar, timezone)
 
         returnValue(True)

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/method/report_common.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/method/report_common.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/method/report_common.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -253,9 +253,9 @@
                 access = None
 
             if calendar:
-                propvalue = property.elementFromCalendarWithAccessRestrictions(calendar, access, timezone)
+                propvalue = yield property.elementFromCalendarWithAccessRestrictions(calendar, access, timezone)
             else:
-                propvalue = property.elementFromResourceWithAccessRestrictions(resource, access, timezone)
+                propvalue = yield property.elementFromResourceWithAccessRestrictions(resource, access, timezone)
             if propvalue is None:
                 raise ValueError("Invalid CalDAV:calendar-data for request: %r" % (property,))
             properties_by_status[responsecode.OK].append(propvalue)

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/resource.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/resource.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/resource.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -316,13 +316,13 @@
             principal = (yield self.ownerPrincipal(request))
             
             # Map owner to their inbox
-            inboxURL = principal.scheduleInboxURL()
+            inboxURL = yield principal.scheduleInboxURL(request)
             if inboxURL:
                 inbox = (yield request.locateResource(inboxURL))
                 myurl = (yield self.canonicalURL(request))
                 inbox.processFreeBusyCalendar(myurl, property.children[0] == caldavxml.Opaque())
 
-        result = (yield super(CalDAVResource, self).writeProperty(property, request))
+        result = yield super(CalDAVResource, self).writeProperty(property, request)
         returnValue(result)
 
     def writeDeadProperty(self, property):
@@ -553,10 +553,10 @@
         """
         
         # For backwards compatibility we need to sync this up with the calendar-free-busy-set on the inbox
-        principal = (yield self.ownerPrincipal(request))
-        inboxURL = principal.scheduleInboxURL()
+        principal = yield self.ownerPrincipal(request)
+        inboxURL = yield principal.scheduleInboxURL(request)
         if inboxURL:
-            inbox = (yield request.locateResource(inboxURL))
+            inbox = yield request.locateResource(inboxURL)
             inbox.processFreeBusyCalendar(request.path, False)
 
     @inlineCallbacks
@@ -566,12 +566,12 @@
         """
         
         # For backwards compatibility we need to sync this up with the calendar-free-busy-set on the inbox
-        principal = (yield self.ownerPrincipal(request))
-        inboxURL = principal.scheduleInboxURL()
+        principal = yield self.ownerPrincipal(request)
+        inboxURL = yield principal.scheduleInboxURL(request)
         if inboxURL:
             (_ignore_scheme, _ignore_host, destination_path, _ignore_query, _ignore_fragment) = urlsplit(normalizeURL(destination_uri))
 
-            inbox = (yield request.locateResource(inboxURL))
+            inbox = yield request.locateResource(inboxURL)
             inbox.processFreeBusyCalendar(request.path, False)
             inbox.processFreeBusyCalendar(destination_uri, destination.isCalendarOpaque())
             
@@ -596,7 +596,7 @@
         
         # Not allowed to delete the default calendar
         principal = (yield self.ownerPrincipal(request))
-        inboxURL = principal.scheduleInboxURL()
+        inboxURL = yield principal.scheduleInboxURL(request)
         if inboxURL:
             inbox = (yield request.locateResource(inboxURL))
             default = (yield inbox.readProperty((caldav_namespace, "schedule-default-calendar-URL"), request))
@@ -618,14 +618,18 @@
         an infinite loop.  A subclass must override one of both of these
         methods.
         """
-        calendar_data = self.iCalendarText(name)
+        d = self.iCalendarText(name)
+        def _gotData(calendar_data):
+            if calendar_data is None:
+                return None
 
-        if calendar_data is None: return None
+            try:
+                return iComponent.fromString(calendar_data)
+            except ValueError:
+                return None
 
-        try:
-            return iComponent.fromString(calendar_data)
-        except ValueError:
-            return None
+        d.addCallback(_gotData)
+        return d
 
     def iCalendarRolledup(self, request):
         """
@@ -648,7 +652,7 @@
         an infinite loop.  A subclass must override one of both of these
         methods.
         """
-        return str(self.iCalendar(name))
+        return self.iCalendar(name).addCallback(str)
 
     def iCalendarXML(self, name=None):
         """
@@ -656,7 +660,7 @@
         This implementation returns an XML element constructed from the object
         returned by L{iCalendar} when given the same arguments.
         """
-        return caldavxml.CalendarData.fromCalendar(self.iCalendar(name))
+        return self.iCalendar(name).addCallback(caldavxml.CalendarData.fromCalendar)
 
     def iCalendarAddressDoNormalization(self, ical):
         """
@@ -855,8 +859,9 @@
 
         if namespace == caldav_namespace:
             if name == "calendar-home-set":
+                urls = yield self.calendarHomeURLs(request)
                 returnValue(caldavxml.CalendarHomeSet(
-                    *[davxml.HRef(url) for url in self.calendarHomeURLs()]
+                    *[davxml.HRef(url) for url in urls]
                 ))
 
             elif name == "calendar-user-address-set":
@@ -865,14 +870,14 @@
                 ))
 
             elif name == "schedule-inbox-URL":
-                url = self.scheduleInboxURL()
+                url = yield self.scheduleInboxURL(request)
                 if url is None:
                     returnValue(None)
                 else:
                     returnValue(caldavxml.ScheduleInboxURL(davxml.HRef(url)))
 
             elif name == "schedule-outbox-URL":
-                url = self.scheduleOutboxURL()
+                url = yield self.scheduleOutboxURL(request)
                 if url is None:
                     returnValue(None)
                 else:
@@ -957,38 +962,38 @@
         d.addCallback(gotInbox)
         return d
 
-    def scheduleInbox(self, request):
+    def scheduleInbox(self, request=None):
         """
         @return: the deferred schedule inbox for this principal.
         """
-        return request.locateResource(self.scheduleInboxURL())
+        return self.scheduleInboxURL(request).addCallback(request.locateResource)
 
-    def scheduleInboxURL(self):
+    def scheduleInboxURL(self, request=None):
         if self.hasDeadProperty((caldav_namespace, "schedule-inbox-URL")):
             inbox = self.readDeadProperty((caldav_namespace, "schedule-inbox-URL"))
-            return str(inbox.children[0])
+            return succeed(str(inbox.children[0]))
         else:
-            return None
+            return succeed(None)
 
-    def scheduleOutboxURL(self):
+    def scheduleOutboxURL(self, request=None):
         """
         @return: the schedule outbox URL for this principal.
         """
         if self.hasDeadProperty((caldav_namespace, "schedule-outbox-URL")):
             outbox = self.readDeadProperty((caldav_namespace, "schedule-outbox-URL"))
-            return str(outbox.children[0])
+            return succeed(str(outbox.children[0]))
         else:
-            return None
+            return succeed(None)
 
-    def dropboxURL(self):
+    def dropboxURL(self, request=None):
         """
         @return: the drop box home collection URL for this principal.
         """
         if self.hasDeadProperty((calendarserver_namespace, "dropbox-home-URL")):
             inbox = self.readDeadProperty((caldav_namespace, "dropbox-home-URL"))
-            return str(inbox.children[0])
+            return succeed(str(inbox.children[0]))
         else:
-            return None
+            return succeed(None)
 
     ##
     # Quota

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/scheduling/implicit.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/scheduling/implicit.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/scheduling/implicit.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -69,7 +69,8 @@
         self.internal_request = internal_request
 
         existing_resource = resource.exists()
-        existing_type = "schedule" if self.checkSchedulingObjectResource(resource) else "calendar"
+        schedulingObject = yield self.checkSchedulingObjectResource(resource)
+        existing_type = "schedule" if schedulingObject else "calendar"
         new_type = "schedule" if (yield self.checkImplicitState()) else "calendar"
 
         if existing_type == "calendar":
@@ -86,7 +87,7 @@
             # Also make sure that we return the new calendar being written rather than the old one
             # when the implicit action is executed
             self.return_calendar = calendar
-            self.calendar = resource.iCalendar()
+            self.calendar = yield resource.iCalendar()
             yield self.checkImplicitState()
         
         # Attendees are not allowed to overwrite one type with another
@@ -106,8 +107,9 @@
         new_type = "schedule" if (yield self.checkImplicitState()) else "calendar"
 
         dest_exists = destresource.exists()
-        dest_is_implicit = self.checkSchedulingObjectResource(destresource)
-        src_is_implicit = self.checkSchedulingObjectResource(srcresource) or new_type == "schedule"
+        dest_is_implicit = yield self.checkSchedulingObjectResource(destresource)
+        src_is_implicit = yield self.checkSchedulingObjectResource(srcresource)
+        src_is_implicit = src_is_implicit or new_type == "schedule"
 
         if srccal and destcal:
             if src_is_implicit and dest_exists or dest_is_implicit:
@@ -136,8 +138,9 @@
 
         new_type = "schedule" if (yield self.checkImplicitState()) else "calendar"
 
-        dest_is_implicit = self.checkSchedulingObjectResource(destresource)
-        src_is_implicit = self.checkSchedulingObjectResource(srcresource) or new_type == "schedule"
+        dest_is_implicit = yield self.checkSchedulingObjectResource(destresource)
+        src_is_implicit = yield self.checkSchedulingObjectResource(srcresource)
+        src_is_implicit = src_is_implicit or new_type == "schedule"
 
         if srccal and destcal:
             if src_is_implicit or dest_is_implicit:
@@ -165,11 +168,13 @@
 
         yield self.checkImplicitState()
 
-        resource_type = "schedule" if self.checkSchedulingObjectResource(resource) else "calendar"
+        schedulingObject = yield self.checkSchedulingObjectResource(resource)
+        resource_type = "schedule" if schedulingObject else "calendar"
         self.action = "remove" if resource_type == "schedule" else "none"
 
         returnValue((self.action != "none", False,))
 
+    @inlineCallbacks
     def checkSchedulingObjectResource(self, resource):
         
         if resource and resource.exists():
@@ -178,24 +183,24 @@
             except HTTPError:
                 implicit = None
             if implicit is not None:
-                return implicit != "false"
+                returnValue(implicit != "false")
             else:
-                calendar = resource.iCalendar()
+                calendar = yield resource.iCalendar()
                 # Get the ORGANIZER and verify it is the same for all components
                 try:
                     organizer = calendar.validOrganizerForScheduling()
                 except ValueError:
                     # We have different ORGANIZERs in the same iCalendar object - this is an error
-                    return False
+                    returnValue(False)
                 organizerPrincipal = resource.principalForCalendarUserAddress(organizer) if organizer else None
                 resource.writeDeadProperty(TwistedSchedulingObjectResource("true" if organizerPrincipal != None else "false"))
                 log.debug("Implicit - checked scheduling object resource state for UID: '%s', result: %s" % (
                     calendar.resourceUID(),
                     "true" if organizerPrincipal != None else "false",
                 ))
-                return organizerPrincipal != None
+                returnValue(organizerPrincipal != None)
 
-        return False
+        returnValue(False)
         
     @inlineCallbacks
     def checkImplicitState(self):
@@ -367,7 +372,7 @@
 
         # Get owner's calendar-home
         calendar_owner_principal = (yield self.resource.ownerPrincipal(self.request))
-        calendar_home = calendar_owner_principal.calendarHome()
+        calendar_home = yield calendar_owner_principal.calendarHome(self.request)
         
         check_parent_uri = parentForURL(check_uri)[:-1] if check_uri else None
 
@@ -379,12 +384,13 @@
 
         @inlineCallbacks
         def queryCalendarCollection(collection, collection_uri):
-            rname = collection.index().resourceNameForUID(self.uid)
+            rname = yield collection.index().resourceNameForUID(self.uid)
             if rname:
                 child = (yield self.request.locateResource(joinURL(collection_uri, rname)))
                 if child == check_resource:
                     returnValue(True)
-                matched_type = "schedule" if self.checkSchedulingObjectResource(child) else "calendar"
+                schedulingObject = yield self.checkSchedulingObjectResource(child)
+                matched_type = "schedule" if schedulingObject else "calendar"
                 if (
                     collection_uri != check_parent_uri and
                     (type == "schedule" or matched_type == "schedule")
@@ -452,7 +458,7 @@
         """
         
         # Find outbox
-        outboxURL = principal.scheduleOutboxURL()
+        outboxURL = yield principal.scheduleOutboxURL(self.request)
         outbox = (yield self.request.locateResource(outboxURL))
         yield outbox.authorize(self.request, (caldavxml.ScheduleSend(),))
 
@@ -481,7 +487,7 @@
         elif self.action == "modify":
 
             # Read in existing data
-            self.oldcalendar = self.resource.iCalendar()
+            self.oldcalendar = yield self.resource.iCalendar()
             
             # Significant change
             no_change, self.changed_rids, reinvites, recurrence_reschedule = self.isOrganizerChangeInsignificant()

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/scheduling/scheduler.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/scheduling/scheduler.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/scheduling/scheduler.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -15,9 +15,8 @@
 ##
 
 from twext.web2.dav.davxml import ErrorResponse
+from twisted.internet.defer import inlineCallbacks, returnValue, fail, succeed
 
-from twisted.internet.defer import inlineCallbacks, returnValue
-
 from twisted.python.failure import Failure
 
 from twisted.web2 import responsecode
@@ -488,7 +487,7 @@
             else:
                 # Map recipient to their inbox
                 inbox = None
-                inboxURL = principal.scheduleInboxURL()
+                inboxURL = yield principal.scheduleInboxURL()
                 if inboxURL:
                     inbox = (yield self.request.locateResource(inboxURL))
 
@@ -509,7 +508,7 @@
         # Verify that the ORGANIZER's cu address maps to a valid user
         organizer = self.calendar.getOrganizer()
         if organizer:
-            organizerPrincipal = self.resource.principalForCalendarUserAddress(organizer)
+            organizerPrincipal = yield self.resource.principalForCalendarUserAddress(organizer)
             if organizerPrincipal:
                 outboxURL = organizerPrincipal.scheduleOutboxURL()
                 if outboxURL:
@@ -533,16 +532,20 @@
             raise HTTPError(ErrorResponse(responsecode.FORBIDDEN, (caldav_namespace, "organizer-allowed")))
 
     def checkOrganizerAsOriginator(self):
-
+        def _checkoutboxurl(outboxuri):
+            if outboxuri != self.request.uri:
+                log.err("Wrong outbox for ORGANIZER in calendar data: %s" % (self.calendar,))
+                raise HTTPError(ErrorResponse(responsecode.FORBIDDEN, (caldav_namespace, "organizer-allowed")))
+            return outboxuri
+                
         # Make sure that the ORGANIZER is local
         if not isinstance(self.organizer, LocalCalendarUser):
             log.err("ORGANIZER is not local to server in calendar data: %s" % (self.calendar,))
             raise HTTPError(ErrorResponse(responsecode.FORBIDDEN, (caldav_namespace, "organizer-allowed")))
 
         # Make sure that the ORGANIZER's Outbox is the request URI
-        if self.doingPOST and self.organizer.principal.scheduleOutboxURL() != self.request.uri:
-            log.err("Wrong outbox for ORGANIZER in calendar data: %s" % (self.calendar,))
-            raise HTTPError(ErrorResponse(responsecode.FORBIDDEN, (caldav_namespace, "organizer-allowed")))
+        if self.doingPOST:
+            return self.organizer.principal.scheduleOutboxURL().addCallback(_checkoutboxurl) 
 
     def checkAttendeeAsOriginator(self):
         """
@@ -556,19 +559,23 @@
         # Must have only one
         if len(attendees) != 1:
             log.err("Wrong number of ATTENDEEs in calendar data: %s" % (self.calendar,))
-            raise HTTPError(ErrorResponse(responsecode.FORBIDDEN, (caldav_namespace, "attendee-allowed")))
+            return fail(HTTPError(ErrorResponse(responsecode.FORBIDDEN, (caldav_namespace, "attendee-allowed"))))
         attendee = attendees[0]
     
         # Attendee's Outbox MUST be the request URI
         attendeePrincipal = self.resource.principalForCalendarUserAddress(attendee)
-        if attendeePrincipal:
-            if self.doingPOST and attendeePrincipal.scheduleOutboxURL() != self.request.uri:
-                log.err("ATTENDEE in calendar data does not match owner of Outbox: %s" % (self.calendar,))
+        d = attendeePrincipal.scheduleOutboxURL(self.request)
+        def _gotOutboxURL(outboxURL):
+            if attendeePrincipal:
+                if self.doingPOST and outboxURL != self.request.uri:
+                    log.err("ATTENDEE in calendar data does not match owner of Outbox: %s" % (self.calendar,))
+                    raise HTTPError(ErrorResponse(responsecode.FORBIDDEN, (caldav_namespace, "attendee-allowed")))
+            else:
+                log.err("Unknown ATTENDEE in calendar data: %s" % (self.calendar,))
                 raise HTTPError(ErrorResponse(responsecode.FORBIDDEN, (caldav_namespace, "attendee-allowed")))
-        else:
-            log.err("Unknown ATTENDEE in calendar data: %s" % (self.calendar,))
-            raise HTTPError(ErrorResponse(responsecode.FORBIDDEN, (caldav_namespace, "attendee-allowed")))
     
+        return d.addCallback(_gotOutboxURL)
+
     def securityChecks(self):
         """
         Check that the originator has the appropriate rights to send this type of iTIP message.
@@ -576,15 +583,18 @@
     
         # Prevent spoofing of ORGANIZER with specific METHODs when local
         if self.calendar.propertyValue("METHOD") in ("PUBLISH", "REQUEST", "ADD", "CANCEL", "DECLINECOUNTER"):
-            self.checkOrganizerAsOriginator()
+            return self.checkOrganizerAsOriginator()
     
         # Prevent spoofing when doing reply-like METHODs
         elif self.calendar.propertyValue("METHOD") in ("REPLY", "COUNTER", "REFRESH"):
-            self.checkAttendeeAsOriginator()
+            return self.checkAttendeeAsOriginator()
             
         else:
             log.err("Unknown iTIP METHOD for security checks: %s" % (self.calendar.propertyValue("METHOD"),))
-            raise HTTPError(ErrorResponse(responsecode.FORBIDDEN, (caldav_namespace, "valid-calendar-data"), description="Unknown iTIP METHOD for security checks"))
+            return fail(HTTPError(ErrorResponse(
+                       responsecode.FORBIDDEN,
+                       (caldav_namespace, "valid-calendar-data"),
+                       description="Unknown iTIP METHOD for security checks")))
 
     def finalChecks(self):
         """
@@ -700,7 +710,7 @@
             else:
                 # Map recipient to their inbox
                 inbox = None
-                inboxURL = principal.scheduleInboxURL()
+                inboxURL = yield principal.scheduleInboxURL()
                 if inboxURL:
                     inbox = (yield self.request.locateResource(inboxURL))
 
@@ -887,7 +897,7 @@
             else:
                 # Map recipient to their inbox
                 inbox = None
-                inboxURL = principal.scheduleInboxURL()
+                inboxURL = yield principal.scheduleInboxURL()
                 if inboxURL:
                     inbox = (yield self.request.locateResource(inboxURL))
 

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/scheduling/utils.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/scheduling/utils.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/scheduling/utils.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -37,9 +37,7 @@
         request._rememberResource(calendar_home, calendar_home.url())
 
         # Run a UID query against the UID
-
-        def queryCalendarCollection(collection, uri):
-            rname = collection.index().resourceNameForUID(uid)
+        def queryCalendarCollection(rname, collection, uri):
             if rname:
                 result["resource"] = collection.getChild(rname)
                 result["resource_name"] = rname
@@ -48,9 +46,10 @@
                 return succeed(False)
             else:
                 return succeed(True)
-        
+        def getResourceName(collection, uri):
+            return collection.index().resourceNameForUID(uid).addCallback(queryCalendarCollection, collection, uri)
         # NB We are by-passing privilege checking here. That should be OK as the data found is not
         # exposed to the user.
-        yield report_common.applyToCalendarCollections(calendar_home, request, calendar_home.url(), "infinity", queryCalendarCollection, None)
+        yield report_common.applyToCalendarCollections(calendar_home, request, calendar_home.url(), "infinity", getResourceName, None)
 
     returnValue((result["resource"], result["resource_name"], result["calendar_collection"], result["calendar_collection_uri"],))

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/static.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/static.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/static.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -34,14 +34,15 @@
     "TimezoneServiceFile",
 ]
 
+import os
 import datetime
-import os
 import errno
 from urlparse import urlsplit
 
 from twext.web2.dav.davxml import ErrorResponse
+from twisted.internet.defer import (fail, succeed, inlineCallbacks, returnValue,
+                                    maybeDeferred, Deferred)
 
-from twisted.internet.defer import fail, succeed, inlineCallbacks, returnValue, maybeDeferred
 from twisted.python.failure import Failure
 from twisted.web2 import responsecode, http, http_headers
 from twisted.web2.http import HTTPError, StatusResponse
@@ -86,6 +87,15 @@
     """
     CalDAV-accessible L{DAVFile} resource.
     """
+    propertyStore = CachingPropertyStore
+
+    @classmethod
+    def fetch(cls, request, path, *args, **kwargs):
+        """
+        stuff etc
+        """
+        return succeed(cls(path, *args, **kwargs))
+
     def __repr__(self):
         if self.isCalendarCollection():
             return "<%s (calendar collection): %s>" % (self.__class__.__name__, self.fp.path)
@@ -274,7 +284,7 @@
                         continue
 
                     # Get the access filtered view of the data
-                    caldata = child.iCalendarTextFiltered(isowner)
+                    caldata = yield child.iCalendarTextFiltered(isowner)
                     try:
                         subcalendar = iComponent.fromString(caldata)
                     except ValueError:
@@ -314,7 +324,7 @@
     def iCalendarText(self, name=None):
         if self.isPseudoCalendarCollection():
             if name is None:
-                return str(self.iCalendar())
+                return self.iCalendar().addCallback(str)
 
             try:
                 calendar_file = self.fp.child(name).open()
@@ -323,7 +333,7 @@
                 raise
 
         elif self.isCollection():
-            return None
+            return succeed(None)
 
         else:
             if name is not None:
@@ -337,10 +347,10 @@
         finally:
             calendar_file.close()
 
-        return calendar_data
+        return succeed(calendar_data)
 
     def iCalendarXML(self, name=None):
-        return caldavxml.CalendarData.fromCalendar(self.iCalendarText(name))
+        return self.iCalendarText(name).addCallback(caldavxml.CalendarData.fromCalendar)
 
     def supportedPrivileges(self, request):
         # read-free-busy support on calendar collection and calendar object resources
@@ -389,48 +399,34 @@
 
     def createSimilarFile(self, path):
         if path == self.fp.path:
-            return self
+            return succeed(self)
 
-        similar = super(CalDAVFile, self).createSimilarFile(path)
+        d = super(CalDAVFile, self).createSimilarFile(path)
+        def _gotFile(similar):
+            if isCalendarCollectionResource(self):
+                #
+                # Override DELETE, MOVE
+                #
+                for method in ("DELETE", "MOVE"):
+                    method = "http_" + method
+                    original = getattr(similar, method)
 
-        if isCalendarCollectionResource(self):
-            #
-            # Override the dead property store
-            #
-            superDeadProperties = similar.deadProperties
+                    @inlineCallbacks
+                    def override(request, original=original):
 
-            def deadProperties():
-                if not hasattr(similar, "_dead_properties"):
-                    similar._dead_properties = self.propertyCollection().propertyStoreForChild(
-                        similar,
-                        superDeadProperties(caching=False)
-                    )
-                return similar._dead_properties
+                        # Call original method (which is deferred)
+                        response = (yield original(request))
 
-            similar.deadProperties = deadProperties
+                        # Wipe the cache
+                        similar.deadProperties().flushCache()
 
-            #
-            # Override DELETE, MOVE
-            #
-            for method in ("DELETE", "MOVE"):
-                method = "http_" + method
-                original = getattr(similar, method)
+                        returnValue(response)
 
-                @inlineCallbacks
-                def override(request, original=original):
+                    setattr(similar, method, override)
 
-                    # Call original method (which is deferred)
-                    response = (yield original(request))
+            return similar
+        return d.addCallback(_gotFile)
 
-                    # Wipe the cache
-                    similar.deadProperties().flushCache()
-
-                    returnValue(response)
-
-                setattr(similar, method, override)
-
-        return similar
-
     def updateCTag(self):
         assert self.isCollection()
         try:
@@ -554,21 +550,19 @@
 
 class AutoProvisioningFileMixIn (AutoProvisioningResourceMixIn):
     def provision(self):
-        self.provisionFile()
-        return super(AutoProvisioningFileMixIn, self).provision()
+        return self.provisionFile()
 
 
-    def provisionFile(self):
+    def provisionFile(self, request=None):
         if hasattr(self, "_provisioned_file"):
             return False
         else:
             self._provisioned_file = True
 
         fp = self.fp
-
         fp.restat(False)
         if fp.exists():
-            return False
+            return succeed(False)
 
         log.msg("Provisioning file: %s" % (self,))
 
@@ -593,7 +587,7 @@
             fp.open("w").close()
             fp.restat(False)
 
-        return True
+        return succeed(True)
 
 class CalendarHomeProvisioningFile (AutoProvisioningFileMixIn, DirectoryCalendarHomeProvisioningResource, DAVFile):
     """
@@ -653,56 +647,57 @@
         assert len(name) > 4, "Directory record has an invalid GUID: %r" % (name,)
         
         childPath = self.fp.child(name[0:2]).child(name[2:4]).child(name)
-        child = self.homeResourceClass(childPath.path, self, record)
+        d = self.homeResourceClass.fetch(None, childPath.path, self, record)
+        def _gotChild(child):
+            if not child.exists():
+                self.provision()
 
-        if not child.exists():
-            self.provision()
+                if not childPath.parent().isdir():
+                    childPath.parent().makedirs()
 
-            if not childPath.parent().isdir():
-                childPath.parent().makedirs()
+                for oldPath in (
+                    # Pre 2.0: All in one directory
+                    self.fp.child(name),
+                    # Pre 1.2: In types hierarchy instead of the GUID hierarchy
+                    self.parent.getChild(record.recordType).fp.child(record.shortNames[0]),
+                ):
+                    if oldPath.exists():
+                        # The child exists at an old location.  Move to new location.
+                        log.msg("Moving calendar home from old location %r to new location %r." % (oldPath, childPath))
+                        try:
+                            oldPath.moveTo(childPath)
+                        except (OSError, IOError), e:
+                            log.err("Error moving calendar home %r: %s" % (oldPath, e))
+                            raise HTTPError(StatusResponse(
+                                responsecode.INTERNAL_SERVER_ERROR,
+                                "Unable to move calendar home."
+                            ))
+                        child.fp.restat(False)
+                        break
+                else:
+                    #
+                    # NOTE: provisionDefaultCalendars() returns a deferred, which we are ignoring.
+                    # The result being that the default calendars will be present at some point
+                    # in the future, not necessarily right now, and we don't have a way to wait
+                    # on that to finish.
+                    #
+                    child.provisionDefaultCalendars()
 
-            for oldPath in (
-                # Pre 2.0: All in one directory
-                self.fp.child(name),
-                # Pre 1.2: In types hierarchy instead of the GUID hierarchy
-                self.parent.getChild(record.recordType).fp.child(record.shortNames[0]),
-            ):
-                if oldPath.exists():
-                    # The child exists at an old location.  Move to new location.
-                    log.msg("Moving calendar home from old location %r to new location %r." % (oldPath, childPath))
-                    try:
-                        oldPath.moveTo(childPath)
-                    except (OSError, IOError), e:
-                        log.err("Error moving calendar home %r: %s" % (oldPath, e))
+                    #
+                    # Try to work around the above a little by telling the client that something
+                    # when wrong temporarily if the child isn't provisioned right away.
+                    #
+                    if not child.exists():
                         raise HTTPError(StatusResponse(
-                            responsecode.INTERNAL_SERVER_ERROR,
-                            "Unable to move calendar home."
+                            responsecode.SERVICE_UNAVAILABLE,
+                            "Provisioning calendar home."
                         ))
-                    child.fp.restat(False)
-                    break
-            else:
-                #
-                # NOTE: provisionDefaultCalendars() returns a deferred, which we are ignoring.
-                # The result being that the default calendars will be present at some point
-                # in the future, not necessarily right now, and we don't have a way to wait
-                # on that to finish.
-                #
-                child.provisionDefaultCalendars()
 
-                #
-                # Try to work around the above a little by telling the client that something
-                # when wrong temporarily if the child isn't provisioned right away.
-                #
-                if not child.exists():
-                    raise HTTPError(StatusResponse(
-                        responsecode.SERVICE_UNAVAILABLE,
-                        "Provisioning calendar home."
-                    ))
+                assert child.exists()
 
-            assert child.exists()
+            return child
+        return d.addCallback(_gotChild)
 
-        return child
-
     def createSimilarFile(self, path):
         raise HTTPError(responsecode.NOT_FOUND)
 
@@ -746,21 +741,26 @@
         }.get(name, None)
 
         if cls is not None:
-            child = cls(self.fp.child(name).path, self)
-            child.cacheNotifier = self.cacheNotifier
-            child.clientNotifier = self.clientNotifier
-            return child
+            d = cls.fetch(None, self.fp.child(name).path, self)
+            def _gotChild(child):
+                child.cacheNotifier = self.cacheNotifier
+                child.clientNotifier = self.clientNotifier
+                return child
+            return d.addCallback(_gotChild)
 
         return self.createSimilarFile(self.fp.child(name).path)
 
+
     def createSimilarFile(self, path):
         if path == self.fp.path:
             return self
         else:
-            similar = CalDAVFile(path, principalCollections=self.principalCollections())
-            similar.cacheNotifier = self.cacheNotifier
-            similar.clientNotifier = self.clientNotifier
-            return similar
+            d = CalDAVFile.fetch(None, path, principalCollections=self.principalCollections())
+            def _gotChild(similar):
+                similar.cacheNotifier = self.cacheNotifier
+                similar.clientNotifier = self.clientNotifier
+                return similar
+            return d.addCallback(_gotChild)
 
     def getChild(self, name):
         # This avoids finding case variants of put children on case-insensitive filesystems.

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/test/test_memcacheprops.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/test/test_memcacheprops.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/test/test_memcacheprops.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -27,6 +27,7 @@
 import os
 
 from twisted.web2.http import HTTPError
+from twisted.internet.defer import succeed
 
 from twistedcaldav.memcacheprops import MemcachePropertyCollection
 from twistedcaldav.test.util import InMemoryPropertyStore
@@ -48,7 +49,7 @@
         return self.children.iterkeys()
 
     def getChild(self, childName):
-        return self.children[childName]
+        return succeed(self.children[childName])
 
     def propertyCollection(self):
         if not hasattr(self, "_propertyCollection"):

Modified: CalendarServer/branches/more-deferreds/twistedcaldav/test/test_static.py
===================================================================
--- CalendarServer/branches/more-deferreds/twistedcaldav/test/test_static.py	2009-07-09 23:45:23 UTC (rev 4445)
+++ CalendarServer/branches/more-deferreds/twistedcaldav/test/test_static.py	2009-07-09 23:56:11 UTC (rev 4446)
@@ -38,8 +38,10 @@
 
 
     def test_childrenHaveCacheNotifier(self):
-        child = self.calendarHome.createSimilarFile('/fake/path')
-        self.assertEquals(child.cacheNotifier, self.calendarHome.cacheNotifier)
+        d = self.calendarHome.createSimilarFile('/fake/path')
+        def _gotResource(child):
+            self.assertEquals(child.cacheNotifier, self.calendarHome.cacheNotifier)
+        return d.addCallback(_gotResource)
 
 
 class CalDAVFileTests(TestCase):
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20090709/6c1e7f3f/attachment-0001.html>


More information about the calendarserver-changes mailing list