]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
add IDirectoryNode.get_child_at_path
authorBrian Warner <warner@lothar.com>
Sat, 7 Jul 2007 02:38:37 +0000 (19:38 -0700)
committerBrian Warner <warner@lothar.com>
Sat, 7 Jul 2007 02:38:37 +0000 (19:38 -0700)
src/allmydata/dirnode.py
src/allmydata/interfaces.py
src/allmydata/test/test_dirnode.py

index d655035b5bde9dbe2cae2662631133c637f36911..7d264ee57839df352844bb80b23cc11856ab980f 100644 (file)
@@ -362,6 +362,20 @@ class ImmutableDirectoryNode:
         wk, we, rk, index = hashutil.generate_dirnode_keys_from_readkey(rk)
         return "DIR-REFRESH:%s" % idlib.b2a(index)
 
+    def get_child_at_path(self, path):
+        if not path:
+            return self
+        if isinstance(path, (str, unicode)):
+            path = path.split("/")
+        childname = path[0]
+        remaining_path = path[1:]
+        d = self.get(childname)
+        if remaining_path:
+            def _got(node):
+                return node.get_child_at_path(remaining_path)
+            d.addCallback(_got)
+        return d
+
 class MutableDirectoryNode(ImmutableDirectoryNode):
     implements(IDirectoryNode)
 
index 3ec401ec8448c95eddde9d6a90489c791fff51c9..1aa4046698d0e9c6dc6b553c2a43887c79b22b3c 100644 (file)
@@ -260,6 +260,17 @@ class IDirectoryNode(Interface):
         """I return a Deferred that fires with a specific named child node,
         either an IFileNode or an IDirectoryNode."""
 
+    def get_child_at_path(path):
+        """Transform a child path into an IDirectoryNode or IFileNode.
+
+        I perform a recursive series of 'get' operations to find the named
+        descendant node. I return a Deferred that fires with the node, or
+        errbacks with IndexError if the node could not be found.
+
+        The path can be either a single string (slash-separated) or a list of
+        path-name elements.
+        """
+
     def set_uri(name, child_uri):
         """I add a child (by URI) at the specific name. I return a Deferred
         that fires when the operation finishes.
index 43b9b87c3e6d3ddb79b54be3a5b3d9fe87acdb9b..f3a27c3fa9f019139271876f5ce4338a0d18b7de 100644 (file)
@@ -262,6 +262,21 @@ class Test(unittest.TestCase):
         d.addCallback(lambda res:
                       self.failIf(res["baz"].is_mutable()))
 
+        d.addCallback(lambda res: rootnode.get_child_at_path("bar/file2"))
+        def _got_file2(res):
+            self.failUnless(isinstance(res, dirnode.FileNode))
+            self.failUnlessEqual(res.uri, file2)
+        d.addCallback(_got_file2)
+
+        d.addCallback(lambda res: rootnode.get_child_at_path(["bar", "file2"]))
+        d.addCallback(_got_file2)
+
+        d.addCallback(lambda res: self.bar_node.get_child_at_path(["file2"]))
+        d.addCallback(_got_file2)
+
+        d.addCallback(lambda res: self.bar_node.get_child_at_path([]))
+        d.addCallback(lambda res: self.failUnlessIdentical(res, self.bar_node))
+
         # test the manifest
         d.addCallback(lambda res: self.rootnode.build_manifest())
         def _check_manifest(manifest):