Rewrite in python

Signed-off-by: Jan Blunck <jblunck@infradead.org>