Exemplo n.º 1
0
    def test_save(self):
        g = Graph.Graph([(1, 2), (1, 3), (1, 4), (2, 4), (2, 6), (2, 7),
                         (7, 4), (6, 1)])

        dot = Dot.Dot(g)
        dot.style(graph="foobar")
        dot.node_style(1, key="value")
        dot.node_style(2, key="another", key2="world")
        dot.edge_style(1, 4, key1="value1", key2="value2")
        dot.edge_style(2, 4, key1="valueA")

        fn = "test_dot.dot"
        self.assertTrue(not os.path.exists(fn))

        try:
            dot.save_dot(fn)

            fp = open(fn, "r")
            data = fp.read()
            fp.close()
            self.assertEqual(data, "".join(dot))

        finally:
            if os.path.exists(fn):
                os.unlink(fn)
Exemplo n.º 2
0
    def test_node_style(self):
        g = Graph.Graph([
            (1, 2),
            (1, 3),
            (1, 4),
            (2, 4),
            (2, 6),
            (2, 7),
            (7, 4),
            (6, 1),
        ])

        dot = Dot.Dot(g)

        self.assertEqual(dot.nodes[1], {})

        dot.node_style(1, key='value')
        self.assertEqual(dot.nodes[1], {'key': 'value'})

        dot.node_style(1, key2='value2')
        self.assertEqual(dot.nodes[1], {'key2': 'value2'})
        self.assertEqual(dot.nodes[2], {})

        dot.all_node_style(key3='value3')
        for n in g:
            self.assertEqual(dot.nodes[n], {'key3': 'value3'})

        self.assertTrue(9 not in dot.nodes)
        dot.node_style(9, key='value')
        self.assertEqual(dot.nodes[9], {'key': 'value'})
Exemplo n.º 3
0
    def test_save(self):
        g = Graph.Graph([
            (1, 2),
            (1, 3),
            (1, 4),
            (2, 4),
            (2, 6),
            (2, 7),
            (7, 4),
            (6, 1),
        ])

        dot = Dot.Dot(g)
        dot.style(graph="foobar")
        dot.node_style(1, key='value')
        dot.node_style(2, key='another', key2='world')
        dot.edge_style(1, 4, key1='value1', key2='value2')
        dot.edge_style(2, 4, key1='valueA')

        fn = 'test_dot.dot'
        self.assertTrue(not os.path.exists(fn))

        try:
            dot.save_dot(fn)

            fp = open(fn, 'r')
            data = fp.read()
            fp.close()
            self.assertEqual(data, ''.join(dot))

        finally:
            if os.path.exists(fn):
                os.unlink(fn)
Exemplo n.º 4
0
    def test_edge_style(self):
        g = Graph.Graph([
            (1, 2),
            (1, 3),
            (1, 4),
            (2, 4),
            (2, 6),
            (2, 7),
            (7, 4),
            (6, 1),
        ])

        dot = Dot.Dot(g)

        self.assertEqual(dot.edges[1][2], {})
        dot.edge_style(1, 2, foo='bar')
        self.assertEqual(dot.edges[1][2], {'foo': 'bar'})

        dot.edge_style(1, 2, foo2='2bar')
        self.assertEqual(dot.edges[1][2], {'foo2': '2bar'})

        self.assertEqual(dot.edges[1][3], {})

        self.assertFalse(6 in dot.edges[1])
        dot.edge_style(1, 6, foo2='2bar')
        self.assertEqual(dot.edges[1][6], {'foo2': '2bar'})

        self.assertRaises(GraphError, dot.edge_style, 1, 9, a=1)
        self.assertRaises(GraphError, dot.edge_style, 9, 1, a=1)
def nakreslím_graf(graf):
    from altgraph import Dot
    #    ,  GraphStat

    #    statistika = GraphStat.degree_dist(graf)
    #    print(statistika)

    #    # create a dot representation of the graph
    dot = Dot.Dot(graf)
    #
    #    # display the graph
    dot.display()
