[47] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | |
---|
[48] | 4 | # PyKotIcon - Windows System Tray Icon for PyKota |
---|
[47] | 5 | # |
---|
| 6 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
| 7 | # This program is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
| 19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 20 | # |
---|
[53] | 21 | # $Id$ |
---|
[47] | 22 | # |
---|
| 23 | # |
---|
| 24 | |
---|
| 25 | import sys |
---|
[63] | 26 | import os |
---|
[47] | 27 | import urllib |
---|
| 28 | import urllib2 |
---|
| 29 | |
---|
[57] | 30 | try : |
---|
| 31 | import win32api |
---|
| 32 | import win32net |
---|
| 33 | import win32netcon |
---|
| 34 | hasWindowsExtensions = 1 |
---|
| 35 | except ImportError : |
---|
| 36 | hasWindowsExtensions = 0 |
---|
[58] | 37 | raise ImportError, "Mark Hammond's Win32 Extensions are missing. Please install them." |
---|
[57] | 38 | |
---|
[58] | 39 | try : |
---|
| 40 | import wxPython.wx |
---|
| 41 | import wx |
---|
[63] | 42 | import wx.grid as gridlib |
---|
[58] | 43 | hasWxPython = 1 |
---|
| 44 | except ImportError : |
---|
| 45 | hasWxPython = 0 |
---|
| 46 | raise ImportError, "wxPython for Windows is missing. Please install it." |
---|
| 47 | |
---|
[47] | 48 | DUMPYKOTA_URL = "http://cgi.librelogiciel.com/cgi-bin/dumpykota.cgi" |
---|
| 49 | |
---|
[56] | 50 | class UserQuota : |
---|
| 51 | """A class for PyKota User Print Quota entries.""" |
---|
| 52 | def __init__(self, printername, pagecount, softlimit, hardlimit, datelimit): |
---|
| 53 | """Initialize user print quota datas.""" |
---|
| 54 | self.PrinterName = printername |
---|
| 55 | try : |
---|
| 56 | self.PageCounter = int(pagecount) |
---|
| 57 | except (ValueError, TypeError) : |
---|
| 58 | self.PageCounter = 0 |
---|
| 59 | try : |
---|
| 60 | self.SoftLimit = int(softlimit) |
---|
| 61 | except (ValueError, TypeError) : |
---|
| 62 | self.SoftLimit = None |
---|
| 63 | try : |
---|
| 64 | self.HardLimit = int(hardlimit) |
---|
| 65 | except (ValueError, TypeError) : |
---|
| 66 | self.HardLimit = None |
---|
| 67 | self.DateLimit = datelimit |
---|
| 68 | |
---|
| 69 | class User : |
---|
| 70 | """A class for PyKota users.""" |
---|
| 71 | def __init__(self, username, userinfo, userquotas) : |
---|
| 72 | """Initialize user datas.""" |
---|
| 73 | self.Name = username |
---|
| 74 | self.LimitBy = userinfo["limitby"][0].lower() |
---|
| 75 | try : |
---|
| 76 | self.Balance = float(userinfo["balance"][0]) |
---|
| 77 | except (ValueError, TypeError) : |
---|
| 78 | self.Balance = 0.0 |
---|
| 79 | try : |
---|
| 80 | self.LifeTimePaid = float(userinfo["lifetimepaid"][0]) |
---|
| 81 | except (ValueError, TypeError) : |
---|
| 82 | self.LifeTimePaid = 0.0 |
---|
| 83 | self.Quotas = [] |
---|
| 84 | for i in range(len(userquotas["printername"])) : |
---|
| 85 | printername = userquotas["printername"][i] |
---|
| 86 | pagecounter = userquotas["pagecounter"][i] |
---|
| 87 | softlimit = userquotas["softlimit"][i] |
---|
| 88 | hardlimit = userquotas["hardlimit"][i] |
---|
| 89 | datelimit = userquotas["datelimit"][i] |
---|
| 90 | self.Quotas.append(UserQuota(printername, pagecounter, softlimit, \ |
---|
| 91 | hardlimit, datelimit)) |
---|
| 92 | |
---|
[55] | 93 | class CGINetworkInterface : |
---|
| 94 | """A class for all network interactions.""" |
---|
[56] | 95 | def __init__(self, cgiurl, authname=None, authpw=None) : |
---|
[55] | 96 | """Initialize CGI connection datas.""" |
---|
[56] | 97 | self.cgiurl = cgiurl |
---|
| 98 | self.authname = authname |
---|
| 99 | self.authpw = authpw |
---|
[55] | 100 | |
---|
| 101 | def retrieveDatas(self, arguments, fieldnames) : |
---|
| 102 | """Retrieve datas from the CGI script.""" |
---|
| 103 | args = { "report" : 1, |
---|
| 104 | "format" : "csv", |
---|
| 105 | } |
---|
| 106 | args.update(arguments) |
---|
| 107 | answer = {} |
---|
| 108 | try : |
---|
[56] | 109 | url = "%s?%s" % (self.cgiurl, urllib.urlencode(args)) |
---|
[55] | 110 | u = urllib2.urlopen(url) |
---|
| 111 | lines = u.readlines() |
---|
| 112 | except IOError, msg : |
---|
| 113 | raise IOError, "Unable to retrieve %s : %s" % (url, msg) |
---|
| 114 | else : |
---|
| 115 | u.close() |
---|
| 116 | try : |
---|
| 117 | lines = [ line.strip().split(",") for line in lines ] |
---|
| 118 | fields = [field[1:-1] for field in lines[0]] |
---|
| 119 | indices = [fields.index(fname) for fname in fieldnames] |
---|
| 120 | answer = dict([ (fieldname, \ |
---|
[56] | 121 | [ line[fields.index(fieldname)][1:-1] \ |
---|
| 122 | for line in lines[1:] ]) \ |
---|
[55] | 123 | for fieldname in fieldnames ]) |
---|
| 124 | except : |
---|
| 125 | raise ValueError, "Invalid datas retrieved from %s" % url |
---|
| 126 | return answer |
---|
| 127 | |
---|
| 128 | def getPrinterNames(self) : |
---|
| 129 | """Retrieve the printer's names.""" |
---|
| 130 | arguments = { "report" : 1, |
---|
| 131 | "format" : "csv", |
---|
| 132 | "datatype" : "printers", |
---|
| 133 | } |
---|
| 134 | return self.retrieveDatas(arguments, ["printername"])["printername"] |
---|
| 135 | |
---|
| 136 | def getUserInfo(self, username) : |
---|
| 137 | """Retrieve the user's information.""" |
---|
| 138 | arguments = { "datatype" : "users", |
---|
| 139 | "filter" : "username=%s" % username, |
---|
| 140 | } |
---|
[58] | 141 | return self.retrieveDatas(arguments, ("limitby", "balance", \ |
---|
| 142 | "lifetimepaid")) |
---|
[55] | 143 | |
---|
| 144 | def getUserPQuotas(self, username) : |
---|
| 145 | """Retrieve the user's print quota information.""" |
---|
| 146 | arguments = { "datatype" : "upquotas", |
---|
| 147 | "filter" : "username=%s" % username, |
---|
| 148 | } |
---|
[56] | 149 | return self.retrieveDatas(arguments, ("printername", "pagecounter", \ |
---|
[58] | 150 | "softlimit", "hardlimit", \ |
---|
| 151 | "datelimit")) |
---|
[56] | 152 | |
---|
| 153 | def getUser(self, username) : |
---|
| 154 | """Retrieves the user account and quota information.""" |
---|
| 155 | info = self.getUserInfo(username) |
---|
[60] | 156 | if info : |
---|
| 157 | quotas = self.getUserPQuotas(username) |
---|
| 158 | return User(username, info, quotas) |
---|
[47] | 159 | |
---|
[63] | 160 | class PyKotIconGrid(gridlib.Grid) : |
---|
| 161 | """A class for user print quota entries.""" |
---|
| 162 | def __init__(self, parent, quotas) : |
---|
| 163 | gridlib.Grid.__init__(self, parent, -1) |
---|
| 164 | self.CreateGrid(len(quotas), 4) |
---|
| 165 | self.EnableEditing(False) |
---|
| 166 | self.SetColLabelValue(0, "Page Counter") |
---|
| 167 | self.SetColLabelValue(1, "Soft Limit") |
---|
| 168 | self.SetColLabelValue(2, "Hard Limit") |
---|
| 169 | self.SetColLabelValue(3, "Date Limit") |
---|
| 170 | attr = gridlib.GridCellAttr() |
---|
| 171 | attr.SetAlignment(wx.ALIGN_RIGHT, wx.ALIGN_RIGHT) |
---|
| 172 | attr.SetReadOnly(True) |
---|
| 173 | self.SetColAttr(0, attr) |
---|
| 174 | self.SetColAttr(1, attr) |
---|
| 175 | self.SetColAttr(2, attr) |
---|
| 176 | attr = gridlib.GridCellAttr() |
---|
| 177 | attr.SetAlignment(wx.ALIGN_CENTRE, wx.ALIGN_CENTRE) |
---|
| 178 | attr.SetReadOnly(True) |
---|
| 179 | self.SetColAttr(3, attr) |
---|
| 180 | for i in range(len(quotas)) : |
---|
| 181 | q = quotas[i] |
---|
| 182 | self.SetRowLabelValue(i, quotas[i].PrinterName) |
---|
| 183 | self.SetCellValue(i, 0, str(q.PageCounter)) |
---|
| 184 | self.SetCellValue(i, 1, str(q.SoftLimit)) |
---|
| 185 | self.SetCellValue(i, 2, str(q.HardLimit)) |
---|
| 186 | self.SetCellValue(i, 3, str(q.DateLimit)) |
---|
| 187 | attr = gridlib.GridCellAttr() |
---|
| 188 | colour = wx.GREEN |
---|
| 189 | if q.SoftLimit is not None : |
---|
| 190 | if q.PageCounter >= q.SoftLimit : |
---|
| 191 | colour = wx.RED |
---|
| 192 | elif q.HardLimit is not None : |
---|
| 193 | if q.PageCounter >= q.HardLimit : |
---|
| 194 | colour = wx.RED |
---|
| 195 | attr.SetBackgroundColour(colour) |
---|
| 196 | self.SetRowAttr(i, attr) |
---|
| 197 | |
---|
[58] | 198 | class PyKotIcon(wxPython.wx.wxFrame): |
---|
[63] | 199 | """Main class.""" |
---|
| 200 | def __init__(self, parent, id): |
---|
| 201 | wxPython.wx.wxFrame.__init__(self, parent, -1, \ |
---|
| 202 | "PyKota Print Quota for user %s" % self.getCurrentUserName(), \ |
---|
| 203 | # size=(640, 480), \ |
---|
| 204 | style = wxPython.wx.wxDEFAULT_FRAME_STYLE | wxPython.wx.wxNO_FULL_REPAINT_ON_RESIZE) |
---|
| 205 | self.greenicon = wxPython.wx.wxIcon(os.path.join("..", "icons", "pykoticon-green.ico"), \ |
---|
[58] | 206 | wxPython.wx.wxBITMAP_TYPE_ICO) |
---|
[63] | 207 | self.redicon = wxPython.wx.wxIcon(os.path.join("..", "icons", "pykoticon-red.ico"), \ |
---|
| 208 | wxPython.wx.wxBITMAP_TYPE_ICO) |
---|
| 209 | self.SetIcon(self.greenicon) |
---|
[58] | 210 | |
---|
| 211 | self.tbicon = wxPython.wx.wxTaskBarIcon() |
---|
[63] | 212 | self.tbicon.SetIcon(self.greenicon, "PyKotIcon") |
---|
| 213 | |
---|
[58] | 214 | wxPython.wx.EVT_TASKBAR_LEFT_DCLICK(self.tbicon, self.OnTaskBarActivate) |
---|
| 215 | wxPython.wx.EVT_TASKBAR_RIGHT_UP(self.tbicon, self.OnTaskBarMenu) |
---|
| 216 | wxPython.wx.EVT_ICONIZE(self, self.OnIconify) |
---|
| 217 | |
---|
| 218 | self.TBMENU_RESTORE = wx.NewId() |
---|
| 219 | self.TBMENU_CLOSE = wx.NewId() |
---|
| 220 | wxPython.wx.EVT_MENU(self.tbicon, self.TBMENU_RESTORE, \ |
---|
| 221 | self.OnTaskBarActivate) |
---|
| 222 | wxPython.wx.EVT_MENU(self.tbicon, self.TBMENU_CLOSE, \ |
---|
| 223 | self.OnTaskBarClose) |
---|
| 224 | self.menu = wxPython.wx.wxMenu() |
---|
| 225 | self.menu.Append(self.TBMENU_RESTORE, "Show Print Quota") |
---|
| 226 | self.menu.Append(self.TBMENU_CLOSE, "Quit") |
---|
[59] | 227 | |
---|
| 228 | self.TBTIMER = wx.NewId() |
---|
[63] | 229 | self.chrono = wxPython.wx.wxTimer(self, self.TBTIMER) |
---|
| 230 | wxPython.wx.EVT_TIMER(self, self.TBTIMER, self.OnChronoTimer) |
---|
[59] | 231 | |
---|
| 232 | self.User = None |
---|
| 233 | self.networkInterface = CGINetworkInterface(DUMPYKOTA_URL) |
---|
[63] | 234 | self.inTimer = False |
---|
| 235 | self.chrono.Start(200) # first time in 0.25 second |
---|
[58] | 236 | |
---|
[59] | 237 | def getCurrentUserName(self) : |
---|
| 238 | """Returns the name of the current user.""" |
---|
| 239 | return win32api.GetUserName() |
---|
| 240 | |
---|
| 241 | def OnChronoTimer(self, event) : |
---|
| 242 | """Retrieves user's data quota information.""" |
---|
[63] | 243 | # we stop it there, needed because we want to |
---|
| 244 | # change the delay the first time. |
---|
| 245 | self.chrono.Stop() |
---|
| 246 | if self.inTimer is False : # avoids re-entrance |
---|
| 247 | self.inTimer = True |
---|
[59] | 248 | self.User = self.networkInterface.getUser(self.getCurrentUserName()) |
---|
[63] | 249 | if self.User.LimitBy == "balance" : |
---|
| 250 | if self.User.Balance <= 0.0 : |
---|
| 251 | self.tbicon.SetIcon(self.redicon, "PyKotIcon") |
---|
| 252 | else : |
---|
| 253 | self.tbicon.SetIcon(self.greenicon, "PyKotIcon") |
---|
| 254 | else : |
---|
| 255 | isRed = False |
---|
| 256 | for q in self.User.Quotas : |
---|
| 257 | if q.SoftLimit is not None : |
---|
| 258 | if q.PageCounter >= q.SoftLimit : |
---|
| 259 | isRed = True |
---|
| 260 | break |
---|
| 261 | elif q.HardLimit is not None : |
---|
| 262 | if q.PageCounter >= q.HardLimit : |
---|
| 263 | isRed = True |
---|
| 264 | break |
---|
| 265 | if isRed is True : |
---|
| 266 | self.tbicon.SetIcon(self.redicon, "PyKotIcon") |
---|
| 267 | else : |
---|
| 268 | self.tbicon.SetIcon(self.greenicon, "PyKotIcon") |
---|
| 269 | if hasattr(self, "quotasgrid") : |
---|
| 270 | self.quotasgrid.Destroy() |
---|
| 271 | del self.quotasgrid |
---|
| 272 | self.quotasgrid = PyKotIconGrid(self, self.User.Quotas) |
---|
| 273 | self.inTimer = False |
---|
| 274 | # Now we want it every 3 minutes |
---|
| 275 | self.chrono.Start(1000 * 60 * 3) # every 3 minutes |
---|
[59] | 276 | |
---|
| 277 | def OnIconify(self, event) : |
---|
[58] | 278 | self.Hide() |
---|
| 279 | |
---|
[59] | 280 | def OnTaskBarActivate(self, event) : |
---|
| 281 | if self.IsIconized() : |
---|
[58] | 282 | self.Iconize(False) |
---|
[59] | 283 | if not self.IsShown() : |
---|
[58] | 284 | self.Show(True) |
---|
| 285 | self.Raise() |
---|
| 286 | |
---|
[59] | 287 | def OnCloseWindow(self, event) : |
---|
| 288 | if hasattr(self, "chrono") : |
---|
| 289 | self.chrono.Stop() |
---|
| 290 | del self.chrono |
---|
[58] | 291 | if hasattr(self, "menu") : |
---|
| 292 | menu.Destroy() |
---|
| 293 | del self.menu |
---|
[59] | 294 | if hasattr(self, "tbicon") : |
---|
[58] | 295 | self.tbicon.Destroy() |
---|
| 296 | del self.tbicon |
---|
| 297 | self.Destroy() |
---|
| 298 | |
---|
[59] | 299 | def OnTaskBarMenu(self, evt) : |
---|
[58] | 300 | self.tbicon.PopupMenu(self.menu) |
---|
| 301 | |
---|
[59] | 302 | def OnTaskBarClose(self, evt) : |
---|
[58] | 303 | self.Close() |
---|
| 304 | wxPython.wx.wxGetApp().ProcessIdle() |
---|
| 305 | |
---|
| 306 | class PyKotIconApp(wxPython.wx.wxApp): |
---|
| 307 | def OnInit(self) : |
---|
[63] | 308 | try : |
---|
| 309 | frame = PyKotIcon(None, -1) |
---|
| 310 | except : |
---|
| 311 | crashed() |
---|
| 312 | else : |
---|
| 313 | frame.Show(True) |
---|
[58] | 314 | return True |
---|
| 315 | |
---|
[63] | 316 | def main(): |
---|
| 317 | """Program's entry point.""" |
---|
| 318 | app = PyKotIconApp() |
---|
| 319 | app.MainLoop() |
---|
| 320 | |
---|
[58] | 321 | def crashed() : |
---|
| 322 | """Minimal crash method.""" |
---|
| 323 | import traceback |
---|
| 324 | lines = [] |
---|
| 325 | for line in traceback.format_exception(*sys.exc_info()) : |
---|
| 326 | lines.extend([l for l in line.split("\n") if l]) |
---|
| 327 | msg = "ERROR: ".join(["%s\n" % l for l in (["ERROR: PyKotIcon"] + lines)]) |
---|
| 328 | sys.stderr.write(msg) |
---|
| 329 | sys.stderr.flush() |
---|
[47] | 330 | |
---|
[58] | 331 | def test() : |
---|
| 332 | """Runs in test mode (console).""" |
---|
[60] | 333 | if len(sys.argv) >= 3 : |
---|
| 334 | username = sys.argv[2] |
---|
| 335 | else : |
---|
| 336 | username = win32api.GetUserName() |
---|
[58] | 337 | net = CGINetworkInterface(DUMPYKOTA_URL) |
---|
| 338 | user = net.getUser(username) |
---|
| 339 | print "UserName : ", user.Name |
---|
| 340 | print "LimitBy : ", user.LimitBy |
---|
| 341 | print "Balance : ", user.Balance |
---|
| 342 | for q in user.Quotas : |
---|
| 343 | print "\tPrinterName : ", q.PrinterName |
---|
| 344 | print "\tPageCounter : ", q.PageCounter |
---|
| 345 | print "\tSoftLimit : ", q.SoftLimit |
---|
| 346 | print "\tHardLimit : ", q.HardLimit |
---|
| 347 | print "\tDateLimit : ", q.DateLimit |
---|
| 348 | print |
---|
[47] | 349 | |
---|
[58] | 350 | if __name__ == '__main__': |
---|
[60] | 351 | if (len(sys.argv) >= 2) and (sys.argv[1] == "--test") : |
---|
[58] | 352 | test() |
---|
| 353 | else : |
---|
| 354 | main() |
---|
| 355 | |
---|