view nose_notify/build/lib/nose_notify/nose_notify.py @ 4:927dfc9d1f8a v0.2 tip

Adding license and making the command line switch work
author Walter Cruz <walter.php@gmail.com>
date Mon, 03 Mar 2008 11:28:54 -0300
parents c047cd63d175
children
line wrap: on
line source

import sys
from nose.plugins.base import Plugin

from StringIO import StringIO

import traceback
import pygtk
pygtk.require('2.0')
import pynotify
import gtk
from new import instancemethod

def writeln(self,*args):
    for i in args:
        self.write(i)
    self.write("\n")

class NoseNotifyPlugin(Plugin):

    enabled = False
    score = 2
    name = "NotifyPlugin"
    
    def __init__(self):
        super(NoseNotifyPlugin, self).__init__()
        self._buf = StringIO()
        self._buf.writeln = instancemethod(writeln,self._buf,StringIO)

    def addSuccess(self, test):
        pass
   
    def addError(self, test, err):
        pass

    def addFailure(self, test, err):
        pass

    def finalize(self, result):
        self._buf.write("Ran %d test%s" %(result.testsRun, result.testsRun != 1 and "s" or ""))
        self._buf.writeln()
        if not result.wasSuccessful():
            self._buf.write('FAILED ( failures=%d errors=%d)'%(len(result.failures),len(result.errors)))
        else:
            self._buf.write('OK')
        pynotify.init("Basics")
        
        message = 'messagebox_info'
        if not result.wasSuccessful():
            if result.failures > 0:
                message = 'messagebox_warning'
            if result.errors > 0:
                message = 'messagebox_critical'
            
            n = pynotify.Notification("Tests", self._buf.getvalue(),message)
            n.set_urgency('critical')
            icon = gtk.StatusIcon()
            icon.set_from_icon_name('dialog-error')
            n.attach_to_status_icon(icon)
        else:
            n = pynotify.Notification("Tests", self._buf.getvalue(),message)
        n.show()

    def formatErr(self, err):
        exctype, value, tb = err
        return ''.join(traceback.format_exception(exctype, value, tb))

    def setOutputStream(self, stream):
        self.stream = self._buf
        return None

    def startContext(self, ctx):
        pass

    def stopContext(self, ctx):
        pass

    def startTest(self, test):
        pass
        
    def stopTest(self, test):
        pass

    def configure(self, options, conf):
        if options.with_notify:
            self.enabled = True
        return

    def options(self, parser, env):
        Plugin.options(self, parser, env)
        parser.add_option("--with-notify", action='store_true', dest='with_notify', default = False, help= 'Use gnome notify' )