Exemplo n.º 6
0
    def test_style(self):
        g = Graph.Graph([])

        dot = Dot.Dot(g)

        self.assertEqual(dot.attr, {})

        dot.style(key="value")
        self.assertEqual(dot.attr, {"key": "value"})

        dot.style(key2="value2")
        self.assertEqual(dot.attr, {"key2": "value2"})
Exemplo n.º 7
0
    def test_style(self):
        g = Graph.Graph([])

        dot = Dot.Dot(g)

        self.assertEqual(dot.attr, {})

        dot.style(key='value')
        self.assertEqual(dot.attr, {'key': 'value'})

        dot.style(key2='value2')
        self.assertEqual(dot.attr, {'key2': 'value2'})
Exemplo n.º 8
0
    def test_iter(self):
        g = Graph.Graph([
            (1, 2),
            (1, 3),
            (1, 4),
            (2, 4),
            (2, 6),
            (2, 7),
            (7, 4),
            (6, 1),
        ])

        dot = Dot.Dot(g)
        dot.style(graph="foobar")
        dot.node_style(1, key='value')
        dot.node_style(2, key='another', key2='world')
        dot.edge_style(1, 4, key1='value1', key2='value2')
        dot.edge_style(2, 4, key1='valueA')

        self.assertEqual(list(iter(dot)), list(dot.iterdot()))

        for item in dot.iterdot():
            self.assertTrue(isinstance(item, str))

        first = list(dot.iterdot())[0]
        self.assertEqual(first, "digraph %s {\n" % (dot.name, ))

        dot.type = 'graph'
        first = list(dot.iterdot())[0]
        self.assertEqual(first, "graph %s {\n" % (dot.name, ))

        dot.type = 'foo'
        self.assertRaises(GraphError, list, dot.iterdot())
        dot.type = 'digraph'

        self.assertEqual(list(dot), [
            'digraph G {\n', 'graph="foobar";', '\n', '\t"1" [',
            'key="value",', '];\n', '\t"2" [', 'key="another",',
            'key2="world",', '];\n', '\t"3" [', '];\n', '\t"4" [', '];\n',
            '\t"6" [', '];\n', '\t"7" [', '];\n', '\t"1" -> "2" [', '];\n',
            '\t"1" -> "3" [', '];\n', '\t"1" -> "4" [', 'key1="value1",',
            'key2="value2",', '];\n', '\t"2" -> "4" [', 'key1="valueA",',
            '];\n', '\t"2" -> "6" [', '];\n', '\t"2" -> "7" [', '];\n',
            '\t"6" -> "1" [', '];\n', '\t"7" -> "4" [', '];\n', '}\n'
        ])
Exemplo n.º 9
0
    def test_edge_style(self):
        g = Graph.Graph([(1, 2), (1, 3), (1, 4), (2, 4), (2, 6), (2, 7),
                         (7, 4), (6, 1)])

        dot = Dot.Dot(g)

        self.assertEqual(dot.edges[1][2], {})
        dot.edge_style(1, 2, foo="bar")
        self.assertEqual(dot.edges[1][2], {"foo": "bar"})

        dot.edge_style(1, 2, foo2="2bar")
        self.assertEqual(dot.edges[1][2], {"foo2": "2bar"})

        self.assertEqual(dot.edges[1][3], {})

        self.assertFalse(6 in dot.edges[1])
        dot.edge_style(1, 6, foo2="2bar")
        self.assertEqual(dot.edges[1][6], {"foo2": "2bar"})

        self.assertRaises(GraphError, dot.edge_style, 1, 9, a=1)
        self.assertRaises(GraphError, dot.edge_style, 9, 1, a=1)
Exemplo n.º 10
0
def show_altgraph(g):
    def clean(name):
        try:
            n0 = name.get_name()
        except:
            n0 = str(name)
        n1 = str(n0).replace('"', '')
        n2 = n1.replace("\n", '')
        return n2
    import altgraph
    from altgraph import Graph, Dot
    graph = Graph.Graph()
    for i, v in enumerate(g.get_vertices()):
        graph.add_node(i)  # , node_data=g.get_vertex_name(v)
    for i, v in enumerate(g.get_vertices()):
        for n in g.get_out_neighbors(v):
            graph.add_edge(v, n)
    dot = Dot.Dot(graph)  # , graph_type="digraph"
    for i, v in enumerate(g.get_vertices()):
        dot.node_style(i, label=clean(g.get_vertex_name(v)))
    dot.display()
