#! /usr/bin/env python # -*- coding: ISO-8859-15 -*- # PyKotIcon - Windows System Tray Icon for PyKota # # (c) 2003-2004 Jerome Alet # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. # # $Id$ # # import sys import os import urllib import urllib2 try : import win32api import win32net import win32netcon hasWindowsExtensions = 1 except ImportError : hasWindowsExtensions = 0 raise ImportError, "Mark Hammond's Win32 Extensions are missing. Please install them." try : import wxPython.wx import wx import wx.grid as gridlib hasWxPython = 1 except ImportError : hasWxPython = 0 raise ImportError, "wxPython for Windows is missing. Please install it." DUMPYKOTA_URL = "http://cgi.librelogiciel.com/cgi-bin/dumpykota.cgi" class UserQuota : """A class for PyKota User Print Quota entries.""" def __init__(self, printername, pagecount, softlimit, hardlimit, datelimit): """Initialize user print quota datas.""" self.PrinterName = printername try : self.PageCounter = int(pagecount) except (ValueError, TypeError) : self.PageCounter = 0 try : self.SoftLimit = int(softlimit) except (ValueError, TypeError) : self.SoftLimit = None try : self.HardLimit = int(hardlimit) except (ValueError, TypeError) : self.HardLimit = None self.DateLimit = datelimit class User : """A class for PyKota users.""" def __init__(self, username, userinfo, userquotas) : """Initialize user datas.""" self.Name = username self.LimitBy = userinfo["limitby"][0].lower() try : self.Balance = float(userinfo["balance"][0]) except (ValueError, TypeError) : self.Balance = 0.0 try : self.LifeTimePaid = float(userinfo["lifetimepaid"][0]) except (ValueError, TypeError) : self.LifeTimePaid = 0.0 self.Quotas = [] for i in range(len(userquotas["printername"])) : printername = userquotas["printername"][i] pagecounter = userquotas["pagecounter"][i] softlimit = userquotas["softlimit"][i] hardlimit = userquotas["hardlimit"][i] datelimit = userquotas["datelimit"][i] self.Quotas.append(UserQuota(printername, pagecounter, softlimit, \ hardlimit, datelimit)) class CGINetworkInterface : """A class for all network interactions.""" def __init__(self, cgiurl, authname=None, authpw=None) : """Initialize CGI connection datas.""" self.cgiurl = cgiurl self.authname = authname self.authpw = authpw def retrieveDatas(self, arguments, fieldnames) : """Retrieve datas from the CGI script.""" args = { "report" : 1, "format" : "csv", } args.update(arguments) answer = {} try : url = "%s?%s" % (self.cgiurl, urllib.urlencode(args)) u = urllib2.urlopen(url) lines = u.readlines() except IOError, msg : raise IOError, "Unable to retrieve %s : %s" % (url, msg) else : u.close() try : lines = [ line.strip().split(",") for line in lines ] fields = [field[1:-1] for field in lines[0]] indices = [fields.index(fname) for fname in fieldnames] answer = dict([ (fieldname, \ [ line[fields.index(fieldname)][1:-1] \ for line in lines[1:] ]) \ for fieldname in fieldnames ]) except : raise ValueError, "Invalid datas retrieved from %s" % url return answer def getPrinterNames(self) : """Retrieve the printer's names.""" arguments = { "report" : 1, "format" : "csv", "datatype" : "printers", } return self.retrieveDatas(arguments, ["printername"])["printername"] def getUserInfo(self, username) : """Retrieve the user's information.""" arguments = { "datatype" : "users", "filter" : "username=%s" % username, } return self.retrieveDatas(arguments, ("limitby", "balance", \ "lifetimepaid")) def getUserPQuotas(self, username) : """Retrieve the user's print quota information.""" arguments = { "datatype" : "upquotas", "filter" : "username=%s" % username, } return self.retrieveDatas(arguments, ("printername", "pagecounter", \ "softlimit", "hardlimit", \ "datelimit")) def getUser(self, username) : """Retrieves the user account and quota information.""" info = self.getUserInfo(username) if info : quotas = self.getUserPQuotas(username) return User(username, info, quotas) class PyKotIconGrid(gridlib.Grid) : """A class for user print quota entries.""" def __init__(self, parent, quotas) : gridlib.Grid.__init__(self, parent, -1) self.CreateGrid(len(quotas), 4) self.EnableEditing(False) self.SetColLabelValue(0, "Page Counter") self.SetColLabelValue(1, "Soft Limit") self.SetColLabelValue(2, "Hard Limit") self.SetColLabelValue(3, "Date Limit") attr = gridlib.GridCellAttr() attr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_RIGHT) attr.SetReadOnly(True) self.SetColAttr(0, attr) self.SetColAttr(1, attr) self.SetColAttr(2, attr) attr = gridlib.GridCellAttr() attr.SetAlignment(wx.ALIGN_CENTRE, wx.ALIGN_CENTRE) attr.SetReadOnly(True) self.SetColAttr(3, attr) for i in range(len(quotas)) : q = quotas[i] self.SetRowLabelValue(i, quotas[i].PrinterName) self.SetCellValue(i, 0, str(q.PageCounter)) self.SetCellValue(i, 1, str(q.SoftLimit)) self.SetCellValue(i, 2, str(q.HardLimit)) self.SetCellValue(i, 3, str(q.DateLimit)) attr = gridlib.GridCellAttr() colour = wx.GREEN if q.SoftLimit is not None : if q.PageCounter >= q.SoftLimit : colour = wx.RED elif q.HardLimit is not None : if q.PageCounter >= q.HardLimit : colour = wx.RED attr.SetBackgroundColour(colour) self.SetRowAttr(i, attr) class PyKotIcon(wxPython.wx.wxFrame): """Main class.""" def __init__(self, parent, id): wxPython.wx.wxFrame.__init__(self, parent, -1, \ "PyKota Print Quota for user %s" % self.getCurrentUserName(), \ # size=(640, 480), \ style = wxPython.wx.wxDEFAULT_FRAME_STYLE | wxPython.wx.wxNO_FULL_REPAINT_ON_RESIZE) self.greenicon = wxPython.wx.wxIcon(os.path.join("..", "icons", "pykoticon-green.ico"), \ wxPython.wx.wxBITMAP_TYPE_ICO) self.redicon = wxPython.wx.wxIcon(os.path.join("..", "icons", "pykoticon-red.ico"), \ wxPython.wx.wxBITMAP_TYPE_ICO) self.SetIcon(self.greenicon) self.tbicon = wxPython.wx.wxTaskBarIcon() self.tbicon.SetIcon(self.greenicon, "PyKotIcon") wxPython.wx.EVT_TASKBAR_LEFT_DCLICK(self.tbicon, self.OnTaskBarActivate) wxPython.wx.EVT_TASKBAR_RIGHT_UP(self.tbicon, self.OnTaskBarMenu) wxPython.wx.EVT_ICONIZE(self, self.OnIconify) self.TBMENU_RESTORE = wx.NewId() self.TBMENU_CLOSE = wx.NewId() wxPython.wx.EVT_MENU(self.tbicon, self.TBMENU_RESTORE, \ self.OnTaskBarActivate) wxPython.wx.EVT_MENU(self.tbicon, self.TBMENU_CLOSE, \ self.OnTaskBarClose) self.menu = wxPython.wx.wxMenu() self.menu.Append(self.TBMENU_RESTORE, "Show Print Quota") self.menu.Append(self.TBMENU_CLOSE, "Quit") self.TBTIMER = wx.NewId() self.chrono = wxPython.wx.wxTimer(self, self.TBTIMER) wxPython.wx.EVT_TIMER(self, self.TBTIMER, self.OnChronoTimer) self.User = None self.networkInterface = CGINetworkInterface(DUMPYKOTA_URL) self.inTimer = False self.chrono.Start(200) # first time in 0.25 second def getCurrentUserName(self) : """Returns the name of the current user.""" return win32api.GetUserName() def OnChronoTimer(self, event) : """Retrieves user's data quota information.""" # we stop it there, needed because we want to # change the delay the first time. self.chrono.Stop() if self.inTimer is False : # avoids re-entrance self.inTimer = True self.User = self.networkInterface.getUser(self.getCurrentUserName()) if self.User.LimitBy == "balance" : if self.User.Balance <= 0.0 : self.tbicon.SetIcon(self.redicon, "PyKotIcon") else : self.tbicon.SetIcon(self.greenicon, "PyKotIcon") else : isRed = False for q in self.User.Quotas : if q.SoftLimit is not None : if q.PageCounter >= q.SoftLimit : isRed = True break elif q.HardLimit is not None : if q.PageCounter >= q.HardLimit : isRed = True break if isRed is True : self.tbicon.SetIcon(self.redicon, "PyKotIcon") else : self.tbicon.SetIcon(self.greenicon, "PyKotIcon") if hasattr(self, "quotasgrid") : self.quotasgrid.Destroy() del self.quotasgrid self.quotasgrid = PyKotIconGrid(self, self.User.Quotas) self.inTimer = False # Now we want it every 3 minutes self.chrono.Start(1000 * 60 * 3) # every 3 minutes def OnIconify(self, event) : self.Hide() def OnTaskBarActivate(self, event) : if self.IsIconized() : self.Iconize(False) if not self.IsShown() : self.Show(True) self.Raise() def OnCloseWindow(self, event) : if hasattr(self, "chrono") : self.chrono.Stop() del self.chrono if hasattr(self, "menu") : menu.Destroy() del self.menu if hasattr(self, "tbicon") : self.tbicon.Destroy() del self.tbicon self.Destroy() def OnTaskBarMenu(self, evt) : self.tbicon.PopupMenu(self.menu) def OnTaskBarClose(self, evt) : self.Close() wxPython.wx.wxGetApp().ProcessIdle() class PyKotIconApp(wxPython.wx.wxApp): def OnInit(self) : try : frame = PyKotIcon(None, -1) except : crashed() else : frame.Show(True) return True def main(): """Program's entry point.""" app = PyKotIconApp() app.MainLoop() def crashed() : """Minimal crash method.""" import traceback lines = [] for line in traceback.format_exception(*sys.exc_info()) : lines.extend([l for l in line.split("\n") if l]) msg = "ERROR: ".join(["%s\n" % l for l in (["ERROR: PyKotIcon"] + lines)]) sys.stderr.write(msg) sys.stderr.flush() def test() : """Runs in test mode (console).""" if len(sys.argv) >= 3 : username = sys.argv[2] else : username = win32api.GetUserName() net = CGINetworkInterface(DUMPYKOTA_URL) user = net.getUser(username) print "UserName : ", user.Name print "LimitBy : ", user.LimitBy print "Balance : ", user.Balance for q in user.Quotas : print "\tPrinterName : ", q.PrinterName print "\tPageCounter : ", q.PageCounter print "\tSoftLimit : ", q.SoftLimit print "\tHardLimit : ", q.HardLimit print "\tDateLimit : ", q.DateLimit print if __name__ == '__main__': if (len(sys.argv) >= 2) and (sys.argv[1] == "--test") : test() else : main()