[1595] | 1 | #! /usr/bin/env python |
---|
| 2 | # -*- coding: ISO-8859-15 -*- |
---|
| 3 | |
---|
| 4 | # PyKota Print Quota Editor |
---|
| 5 | # |
---|
| 6 | # PyKota - Print Quotas for CUPS and LPRng |
---|
| 7 | # |
---|
| 8 | # (c) 2003-2004 Jerome Alet <alet@librelogiciel.com> |
---|
| 9 | # This program is free software; you can redistribute it and/or modify |
---|
| 10 | # it under the terms of the GNU General Public License as published by |
---|
| 11 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 12 | # (at your option) any later version. |
---|
| 13 | # |
---|
| 14 | # This program is distributed in the hope that it will be useful, |
---|
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | # GNU General Public License for more details. |
---|
| 18 | # |
---|
| 19 | # You should have received a copy of the GNU General Public License |
---|
| 20 | # along with this program; if not, write to the Free Software |
---|
| 21 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
---|
| 22 | # |
---|
| 23 | # $Id$ |
---|
| 24 | # |
---|
| 25 | # $Log$ |
---|
[1596] | 26 | # Revision 1.2 2004/07/07 14:14:31 jalet |
---|
| 27 | # Now handles limits by quota in addition to limits by balance |
---|
| 28 | # |
---|
[1595] | 29 | # Revision 1.1 2004/07/07 13:21:27 jalet |
---|
| 30 | # Introduction of the pykosd command |
---|
| 31 | # |
---|
| 32 | # |
---|
| 33 | # |
---|
| 34 | |
---|
| 35 | import sys |
---|
| 36 | import os |
---|
| 37 | import pwd |
---|
| 38 | import time |
---|
| 39 | import pyosd |
---|
| 40 | |
---|
| 41 | from pykota.tool import PyKotaTool, PyKotaToolError, crashed |
---|
| 42 | |
---|
| 43 | if __name__ == "__main__" : |
---|
| 44 | retcode = -1 |
---|
| 45 | try : |
---|
| 46 | cmd = PyKotaTool(doc="A tool to display remaining print units") |
---|
| 47 | except : |
---|
| 48 | crashed("Initialization problem.") |
---|
| 49 | else : |
---|
| 50 | try : |
---|
| 51 | uid = os.geteuid() |
---|
| 52 | uname = pwd.getpwuid(uid)[0] |
---|
| 53 | user = cmd.storage.getUser(uname) |
---|
| 54 | if not user.Exists : |
---|
| 55 | raise PyKotaToolError, "User %s doesn't exist in PyKota's database." % uname |
---|
[1596] | 56 | if user.LimitBy == "quota" : |
---|
| 57 | printers = cmd.storage.getMatchingPrinters("*") |
---|
| 58 | upquotas = [ cmd.storage.getUserPQuota(user, p) for p in printers ] |
---|
| 59 | nblines = len(upquotas) |
---|
| 60 | display = pyosd.osd(colour="#FF0000", timeout=5, shadow=2, lines=nblines) |
---|
| 61 | for line in range(nblines) : |
---|
| 62 | upq = upquotas[line] |
---|
| 63 | if upq.HardLimit is None : |
---|
| 64 | if upq.SoftLimit is None : |
---|
| 65 | percent = 0 |
---|
| 66 | else : |
---|
| 67 | percent = (upq.PageCounter * 100) / upq.SoftLimit |
---|
| 68 | else : |
---|
| 69 | percent = (upq.PageCounter * 100) / upq.HardLimit |
---|
| 70 | percent = min(percent, 100) |
---|
| 71 | display.display("Pages used on %s : %s%%" % (upq.Printer.Name, percent), type=pyosd.TYPE_STRING, line=line) |
---|
| 72 | else : |
---|
| 73 | display = pyosd.osd(colour="#FF0000", timeout=5, shadow=2) |
---|
| 74 | display.display("PyKota Units left : %.2f" % user.AccountBalance, type=pyosd.TYPE_STRING) |
---|
[1595] | 75 | time.sleep(6) |
---|
| 76 | except : |
---|
| 77 | cmd.crashed("Strange problem : please report it ASAP to alet@librelogiciel.com") |
---|
| 78 | else : |
---|
| 79 | retcode = 0 |
---|
| 80 | try : |
---|
| 81 | cmd.storage.close() |
---|
| 82 | except : |
---|
| 83 | pass |
---|
| 84 | sys.exit(retcode) |
---|