Exemplo n.º 11
0
    def test_node_style(self):
        g = Graph.Graph([(1, 2), (1, 3), (1, 4), (2, 4), (2, 6), (2, 7),
                         (7, 4), (6, 1)])

        dot = Dot.Dot(g)

        self.assertEqual(dot.nodes[1], {})

        dot.node_style(1, key="value")
        self.assertEqual(dot.nodes[1], {"key": "value"})

        dot.node_style(1, key2="value2")
        self.assertEqual(dot.nodes[1], {"key2": "value2"})
        self.assertEqual(dot.nodes[2], {})

        dot.all_node_style(key3="value3")
        for n in g:
            self.assertEqual(dot.nodes[n], {"key3": "value3"})

        self.assertTrue(9 not in dot.nodes)
        dot.node_style(9, key="value")
        self.assertEqual(dot.nodes[9], {"key": "value"})
Exemplo n.º 12
0
def make_dot(graph):
    """Convert the graph to a dot file.

    Node and edge styles is obtained from the corresponding data.

    Args:
        graph (Graph.Graph): The graph.

    Returns:
        Dot.Dot: The dot file generator.

    """
    dot = Dot.Dot(graph, graphtype="digraph")
    dot.style(overlap=False, bgcolor="transparent")
    for node in graph:
        node_data = graph.node_data(node)
        shape = ("circle" if node_data.type is "vertex" else "doublecircle")
        color, fillcolor = dict(missing=("red", "moccasin"),
                                outdated=("forestgreen", "lightblue")
                                ).get(node_data.status, ("black", "white"))
        dot.node_style(node, shape=shape,
                       style="filled", fillcolor=fillcolor, color=color)
    for edge, edge_data, head, tail in (graph.describe_edge(edge)
                                        for edge in graph.edge_list()):
        section = edge_data.section
        color = dict(fetch="forestgreen",
                     extract="darkgreen",
                     build="blue",
                     runtime="red",
                     virtual="darkgray").get(section, "black")
        style = dict(virtual="dashed").get(section, "solid")
        dot.edge_style(
            head, tail,
            label=section if section not in ("library", "virtual") else "",
            style=style, color=color, fontcolor=color)
    return dot
Exemplo n.º 13
0
    def test_constructor(self):
        g = Graph.Graph([(1, 2), (1, 3), (1, 4), (2, 4), (2, 6), (2, 7),
                         (7, 4), (6, 1)])

        dot = Dot.Dot(g)

        self.assertEqual(dot.name, "G")
        self.assertEqual(dot.attr, {})
        self.assertEqual(dot.temp_dot, "tmp_dot.dot")
        self.assertEqual(dot.temp_neo, "tmp_neo.dot")
        self.assertEqual(dot.dot, "dot")
        self.assertEqual(dot.dotty, "dotty")
        self.assertEqual(dot.neato, "neato")
        self.assertEqual(dot.type, "digraph")

        self.assertEqual(dot.nodes, dict([(x, {}) for x in g]))

        edges = {}
        for head in g:
            edges[head] = {}
            for tail in g.out_nbrs(head):
                edges[head][tail] = {}

        self.assertEqual(dot.edges[1], edges[1])
        self.assertEqual(dot.edges, edges)

        dot = Dot.Dot(
            g,
            nodes=[1, 2],
            edgefn=lambda node: list(sorted(g.out_nbrs(node)))[:-1],
            nodevisitor=lambda node: {"label": node},
            edgevisitor=lambda head, tail: {"label": (head, tail)},
            name="testgraph",
            dot="/usr/local/bin/dot",
            dotty="/usr/local/bin/dotty",
            neato="/usr/local/bin/neato",
            graphtype="graph",
        )

        self.assertEqual(dot.name, "testgraph")
        self.assertEqual(dot.attr, {})
        self.assertEqual(dot.temp_dot, "tmp_dot.dot")
        self.assertEqual(dot.temp_neo, "tmp_neo.dot")
        self.assertEqual(dot.dot, "/usr/local/bin/dot")
        self.assertEqual(dot.dotty, "/usr/local/bin/dotty")
        self.assertEqual(dot.neato, "/usr/local/bin/neato")
        self.assertEqual(dot.type, "graph")

        self.assertEqual(dot.nodes, dict([(x, {"label": x}) for x in [1, 2]]))

        edges = {}
        for head in [1, 2]:
            edges[head] = {}
            for tail in list(sorted(g.out_nbrs(head)))[:-1]:
                if tail not in [1, 2]:
                    continue
                edges[head][tail] = {"label": (head, tail)}

        self.assertEqual(dot.edges[1], edges[1])
        self.assertEqual(dot.edges, edges)

        self.assertRaises(GraphError, Dot.Dot, g, nodes=[1, 2, 9])
