#! /usr/bin/env python # -*- coding: ISO-8859-15 -*- # PyKota Print Quota Editor # # PyKota - Print Quotas for CUPS and LPRng # # (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$ # # $Log$ # Revision 1.2 2004/07/07 14:14:31 jalet # Now handles limits by quota in addition to limits by balance # # Revision 1.1 2004/07/07 13:21:27 jalet # Introduction of the pykosd command # # # import sys import os import pwd import time import pyosd from pykota.tool import PyKotaTool, PyKotaToolError, crashed if __name__ == "__main__" : retcode = -1 try : cmd = PyKotaTool(doc="A tool to display remaining print units") except : crashed("Initialization problem.") else : try : uid = os.geteuid() uname = pwd.getpwuid(uid)[0] user = cmd.storage.getUser(uname) if not user.Exists : raise PyKotaToolError, "User %s doesn't exist in PyKota's database." % uname if user.LimitBy == "quota" : printers = cmd.storage.getMatchingPrinters("*") upquotas = [ cmd.storage.getUserPQuota(user, p) for p in printers ] nblines = len(upquotas) display = pyosd.osd(colour="#FF0000", timeout=5, shadow=2, lines=nblines) for line in range(nblines) : upq = upquotas[line] if upq.HardLimit is None : if upq.SoftLimit is None : percent = 0 else : percent = (upq.PageCounter * 100) / upq.SoftLimit else : percent = (upq.PageCounter * 100) / upq.HardLimit percent = min(percent, 100) display.display("Pages used on %s : %s%%" % (upq.Printer.Name, percent), type=pyosd.TYPE_STRING, line=line) else : display = pyosd.osd(colour="#FF0000", timeout=5, shadow=2) display.display("PyKota Units left : %.2f" % user.AccountBalance, type=pyosd.TYPE_STRING) time.sleep(6) except : cmd.crashed("Strange problem : please report it ASAP to alet@librelogiciel.com") else : retcode = 0 try : cmd.storage.close() except : pass sys.exit(retcode)