]> gitweb.pimeys.fr Git - chopes.git/blob - graphe.py
ce qui permet de faire des graphes (noms seuls et liens, pas d'infos supplémentaires).
[chopes.git] / graphe.py
1 #!/usr/bin/python
2 # -*- encoding: utf-8 -*-
3
4
5 # Import graphviz
6 import gv
7
8 import psycopg2
9 import psycopg2.extras
10
11 def getcursor():
12 con = psycopg2.connect(database = "chopes")
13 cur = con.cursor(cursor_factory=psycopg2.extras.DictCursor)
14 return con, cur
15
16 # Import pygraph
17 from pygraph.classes.graph import graph
18 from pygraph.classes.digraph import digraph
19 from pygraph.algorithms.searching import breadth_first_search
20 from pygraph.readwrite.dot import write
21
22
23 def getgraph():
24 con, cur = getcursor()
25 cur.execute("SELECT * FROM gens;")
26 l = cur.fetchall()
27 gens = {n: nom.decode("utf8") for n,nom in l}
28 cur.execute("SELECT id1, id2 FROM chopes;")
29 chopes = cur.fetchall()
30 return gens, chopes
31
32
33 if __name__ == "__main__":
34 gens, chopes = getgraph()
35 #gr = graph()
36 textgraph = u"graph chope {\n"
37 for noeud in gens.values():
38 textgraph += u'"%s";\n' % noeud
39 #gr.add_nodes(gens.values())
40
41 for chopeur, victime in chopes:
42 textgraph += u'"%s" -- "%s";\n' % (gens[chopeur], gens[victime])
43 #gr.add_edge((gens[chopeur], gens[victime]))
44 textgraph += "}\n"
45 print textgraph
46 # Draw as PNG
47 #dot = write(gr)
48 dot = textgraph.encode("utf8")
49 gvv = gv.readstring(dot)
50 gv.layout(gvv, 'dot')
51 gv.render(gvv, 'png', 'chopes.png')