Exemplo n.º 14
0
    def test_img(self):
        g = Graph.Graph([(1, 2), (1, 3), (1, 4), (2, 4), (2, 6), (2, 7),
                         (7, 4), (6, 1)])

        dot = Dot.Dot(
            g,
            dot="/usr/local/bin/!!dot",
            dotty="/usr/local/bin/!!dotty",
            neato="/usr/local/bin/!!neato",
        )
        dot.style(size="10,10", rankdir="RL", page="5, 5", ranksep=0.75)
        dot.node_style(1, label="BASE_NODE", shape="box", color="blue")
        dot.node_style(2, style="filled", fillcolor="red")
        dot.edge_style(1, 4, style="dotted")
        dot.edge_style(2, 4, arrowhead="dot", label="binds", labelangle="90")

        system_cmds = []

        def fake_system(cmd):
            system_cmds.append(cmd)
            return None

        try:
            real_system = os.system
            os.system = fake_system

            system_cmds = []
            dot.save_img("foo")
            self.assertEqual(
                system_cmds,
                ["/usr/local/bin/!!dot -Tgif tmp_dot.dot -o foo.gif"])

            system_cmds = []
            dot.save_img("foo", file_type="jpg")
            self.assertEqual(
                system_cmds,
                ["/usr/local/bin/!!dot -Tjpg tmp_dot.dot -o foo.jpg"])

            system_cmds = []
            dot.save_img("bar", file_type="jpg", mode="neato")
            self.assertEqual(
                system_cmds,
                [
                    "/usr/local/bin/!!neato -o tmp_dot.dot tmp_neo.dot",
                    "/usr/local/bin/!!dot -Tjpg tmp_dot.dot -o bar.jpg",
                ],
            )

            system_cmds = []
            dot.display()
            self.assertEqual(system_cmds,
                             ["/usr/local/bin/!!dotty tmp_dot.dot"])

            system_cmds = []
            dot.display(mode="neato")
            self.assertEqual(
                system_cmds,
                [
                    "/usr/local/bin/!!neato -o tmp_dot.dot tmp_neo.dot",
                    "/usr/local/bin/!!dotty tmp_dot.dot",
                ],
            )

        finally:
            if os.path.exists(dot.temp_dot):
                os.unlink(dot.temp_dot)
            if os.path.exists(dot.temp_neo):
                os.unlink(dot.temp_neo)
            os.system = real_system

        if os.path.exists("/usr/local/bin/dot") and os.path.exists(
                "/usr/local/bin/neato"):
            try:
                dot.dot = "/usr/local/bin/dot"
                dot.neato = "/usr/local/bin/neato"
                self.assertFalse(os.path.exists("foo.gif"))
                dot.save_img("foo")
                self.assertTrue(os.path.exists("foo.gif"))
                os.unlink("foo.gif")

                self.assertFalse(os.path.exists("foo.gif"))
                dot.save_img("foo", mode="neato")
                self.assertTrue(os.path.exists("foo.gif"))
                os.unlink("foo.gif")

            finally:
                if os.path.exists(dot.temp_dot):
                    os.unlink(dot.temp_dot)
                if os.path.exists(dot.temp_neo):
                    os.unlink(dot.temp_neo)
