first python release

Posted by Matt Rose Tue, 12 Jul 2005 10:31:44 GMT

this is a little snippet I wrote to monitor the bugzilla server at work, and put a tray icon in my GNOME panel to tell me that there is a bug in the queue.

the code is below. there's probably some wordwrap issues.

The huge todo list. Properly deal with when someone reassigns a bug out of the queue, and kill the notification. properly background the process, so that I can just run it out of the gnome session manager. have the ability to configure the icon have the ability to pass in the filename containing the url on the command line, or pass in the url on the command line. Later on: Properly deal with more than one entry in the queue, and print out a list of bugnumbers and summaries. I already set up the dictionary to do this right, but I can't figure out what to do with it. I probably should look at porting libnotify to python, if no-one else has.

from sgmllib import SGMLParser
import os,sys,signal
import re
import urllib
import time
class TableParser(SGMLParser):
        def reset(self):
                SGMLParser.reset(self)
                self.entries = []
                self.placement = 0
                self.count = 0

        def start_tr(self,attrs):
                buglist = [v for k, v in attrs if re.match('bz', v)]
                if buglist:
                        self.placement += 1
                        self.count += 1
        def end_tr(self):
                self.placement = 0

        def start_td(self,attrs):
                if self.placement == 1:
                        self.placement += 1

        def handle_data(self,string):
                if self.placement == 2:
                        self.entries.append(string)

def geturl(file):
        fsock = open(file)
        url = fsock.read()
        sock = urllib.urlopen(url)
        parser = TableParser()
        parser.feed(sock.read())
        parser.close()
        sock.close()
        allbugs = {}
        while parser.count > 0:
                bugstring = parser.entries[:23]
                del parser.entries [:23]
                subj = bugstring[21].strip()
                bugno = bugstring[1]
                bug = [bugstring[4],bugstring[7],bugstring[10],bugstring[13],bugstring[16],subj]
                allbugs[bugno] = bug
                parser.count -= 1
        return allbugs
def bugcount(allbugs):
        len(allbugs)
        return bugcount

def format(allbugs):
        string = ["%s: %s" % (k, v) for k, v in allbugs.items()]
        return string

if __name__ == "__main__":
        while 1:
                time.sleep(60)
                allbugs = geturl('../bugsurlall')
                if len(allbugs) > 0:

                         rc = os.spawnvp('P_WAIT','zenity',['zenity','--notification','--window-icon=/usr/share/pixmaps/apple-red.png'])