def dehead_filetree(tree: ExtractFileTree) -> ExtractFileTree:
"""Remove the head of the given filetree while preserving the old head
name.
So a tree ``{1: [2: [3: [4: [f1, f2]]]}`` will be converted to
``{1: [f1, f2]}``.
:param dict tree: The file tree as generated by :py:func:`extract`.
:returns: The same tree but deheaded as described.
:rtype: dict
"""
assert len(tree) == 1
head_node = list(tree.keys())[0]
head = tree[head_node]
while (
isinstance(head, t.MutableSequence) and len(head) == 1 and
isinstance(head[0], t.MutableMapping) and len(head[0]) == 1
):
head = list(head[0].values())[0]
tree[head_node] = head
return tree
评论列表
文章目录