Exemplo n.º 15
0
    def test_img(self):
        g = Graph.Graph([
            (1, 2),
            (1, 3),
            (1, 4),
            (2, 4),
            (2, 6),
            (2, 7),
            (7, 4),
            (6, 1),
        ])

        dot = Dot.Dot(g,
                      dot='/usr/local/bin/!!dot',
                      dotty='/usr/local/bin/!!dotty',
                      neato='/usr/local/bin/!!neato')
        dot.style(size='10,10', rankdir='RL', page='5, 5', ranksep=0.75)
        dot.node_style(1, label='BASE_NODE', shape='box', color='blue')
        dot.node_style(2, style='filled', fillcolor='red')
        dot.edge_style(1, 4, style='dotted')
        dot.edge_style(2, 4, arrowhead='dot', label='binds', labelangle='90')

        system_cmds = []

        def fake_system(cmd):
            system_cmds.append(cmd)
            return None

        try:
            real_system = os.system
            os.system = fake_system

            system_cmds = []
            dot.save_img('foo')
            self.assertEqual(
                system_cmds,
                ['/usr/local/bin/!!dot -Tgif tmp_dot.dot -o foo.gif'])

            system_cmds = []
            dot.save_img('foo', file_type='jpg')
            self.assertEqual(
                system_cmds,
                ['/usr/local/bin/!!dot -Tjpg tmp_dot.dot -o foo.jpg'])

            system_cmds = []
            dot.save_img('bar', file_type='jpg', mode='neato')
            self.assertEqual(system_cmds, [
                '/usr/local/bin/!!neato -o tmp_dot.dot tmp_neo.dot',
                '/usr/local/bin/!!dot -Tjpg tmp_dot.dot -o bar.jpg',
            ])

            system_cmds = []
            dot.display()
            self.assertEqual(system_cmds,
                             ['/usr/local/bin/!!dotty tmp_dot.dot'])

            system_cmds = []
            dot.display(mode='neato')
            self.assertEqual(system_cmds, [
                '/usr/local/bin/!!neato -o tmp_dot.dot tmp_neo.dot',
                '/usr/local/bin/!!dotty tmp_dot.dot'
            ])

        finally:
            if os.path.exists(dot.temp_dot):
                os.unlink(dot.temp_dot)
            if os.path.exists(dot.temp_neo):
                os.unlink(dot.temp_neo)
            os.system = real_system

        if os.path.exists('/usr/local/bin/dot') and os.path.exists(
                '/usr/local/bin/neato'):
            try:
                dot.dot = '/usr/local/bin/dot'
                dot.neato = '/usr/local/bin/neato'
                self.assertFalse(os.path.exists('foo.gif'))
                dot.save_img('foo')
                self.assertTrue(os.path.exists('foo.gif'))
                os.unlink('foo.gif')

                self.assertFalse(os.path.exists('foo.gif'))
                dot.save_img('foo', mode='neato')
                self.assertTrue(os.path.exists('foo.gif'))
                os.unlink('foo.gif')

            finally:
                if os.path.exists(dot.temp_dot):
                    os.unlink(dot.temp_dot)
                if os.path.exists(dot.temp_neo):
                    os.unlink(dot.temp_neo)
Exemplo n.º 16
0
from altgraph import Graph, Dot

# create a graph
edges = [(1, 2), (1, 3), (3, 4), (3, 5), (4, 5), (5, 4)]
graph = Graph.Graph(edges)

# create a dot representation of the graph
dot = Dot.Dot(graph)

# display the graph
dot.display()

# save the dot representation into the mydot.dot file
dot.save_dot(file_name='mydot.dot')

# save dot file as gif image into the graph.gif file
dot.save_img(file_name='graph', file_type='gif')