提交 e8336102 编写于 作者: L Lukáš Doktor

avocado..tree: Support for getting node by path

This function can be used to retrieve existing node by path separated
by '/'. Additionally when "create" is supplied it it can create the
missing nodes in the same way as "mkdir -p" in bash.
Signed-off-by: NLukáš Doktor <ldoktor@redhat.com>
上级 3fade65f
......@@ -248,6 +248,29 @@ class TreeNode(object):
child.set_environment_dirty()
self._environment = None
def get_node(self, path, create=False):
"""
:param path: Path of the desired node (relative to this node)
:param create: Create the node (and intermediary ones) when not present
:return: the node associated with this path
:raise ValueError: When path doesn't exist and create not set
"""
node = self
for name in path.split('/'):
if not name:
continue
try:
node = node.children[node.children.index(name)]
except ValueError:
if create:
child = node.__class__(name)
node.add_child(child)
node = child
else:
raise ValueError("Path %s does not exists in this tree\n%s"
% (path, self.get_ascii()))
return node
def iter_children_preorder(self):
""" Iterate through children """
queue = collections.deque()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册