Friday 28 September 2007

Test notifications in gnome



I was quite impressed about rails/mac folks creating fancy test notifications by simply connecting zentest/autotest with growl. I wondered how can I get something similar in gnome. As it turns out - it's pretty straightforward - involves only 4 lines of python code and works using dbus. It uses one of the parts freedesktop.org - NotifyService. I created a tiny script for that. It looks rather nice and integrates well (the same service is used by gajim or rhythmbox).


Here's how you invoke it: konrad@machine:~$ python ./bin/test_notifier/test_notifier.py pass 'Test passed' "4 tests
9 assertions
0 failures
0 errors"

konrad@machine:~$ python ./bin/test_notifier/test_notifier.py fail 'Test failed' "4 tests
9 assertions
2 failures
0 errors"


And the code itself (btw: forget about 4 lines - here comes error handling:>)


import sys
import dbus
from os import path, getcwd

try:
status, title, message = sys.argv[1:]
icon_filename = {'pass': 'passed.png', 'fail': 'failed.png'}.get(status)
except Exception:
print 'Usage:\n\t\ttest_notifier [fail|pass] title message'
exit(0)

bus = dbus.SessionBus()
notifyService = bus.get_object("org.freedesktop.Notifications",
'/org/freedesktop/Notifications')
interface = dbus.Interface(notifyService,
'org.freedesktop.Notifications')

icon_filename = path.join(getcwd(),
path.dirname(__file__),
icon_filename)

interface.Notify('test_runner', 0,
icon_filename,
title, message,
[], {}, -1)

1 comment:

Tartley said...

This is brilliant.... let me try it out, as soon as I've moved house this weekend...