From: Brian Warner Date: Sat, 7 Jul 2007 02:38:37 +0000 (-0700) Subject: add IDirectoryNode.get_child_at_path X-Git-Url: https://git.rkrishnan.org/(%5B%5E?a=commitdiff_plain;h=9e42dda6a43a5410c5377ba6dfc1c4e511fde644;p=tahoe-lafs%2Ftahoe-lafs.git add IDirectoryNode.get_child_at_path --- diff --git a/src/allmydata/dirnode.py b/src/allmydata/dirnode.py index d655035b..7d264ee5 100644 --- a/src/allmydata/dirnode.py +++ b/src/allmydata/dirnode.py @@ -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) diff --git a/src/allmydata/interfaces.py b/src/allmydata/interfaces.py index 3ec401ec..1aa40466 100644 --- a/src/allmydata/interfaces.py +++ b/src/allmydata/interfaces.py @@ -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. diff --git a/src/allmydata/test/test_dirnode.py b/src/allmydata/test/test_dirnode.py index 43b9b87c..f3a27c3f 100644 --- a/src/allmydata/test/test_dirnode.py +++ b/src/allmydata/test/test_dirnode.py @@ -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):