view nose_notify/build/lib/nose_notify/nose_notify.py @ 3:c047cd63d175 v0.2

Better handling of everything
author Walter Cruz <walter.php@gmail.com>
date Fri, 29 Feb 2008 18:57:44 -0300
parents 3cdd7f281e28
children 927dfc9d1f8a
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
        #self._buf.write(test.shortDescription() or str(test))
        
    def stopTest(self, test):
        pass
        #self._buf.write(' ')

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

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