Ejemplo n.º 1
0
def test_tree2():
    """Tree2."""
    root = Node("root")
    s0 = Node("sub0", parent=root, edge=2)
    Node("sub0B", parent=s0, foo=4, edge=109)
    Node("sub0A", parent=s0, edge="")
    s1 = Node("sub1", parent=root, edge="")
    Node("sub1A", parent=s1, edge=7)
    Node("sub1B", parent=s1, edge=8)
    s1c = Node("sub1C", parent=s1, edge=22)
    Node("sub1Ca", parent=s1c, edge=42)

    def nodenamefunc(node):
        return '%s:%s' % (node.name, node.depth)

    def edgeattrfunc(node, child):
        return 'label="%s:%s"' % (node.name, child.name)

    r = RenderTreeGraph(root,
                        options=["rankdir=LR;"],
                        nodenamefunc=nodenamefunc,
                        nodeattrfunc=lambda node: "shape=box",
                        edgeattrfunc=edgeattrfunc)

    r.to_dotfile(join(GENPATH, "tree2.dot"))
    assert cmp(join(GENPATH, "tree2.dot"), join(REFPATH, "tree2.dot"))
def render_tree(tree, filepath):
    """Wrapper around rendering trees into `png` or `dot` formats. Tree are 
    rendered from the root node.
    
    Args:
        tree (NodeMixin): 
        filepath (str|Path): 
    """
    from anytree.dotexport import RenderTreeGraph
    tree = RenderTreeGraph(tree.root)
    base, ext = os.path.splitext(filepath)
    if ext == '.png':
        tree.to_picture(filename=filepath)
    elif ext == '.dot':
        tree.to_dotfile(filename=filepath)
    else:
        raise Exception