+#!/usr/bin/python
+# -*- encoding: utf-8 -*-
+
+
+# Import graphviz
+import gv
+
+import psycopg2
+import psycopg2.extras
+
+def getcursor():
+ con = psycopg2.connect(database = "chopes")
+ cur = con.cursor(cursor_factory=psycopg2.extras.DictCursor)
+ return con, cur
+
+# Import pygraph
+from pygraph.classes.graph import graph
+from pygraph.classes.digraph import digraph
+from pygraph.algorithms.searching import breadth_first_search
+from pygraph.readwrite.dot import write
+
+
+def getgraph():
+ con, cur = getcursor()
+ cur.execute("SELECT * FROM gens;")
+ l = cur.fetchall()
+ gens = {n: nom.decode("utf8") for n,nom in l}
+ cur.execute("SELECT id1, id2 FROM chopes;")
+ chopes = cur.fetchall()
+ return gens, chopes
+
+
+if __name__ == "__main__":
+ gens, chopes = getgraph()
+ #gr = graph()
+ textgraph = u"graph chope {\n"
+ for noeud in gens.values():
+ textgraph += u'"%s";\n' % noeud
+ #gr.add_nodes(gens.values())
+
+ for chopeur, victime in chopes:
+ textgraph += u'"%s" -- "%s";\n' % (gens[chopeur], gens[victime])
+ #gr.add_edge((gens[chopeur], gens[victime]))
+ textgraph += "}\n"
+ print textgraph
+ # Draw as PNG
+ #dot = write(gr)
+ dot = textgraph.encode("utf8")
+ gvv = gv.readstring(dot)
+ gv.layout(gvv, 'dot')
+ gv.render(gvv, 'png', 'chopes.png')