add IDirectoryNode.get_child_at_path

This commit is contained in:
Brian Warner 2007-07-06 19:38:37 -07:00
parent 9dc9f59a86
commit 9e42dda6a4
3 changed files with 40 additions and 0 deletions

View 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)

View 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.

View 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):