[CalendarServer-changes] [6326] CalendarServer/branches/users/glyph/more-deferreds-6/twistedcaldav

source_changes at macosforge.org source_changes at macosforge.org
Tue Sep 21 12:08:12 PDT 2010


Revision: 6326
          http://trac.macosforge.org/projects/calendarserver/changeset/6326
Author:   glyph at apple.com
Date:     2010-09-21 12:08:08 -0700 (Tue, 21 Sep 2010)
Log Message:
-----------
support deferred returns from makeChild (and getChild)

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/more-deferreds-6/twistedcaldav/extensions.py
    CalendarServer/branches/users/glyph/more-deferreds-6/twistedcaldav/test/test_extensions.py

Modified: CalendarServer/branches/users/glyph/more-deferreds-6/twistedcaldav/extensions.py
===================================================================
--- CalendarServer/branches/users/glyph/more-deferreds-6/twistedcaldav/extensions.py	2010-09-21 18:40:17 UTC (rev 6325)
+++ CalendarServer/branches/users/glyph/more-deferreds-6/twistedcaldav/extensions.py	2010-09-21 19:08:08 UTC (rev 6326)
@@ -38,7 +38,7 @@
 from twisted.cred.error import LoginFailed, UnauthorizedLogin
 
 import twext.web2.server
-from twext.web2 import responsecode, iweb, http, server
+from twext.web2 import responsecode, server
 from twext.web2.auth.wrapper import UnauthorizedResponse
 from twext.web2.http import HTTPError, Response, RedirectResponse
 from twext.web2.http import StatusResponse
@@ -734,6 +734,7 @@
         self.putChildren = {}
         super(DAVResourceWithChildrenMixin, self).__init__(principalCollections=principalCollections)
 
+
     def putChild(self, name, child):
         """
         Register a child with the given name with this resource.
@@ -742,9 +743,12 @@
         """
         self.putChildren[name] = child
 
+
     def getChild(self, name):
         """
-        Look up a child resource.
+        Look up a child resource.  First check C{self.putChildren}, then call
+        C{self.makeChild} if no pre-existing children were found.
+
         @return: the child of this resource with the given name.
         """
         if name == "":
@@ -755,23 +759,34 @@
             result = self.makeChild(name)
         return result
 
+
     def makeChild(self, name):
-        # Subclasses with real children need to override this and return the appropriate object
+        """
+        Called by L{DAVResourceWithChildrenMixin.getChild} to dynamically
+        create children that have not been pre-created with C{putChild}.
+        """
         return None
 
+
     def listChildren(self):
         """
         @return: a sequence of the names of all known children of this resource.
         """
         return self.putChildren.keys()
 
+
     def locateChild(self, req, segments):
         """
-        See L{IResource}C{.locateChild}.
+        See L{IResource.locateChild}.
         """
-        # If getChild() finds a child resource, return it
-        return (self.getChild(segments[0]), segments[1:])
+        thisSegment = segments[0]
+        moreSegments = segments[1:]
+        return maybeDeferred(self.getChild, thisSegment).addCallback(
+            lambda it: (it, moreSegments)
+        )
 
+
+
 class DAVResourceWithoutChildrenMixin (object):
     """
     Bits needed from twext.web2.static
@@ -789,6 +804,8 @@
     def locateChild(self, request, segments):
         return self, server.StopTraversal
 
+
+
 class DAVPrincipalResource (DirectoryPrincipalPropertySearchMixIn,
                             SuperDAVPrincipalResource, LoggingMixIn,
                             DirectoryRenderingMixIn):
@@ -888,8 +905,8 @@
         if self.deadProperties().contains((dav_namespace, "resourcetype")):
             return self.deadProperties().get((dav_namespace, "resourcetype"))
         if self.isCollection():
-            return davxml.ResourceType.collection
-        return davxml.ResourceType.empty
+            return davxml.ResourceType.collection #@UndefinedVariable
+        return davxml.ResourceType.empty #@UndefinedVariable
 
     def render(self, request):
         if not self.fp.exists():

Modified: CalendarServer/branches/users/glyph/more-deferreds-6/twistedcaldav/test/test_extensions.py
===================================================================
--- CalendarServer/branches/users/glyph/more-deferreds-6/twistedcaldav/test/test_extensions.py	2010-09-21 18:40:17 UTC (rev 6325)
+++ CalendarServer/branches/users/glyph/more-deferreds-6/twistedcaldav/test/test_extensions.py	2010-09-21 19:08:08 UTC (rev 6326)
@@ -21,11 +21,11 @@
 from twext.web2.http_headers import MimeType
 from twext.web2.static import MetaDataMixin
 
-from twisted.internet.defer import inlineCallbacks, succeed
+from twisted.internet.defer import inlineCallbacks, Deferred, succeed
 from twisted.trial.unittest import TestCase
 from twisted.web.microdom import parseString
 
-from twistedcaldav.extensions import DAVFile
+from twistedcaldav.extensions import DAVFile, DAVResourceWithChildrenMixin
 
 from xml.etree.cElementTree import XML
 
@@ -184,3 +184,30 @@
         yield self.doDirectoryTest([nonASCIIFilename], addUnicodeChild,
                                    [nonASCIIFilename.encode("utf-8")])
 
+
+
+class ChildTraversalTests(TestCase):
+    def test_makeChildDeferred(self):
+        """
+        If L{DAVResourceWithChildrenMixin.makeChild} returns a L{Deferred},
+        L{DAVResourceWithChildrenMixin.locateChild} will return a L{Deferred}.
+        """
+        class FakeChild(object):
+            def __init__(self, name):
+                self.name = name
+        class SmellsLikeDAVResource(object):
+            def __init__(self, **kw):
+                pass
+        class ResourceWithCheese(DAVResourceWithChildrenMixin,
+                                 SmellsLikeDAVResource):
+            def makeChild(self, name):
+                return succeed(FakeChild(name))
+        d = ResourceWithCheese().locateChild(None, ['cheese', 'burger'])
+        self.assertIsInstance(d, Deferred)
+        x = []
+        d.addCallback(x.append)
+        self.assertEquals(len(x), 1)
+        [result] = x
+        self.assertEquals(len(result), 2)
+        self.assertEquals(result[0].name, 'cheese')
+        self.assertEquals(result[1], ['burger'])
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20100921/80e0937d/attachment-0001.html>


More information about the calendarserver-changes mailing list